tor-browser

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

PromiseNativeHandler.h (2622B)


      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 mozilla_dom_PromiseNativeHandler_h
      8 #define mozilla_dom_PromiseNativeHandler_h
      9 
     10 #include <functional>
     11 
     12 #include "js/TypeDecls.h"
     13 #include "js/Value.h"
     14 #include "mozilla/ErrorResult.h"
     15 #include "mozilla/StaticString.h"
     16 #include "nsISupports.h"
     17 
     18 namespace mozilla::dom {
     19 
     20 /*
     21 * PromiseNativeHandler allows C++ to react to a Promise being
     22 * rejected/resolved. A PromiseNativeHandler can be appended to a Promise using
     23 * Promise::AppendNativeHandler().
     24 */
     25 class PromiseNativeHandler : public nsISupports {
     26 protected:
     27  virtual ~PromiseNativeHandler() = default;
     28 
     29 public:
     30  MOZ_CAN_RUN_SCRIPT
     31  virtual void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
     32                                ErrorResult& aRv) = 0;
     33 
     34  MOZ_CAN_RUN_SCRIPT
     35  virtual void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
     36                                ErrorResult& aRv) = 0;
     37 };
     38 
     39 // This base class exists solely to use NS_IMPL_ISUPPORTS because it doesn't
     40 // support template classes.
     41 class MozPromiseRejectOnDestructionBase : public PromiseNativeHandler {
     42  NS_DECL_ISUPPORTS
     43 
     44  void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
     45                        ErrorResult& aRv) override {}
     46  void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
     47                        ErrorResult& aRv) override {}
     48 
     49 protected:
     50  ~MozPromiseRejectOnDestructionBase() override = default;
     51 };
     52 
     53 // Use this when you subscribe to a JS promise to settle a MozPromise that is
     54 // not guaranteed to be settled by anyone else.
     55 template <typename T>
     56 class MozPromiseRejectOnDestruction final
     57    : public MozPromiseRejectOnDestructionBase {
     58 public:
     59  // (Accepting RefPtr<T> instead of T* because compiler fails to implicitly
     60  // convert it at call sites)
     61  MozPromiseRejectOnDestruction(const RefPtr<T>& aMozPromise,
     62                                StaticString aCallSite)
     63      : mMozPromise(aMozPromise), mCallSite(aCallSite) {
     64    MOZ_ASSERT(aMozPromise);
     65  }
     66 
     67 protected:
     68  ~MozPromiseRejectOnDestruction() override {
     69    // Rejecting will be no-op if the promise is already settled
     70    mMozPromise->Reject(NS_BINDING_ABORTED, mCallSite);
     71  }
     72 
     73  RefPtr<T> mMozPromise;
     74  StaticString mCallSite;
     75 };
     76 
     77 }  // namespace mozilla::dom
     78 
     79 #endif  // mozilla_dom_PromiseNativeHandler_h