Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / dom / payments / PaymentRequest.h
blob6bece3f3d8a976d3b06bdb00b4eb2ed529c7c133
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_PaymentRequest_h
8 #define mozilla_dom_PaymentRequest_h
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/dom/PaymentRequestBinding.h"
12 #include "mozilla/dom/Promise.h"
13 #include "mozilla/dom/PromiseNativeHandler.h"
14 #include "mozilla/ErrorResult.h"
15 #include "nsIDocumentActivity.h"
16 #include "nsWrapperCache.h"
17 #include "PaymentRequestUpdateEvent.h"
19 namespace mozilla {
20 namespace dom {
22 class PaymentAddress;
23 class PaymentRequestChild;
24 class PaymentResponse;
25 class ResponseData;
27 class GeneralDetails final {
28 public:
29 GeneralDetails() = default;
30 ~GeneralDetails() = default;
31 nsString details;
34 class BasicCardDetails final {
35 public:
36 struct Address {
37 nsString country;
38 CopyableTArray<nsString> addressLine;
39 nsString region;
40 nsString regionCode;
41 nsString city;
42 nsString dependentLocality;
43 nsString postalCode;
44 nsString sortingCode;
45 nsString organization;
46 nsString recipient;
47 nsString phone;
49 BasicCardDetails() = default;
50 ~BasicCardDetails() = default;
52 Address billingAddress;
55 class ChangeDetails final {
56 public:
57 enum Type { Unknown = 0, GeneralMethodDetails = 1, BasicCardMethodDetails };
58 ChangeDetails() : mType(ChangeDetails::Unknown) {}
59 explicit ChangeDetails(const GeneralDetails& aGeneralDetails)
60 : mType(GeneralMethodDetails), mGeneralDetails(aGeneralDetails) {}
61 explicit ChangeDetails(const BasicCardDetails& aBasicCardDetails)
62 : mType(BasicCardMethodDetails), mBasicCardDetails(aBasicCardDetails) {}
63 ChangeDetails& operator=(const GeneralDetails& aGeneralDetails) {
64 mType = GeneralMethodDetails;
65 mGeneralDetails = aGeneralDetails;
66 mBasicCardDetails = BasicCardDetails();
67 return *this;
69 ChangeDetails& operator=(const BasicCardDetails& aBasicCardDetails) {
70 mType = BasicCardMethodDetails;
71 mGeneralDetails = GeneralDetails();
72 mBasicCardDetails = aBasicCardDetails;
73 return *this;
75 ~ChangeDetails() = default;
77 const Type& type() const { return mType; }
78 const GeneralDetails& generalDetails() const { return mGeneralDetails; }
79 const BasicCardDetails& basicCardDetails() const { return mBasicCardDetails; }
81 private:
82 Type mType;
83 GeneralDetails mGeneralDetails;
84 BasicCardDetails mBasicCardDetails;
87 class PaymentRequest final : public DOMEventTargetHelper,
88 public PromiseNativeHandler,
89 public nsIDocumentActivity {
90 public:
91 NS_DECL_ISUPPORTS_INHERITED
92 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PaymentRequest,
93 DOMEventTargetHelper)
94 NS_DECL_NSIDOCUMENTACTIVITY
96 virtual JSObject* WrapObject(JSContext* aCx,
97 JS::Handle<JSObject*> aGivenProto) override;
99 static already_AddRefed<PaymentRequest> CreatePaymentRequest(
100 nsPIDOMWindowInner* aWindow, ErrorResult& aRv);
102 static bool PrefEnabled(JSContext* aCx, JSObject* aObj);
104 // Parameter validation methods
105 static void IsValidStandardizedPMI(const nsAString& aIdentifier,
106 ErrorResult& aRv);
108 static void IsValidPaymentMethodIdentifier(const nsAString& aIdentifier,
109 ErrorResult& aRv);
111 static void IsValidMethodData(JSContext* aCx,
112 const Sequence<PaymentMethodData>& aMethodData,
113 ErrorResult& aRv);
115 static void IsValidNumber(const nsAString& aItem, const nsAString& aStr,
116 ErrorResult& aRv);
118 static void IsNonNegativeNumber(const nsAString& aItem, const nsAString& aStr,
119 ErrorResult& aRv);
121 static void IsValidCurrencyAmount(const nsAString& aItem,
122 const PaymentCurrencyAmount& aAmount,
123 const bool aIsTotalItem, ErrorResult& aRv);
125 static void IsValidCurrency(const nsAString& aItem,
126 const nsAString& aCurrency, ErrorResult& aRv);
128 static void IsValidDetailsInit(const PaymentDetailsInit& aDetails,
129 const bool aRequestShipping, ErrorResult& aRv);
131 static void IsValidDetailsUpdate(const PaymentDetailsUpdate& aDetails,
132 const bool aRequestShipping,
133 ErrorResult& aRv);
135 static void IsValidDetailsBase(const PaymentDetailsBase& aDetails,
136 const bool aRequestShipping, ErrorResult& aRv);
138 // Webidl implementation
139 static already_AddRefed<PaymentRequest> Constructor(
140 const GlobalObject& aGlobal,
141 const Sequence<PaymentMethodData>& aMethodData,
142 const PaymentDetailsInit& aDetails, const PaymentOptions& aOptions,
143 ErrorResult& aRv);
145 already_AddRefed<Promise> CanMakePayment(ErrorResult& aRv);
146 void RespondCanMakePayment(bool aResult);
148 already_AddRefed<Promise> Show(
149 const Optional<OwningNonNull<Promise>>& detailsPromise, ErrorResult& aRv);
150 void RespondShowPayment(const nsAString& aMethodName,
151 const ResponseData& aData,
152 const nsAString& aPayerName,
153 const nsAString& aPayerEmail,
154 const nsAString& aPayerPhone, ErrorResult&& aResult);
155 void RejectShowPayment(ErrorResult&& aRejectReason);
156 void RespondComplete();
158 already_AddRefed<Promise> Abort(ErrorResult& aRv);
159 void RespondAbortPayment(bool aResult);
161 void RetryPayment(JSContext* aCx, const PaymentValidationErrors& aErrors,
162 ErrorResult& aRv);
164 void GetId(nsAString& aRetVal) const;
165 void GetInternalId(nsAString& aRetVal);
166 void SetId(const nsAString& aId);
168 bool Equals(const nsAString& aInternalId) const;
170 bool ReadyForUpdate();
171 bool IsUpdating() const { return mUpdating; }
172 void SetUpdating(bool aUpdating);
174 already_AddRefed<PaymentResponse> GetResponse() const;
176 already_AddRefed<PaymentAddress> GetShippingAddress() const;
177 // Update mShippingAddress and fire shippingaddresschange event
178 nsresult UpdateShippingAddress(
179 const nsAString& aCountry, const nsTArray<nsString>& aAddressLine,
180 const nsAString& aRegion, const nsAString& aRegionCode,
181 const nsAString& aCity, const nsAString& aDependentLocality,
182 const nsAString& aPostalCode, const nsAString& aSortingCode,
183 const nsAString& aOrganization, const nsAString& aRecipient,
184 const nsAString& aPhone);
186 void SetShippingOption(const nsAString& aShippingOption);
187 void GetShippingOption(nsAString& aRetVal) const;
188 void GetOptions(PaymentOptions& aRetVal) const;
189 void SetOptions(const PaymentOptions& aOptions);
190 nsresult UpdateShippingOption(const nsAString& aShippingOption);
192 void UpdatePayment(JSContext* aCx, const PaymentDetailsUpdate& aDetails,
193 ErrorResult& aRv);
194 void AbortUpdate(ErrorResult& aReason);
196 void SetShippingType(const Nullable<PaymentShippingType>& aShippingType);
197 Nullable<PaymentShippingType> GetShippingType() const;
199 inline void ShippingWasRequested() { mRequestShipping = true; }
201 nsresult UpdatePaymentMethod(const nsAString& aMethodName,
202 const ChangeDetails& aMethodDetails);
204 void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
205 ErrorResult& aRv) override;
206 void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
207 ErrorResult& aRv) override;
209 bool InFullyActiveDocument();
211 IMPL_EVENT_HANDLER(merchantvalidation);
212 IMPL_EVENT_HANDLER(shippingaddresschange);
213 IMPL_EVENT_HANDLER(shippingoptionchange);
214 IMPL_EVENT_HANDLER(paymentmethodchange);
216 void SetIPC(PaymentRequestChild* aChild) { mIPC = aChild; }
218 PaymentRequestChild* GetIPC() const { return mIPC; }
220 private:
221 PaymentOptions mOptions;
223 protected:
224 ~PaymentRequest();
226 void RegisterActivityObserver();
227 void UnregisterActivityObserver();
229 nsresult DispatchUpdateEvent(const nsAString& aType);
231 nsresult DispatchMerchantValidationEvent(const nsAString& aType);
233 nsresult DispatchPaymentMethodChangeEvent(const nsAString& aMethodName,
234 const ChangeDetails& aMethodDatils);
236 PaymentRequest(nsPIDOMWindowInner* aWindow, const nsAString& aInternalId);
238 // Id for internal identification
239 nsString mInternalId;
240 // Id for communicating with merchant side
241 // mId is initialized to details.id if it exists
242 // otherwise, mId has the same value as mInternalId.
243 nsString mId;
244 // Promise for "PaymentRequest::CanMakePayment"
245 RefPtr<Promise> mResultPromise;
246 // Promise for "PaymentRequest::Show"
247 RefPtr<Promise> mAcceptPromise;
248 // Promise for "PaymentRequest::Abort"
249 RefPtr<Promise> mAbortPromise;
250 // Resolve mAcceptPromise with mResponse if user accepts the request.
251 RefPtr<PaymentResponse> mResponse;
252 // The redacted shipping address.
253 RefPtr<PaymentAddress> mShippingAddress;
254 // The full shipping address to be used in the response upon payment.
255 RefPtr<PaymentAddress> mFullShippingAddress;
256 // Hold a reference to the document to allow unregistering the activity
257 // observer.
258 RefPtr<Document> mDocument;
259 // It is populated when the user chooses a shipping option.
260 nsString mShippingOption;
262 Nullable<PaymentShippingType> mShippingType;
264 // "true" when there is a pending updateWith() call to update the payment
265 // request and "false" otherwise.
266 bool mUpdating;
268 // Whether shipping was requested. This models [[options]].requestShipping,
269 // but we don't actually store the full [[options]] internal slot.
270 bool mRequestShipping;
272 // The error is set in AbortUpdate(). The value is not-failed by default.
273 ErrorResult mUpdateError;
275 enum { eUnknown, eCreated, eInteractive, eClosed } mState;
277 PaymentRequestChild* mIPC;
279 } // namespace dom
280 } // namespace mozilla
282 #endif // mozilla_dom_PaymentRequest_h