DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_PaymentRequest_h
#define mozilla_dom_PaymentRequest_h

#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/dom/PaymentRequestBinding.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/ErrorResult.h"
#include "nsIDocumentActivity.h"
#include "nsWrapperCache.h"
#include "PaymentRequestUpdateEvent.h"

namespace mozilla {
namespace dom {

class PaymentAddress;
class PaymentRequestChild;
class PaymentResponse;
class ResponseData;

class GeneralDetails final {
 public:
  GeneralDetails() = default;
  ~GeneralDetails() = default;
  nsString details;
};

class BasicCardDetails final {
 public:
  struct Address {
    nsString country;
    nsTArray<nsString> addressLine;
    nsString region;
    nsString regionCode;
    nsString city;
    nsString dependentLocality;
    nsString postalCode;
    nsString sortingCode;
    nsString organization;
    nsString recipient;
    nsString phone;
  };
  BasicCardDetails() = default;
  ~BasicCardDetails() = default;

  Address billingAddress;
};

class ChangeDetails final {
 public:
  enum Type { Unknown = 0, GeneralMethodDetails = 1, BasicCardMethodDetails };
  ChangeDetails() : mType(ChangeDetails::Unknown) {}
  explicit ChangeDetails(const GeneralDetails& aGeneralDetails)
      : mType(GeneralMethodDetails), mGeneralDetails(aGeneralDetails) {}
  explicit ChangeDetails(const BasicCardDetails& aBasicCardDetails)
      : mType(BasicCardMethodDetails), mBasicCardDetails(aBasicCardDetails) {}
  ChangeDetails& operator=(const GeneralDetails& aGeneralDetails) {
    mType = GeneralMethodDetails;
    mGeneralDetails = aGeneralDetails;
    mBasicCardDetails = BasicCardDetails();
    return *this;
  }
  ChangeDetails& operator=(const BasicCardDetails& aBasicCardDetails) {
    mType = BasicCardMethodDetails;
    mGeneralDetails = GeneralDetails();
    mBasicCardDetails = aBasicCardDetails;
    return *this;
  }
  ~ChangeDetails() = default;

  const Type& type() const { return mType; }
  const GeneralDetails& generalDetails() const { return mGeneralDetails; }
  const BasicCardDetails& basicCardDetails() const { return mBasicCardDetails; }

 private:
  Type mType;
  GeneralDetails mGeneralDetails;
  BasicCardDetails mBasicCardDetails;
};

class PaymentRequest final : public DOMEventTargetHelper,
                             public PromiseNativeHandler,
                             public nsIDocumentActivity {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PaymentRequest,
                                                         DOMEventTargetHelper)
  NS_DECL_NSIDOCUMENTACTIVITY

  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

  static already_AddRefed<PaymentRequest> CreatePaymentRequest(
      nsPIDOMWindowInner* aWindow, nsresult& aRv);

  static bool PrefEnabled(JSContext* aCx, JSObject* aObj);

  static nsresult IsValidStandardizedPMI(const nsAString& aIdentifier,
                                         nsAString& aErrorMsg);

  static nsresult IsValidPaymentMethodIdentifier(const nsAString& aIdentifier,
                                                 nsAString& aErrorMsg);

  static nsresult IsValidMethodData(
      JSContext* aCx, const Sequence<PaymentMethodData>& aMethodData,
      nsAString& aErrorMsg);

  static nsresult IsValidNumber(const nsAString& aItem, const nsAString& aStr,
                                nsAString& aErrorMsg);
  static nsresult IsNonNegativeNumber(const nsAString& aItem,
                                      const nsAString& aStr,
                                      nsAString& aErrorMsg);

  static nsresult IsValidCurrencyAmount(const nsAString& aItem,
                                        const PaymentCurrencyAmount& aAmount,
                                        const bool aIsTotalItem,
                                        nsAString& aErrorMsg);

  static nsresult IsValidCurrency(const nsAString& aItem,
                                  const nsAString& aCurrency,
                                  nsAString& aErrorMsg);

  static nsresult IsValidDetailsInit(const PaymentDetailsInit& aDetails,
                                     const bool aRequestShipping,
                                     nsAString& aErrorMsg);

  static nsresult IsValidDetailsUpdate(const PaymentDetailsUpdate& aDetails,
                                       const bool aRequestShipping);

  static nsresult IsValidDetailsBase(const PaymentDetailsBase& aDetails,
                                     const bool aRequestShipping,
                                     nsAString& aErrorMsg);

  static already_AddRefed<PaymentRequest> Constructor(
      const GlobalObject& aGlobal,
      const Sequence<PaymentMethodData>& aMethodData,
      const PaymentDetailsInit& aDetails, const PaymentOptions& aOptions,
      ErrorResult& aRv);

  already_AddRefed<Promise> CanMakePayment(ErrorResult& aRv);
  void RespondCanMakePayment(bool aResult);

