tor-browser

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

ResolvableNormalOriginOp.h (1930B)


      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 file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef DOM_QUOTA_RESOLVABLENORMALORIGINOP_H_
      8 #define DOM_QUOTA_RESOLVABLENORMALORIGINOP_H_
      9 
     10 #include "NormalOriginOperationBase.h"
     11 #include "mozilla/MozPromise.h"
     12 #include "mozilla/dom/FlippedOnce.h"
     13 
     14 namespace mozilla::dom::quota {
     15 
     16 template <typename ResolveValueT, bool IsExclusive>
     17 class ResolvableNormalOriginOp : public NormalOriginOperationBase {
     18 public:
     19  NS_INLINE_DECL_REFCOUNTING(ResolvableNormalOriginOp, override)
     20 
     21  using PromiseType = MozPromise<ResolveValueT, nsresult, IsExclusive>;
     22 
     23  RefPtr<PromiseType> OnResults() {
     24    AssertIsOnOwningThread();
     25 
     26    return mPromiseHolder.Ensure(__func__);
     27  }
     28 
     29 protected:
     30  ResolvableNormalOriginOp(MovingNotNull<RefPtr<QuotaManager>> aQuotaManager,
     31                           const char* aName)
     32      : NormalOriginOperationBase(std::move(aQuotaManager), aName) {
     33    AssertIsOnOwningThread();
     34  }
     35 
     36  virtual ~ResolvableNormalOriginOp() = default;
     37 
     38  virtual ResolveValueT UnwrapResolveValue() = 0;
     39 
     40 #ifdef DEBUG
     41  bool ResolveValueConsumed() { return mResolveValueConsumed; }
     42 #endif
     43 
     44 private:
     45  void SendResults() override {
     46    if (Canceled()) {
     47      mResultCode = NS_ERROR_FAILURE;
     48    }
     49 
     50    if (NS_SUCCEEDED(mResultCode)) {
     51      mPromiseHolder.ResolveIfExists(UnwrapResolveValue(), __func__);
     52 #ifdef DEBUG
     53      mResolveValueConsumed.Flip();
     54 #endif
     55    } else {
     56      mPromiseHolder.RejectIfExists(mResultCode, __func__);
     57    }
     58  }
     59 
     60  MozPromiseHolder<PromiseType> mPromiseHolder;
     61 #ifdef DEBUG
     62  FlippedOnce<false> mResolveValueConsumed;
     63 #endif
     64 };
     65 
     66 }  // namespace mozilla::dom::quota
     67 
     68 #endif  // DOM_QUOTA_RESOLVABLENORMALORIGINOP_H_