  already_AddRefed<Promise> Show(
      const Optional<OwningNonNull<Promise>>& detailsPromise, ErrorResult& aRv);
  void RespondShowPayment(const nsAString& aMethodName,
                          const ResponseData& aData,
                          const nsAString& aPayerName,
                          const nsAString& aPayerEmail,
                          const nsAString& aPayerPhone, nsresult aRv);
  void RejectShowPayment(nsresult aRejectReason);
  void RespondComplete();

  already_AddRefed<Promise> Abort(ErrorResult& aRv);
  void RespondAbortPayment(bool aResult);

  nsresult RetryPayment(JSContext* aCx, const PaymentValidationErrors& aErrors);

  void GetId(nsAString& aRetVal) const;
  void GetInternalId(nsAString& aRetVal);
  void SetId(const nsAString& aId);

  bool Equals(const nsAString& aInternalId) const;

  bool ReadyForUpdate();
  bool IsUpdating() const { return mUpdating; }
  void SetUpdating(bool aUpdating);

  already_AddRefed<PaymentResponse> GetResponse() const;

  already_AddRefed<PaymentAddress> GetShippingAddress() const;
  // Update mShippingAddress and fire shippingaddresschange event
  nsresult UpdateShippingAddress(
      const nsAString& aCountry, const nsTArray<nsString>& aAddressLine,
      const nsAString& aRegion, const nsAString& aRegionCode,
      const nsAString& aCity, const nsAString& aDependentLocality,
      const nsAString& aPostalCode, const nsAString& aSortingCode,
      const nsAString& aOrganization, const nsAString& aRecipient,
      const nsAString& aPhone);

  void SetShippingOption(const nsAString& aShippingOption);
  void GetShippingOption(nsAString& aRetVal) const;
  void GetOptions(PaymentOptions& aRetVal) const;
  void SetOptions(const PaymentOptions& aOptions);
  nsresult UpdateShippingOption(const nsAString& aShippingOption);

  nsresult UpdatePayment(JSContext* aCx, const PaymentDetailsUpdate& aDetails);
  void AbortUpdate(nsresult aRv);

  void SetShippingType(const Nullable<PaymentShippingType>& aShippingType);
  Nullable<PaymentShippingType> GetShippingType() const;

  inline void ShippingWasRequested() { mRequestShipping = true; }

  nsresult UpdatePaymentMethod(const nsAString& aMethodName,
                               const ChangeDetails& aMethodDetails);

  void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
  void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;

  bool InFullyActiveDocument();

  IMPL_EVENT_HANDLER(merchantvalidation);
  IMPL_EVENT_HANDLER(shippingaddresschange);
  IMPL_EVENT_HANDLER(shippingoptionchange);
  IMPL_EVENT_HANDLER(paymentmethodchange);

  void SetIPC(PaymentRequestChild* aChild) { mIPC = aChild; }

  PaymentRequestChild* GetIPC() const { return mIPC; }

 private:
  PaymentOptions mOptions;

 protected:
  ~PaymentRequest();

  void RegisterActivityObserver();
  void UnregisterActivityObserver();

  nsresult DispatchUpdateEvent(const nsAString& aType);

  nsresult DispatchMerchantValidationEvent(const nsAString& aType);

  nsresult DispatchPaymentMethodChangeEvent(const nsAString& aMethodName,
                                            const ChangeDetails& aMethodDatils);

  PaymentRequest(nsPIDOMWindowInner* aWindow, const nsAString& aInternalId);

  // Id for internal identification
  nsString mInternalId;
  // Id for communicating with merchant side
  // mId is initialized to details.id if it exists
  // otherwise, mId has the same value as mInternalId.
  nsString mId;
  // Promise for "PaymentRequest::CanMakePayment"
  RefPtr<Promise> mResultPromise;
  // Promise for "PaymentRequest::Show"
  RefPtr<Promise> mAcceptPromise;
  // Promise for "PaymentRequest::Abort"
  RefPtr<Promise> mAbortPromise;
  // Resolve mAcceptPromise with mResponse if user accepts the request.
  RefPtr<PaymentResponse> mResponse;
  // The redacted shipping address.
  RefPtr<PaymentAddress> mShippingAddress;
  // The full shipping address to be used in the response upon payment.
  RefPtr<PaymentAddress> mFullShippingAddress;
  // Hold a reference to the document to allow unregistering the activity
  // observer.
  RefPtr<Document> mDocument;
  // It is populated when the user chooses a shipping option.
  nsString mShippingOption;

  Nullable<PaymentShippingType> mShippingType;

  // "true" when there is a pending updateWith() call to update the payment
  // request and "false" otherwise.
  bool mUpdating;

  // Whether shipping was requested. This models [[options]].requestShipping,
  // but we don't actually store the full [[options]] internal slot.
  bool mRequestShipping;

  // The error is set in AbortUpdate(). The value is NS_OK by default.
  nsresult mUpdateError;

  enum { eUnknown, eCreated, eInteractive, eClosed } mState;

  PaymentRequestChild* mIPC;
};
}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_PaymentRequest_h