Bug 1883853 [wpt PR 44937] - [wdspec] fix test_set_permission_origin_unknown, a=testonly
[gecko.git] / dom / payments / PaymentResponse.h
blobaeffe4fac4fc0ce4d51f002ecd3054208e42388e
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_PaymentResponse_h
8 #define mozilla_dom_PaymentResponse_h
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/dom/PaymentResponseBinding.h" // PaymentComplete
12 #include "nsPIDOMWindow.h"
13 #include "nsITimer.h"
15 namespace mozilla::dom {
17 class PaymentAddress;
18 class PaymentRequest;
19 struct PaymentValidationErrors;
20 class Promise;
22 class GeneralData final {
23 public:
24 GeneralData() = default;
25 ~GeneralData() = default;
26 nsString data;
29 class BasicCardData final {
30 public:
31 struct Address {
32 nsString country;
33 CopyableTArray<nsString> addressLine;
34 nsString region;
35 nsString regionCode;
36 nsString city;
37 nsString dependentLocality;
38 nsString postalCode;
39 nsString sortingCode;
40 nsString organization;
41 nsString recipient;
42 nsString phone;
44 BasicCardData() = default;
45 ~BasicCardData() = default;
47 nsString cardholderName;
48 nsString cardNumber;
49 nsString expiryMonth;
50 nsString expiryYear;
51 nsString cardSecurityCode;
52 Address billingAddress;
55 class ResponseData final {
56 public:
57 enum Type { Unknown = 0, GeneralResponse = 1, BasicCardResponse };
58 ResponseData() : mType(ResponseData::Unknown) {}
59 explicit ResponseData(const GeneralData& aGeneralData)
60 : mType(GeneralResponse), mGeneralData(aGeneralData) {}
61 explicit ResponseData(const BasicCardData& aBasicCardData)
62 : mType(BasicCardResponse), mBasicCardData(aBasicCardData) {}
63 ResponseData& operator=(const GeneralData& aGeneralData) {
64 mType = GeneralResponse;
65 mGeneralData = aGeneralData;
66 mBasicCardData = BasicCardData();
67 return *this;
69 ResponseData& operator=(const BasicCardData& aBasicCardData) {
70 mType = BasicCardResponse;
71 mGeneralData = GeneralData();
72 mBasicCardData = aBasicCardData;
73 return *this;
75 ~ResponseData() = default;
77 const Type& type() const { return mType; }
78 const GeneralData& generalData() const { return mGeneralData; }
79 const BasicCardData& basicCardData() const { return mBasicCardData; }
81 private:
82 Type mType;
83 GeneralData mGeneralData;
84 BasicCardData mBasicCardData;
87 class PaymentResponse final : public DOMEventTargetHelper,
88 public nsITimerCallback {
89 public:
90 NS_DECL_ISUPPORTS_INHERITED
92 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PaymentResponse,
93 DOMEventTargetHelper)
95 NS_IMETHOD Notify(nsITimer* aTimer) override;
97 PaymentResponse(nsPIDOMWindowInner* aWindow, PaymentRequest* aRequest,
98 const nsAString& aRequestId, const nsAString& aMethodName,
99 const nsAString& aShippingOption,
100 PaymentAddress* aShippingAddress,
101 const ResponseData& aDetails, const nsAString& aPayerName,
102 const nsAString& aPayerEmail, const nsAString& aPayerPhone);
104 virtual JSObject* WrapObject(JSContext* aCx,
105 JS::Handle<JSObject*> aGivenProto) override;
107 void GetRequestId(nsString& aRetVal) const;
109 void GetMethodName(nsString& aRetVal) const;
111 void GetDetails(JSContext* cx, JS::MutableHandle<JSObject*> aRetVal) const;
113 already_AddRefed<PaymentAddress> GetShippingAddress() const;
115 void GetShippingOption(nsString& aRetVal) const;
117 void GetPayerName(nsString& aRetVal) const;
119 void GetPayerEmail(nsString& aRetVal) const;
121 void GetPayerPhone(nsString& aRetVal) const;
123 // Return a raw pointer here to avoid refcounting, but make sure it's safe
124 // (the object should be kept alive by the callee).
125 already_AddRefed<Promise> Complete(PaymentComplete result, ErrorResult& aRv);
127 void RespondComplete();
129 IMPL_EVENT_HANDLER(payerdetailchange);
131 nsresult UpdatePayerDetail(const nsAString& aPayerName,
132 const nsAString& aPayerEmail,
133 const nsAString& aPayerPhone);
135 already_AddRefed<Promise> Retry(JSContext* aCx,
136 const PaymentValidationErrors& errorField,
137 ErrorResult& aRv);
139 void RespondRetry(const nsAString& aMethodName,
140 const nsAString& aShippingOption,
141 PaymentAddress* aShippingAddress,
142 const ResponseData& aDetails, const nsAString& aPayerName,
143 const nsAString& aPayerEmail, const nsAString& aPayerPhone);
144 void RejectRetry(ErrorResult&& aRejectReason);
146 protected:
147 ~PaymentResponse();
149 void ValidatePaymentValidationErrors(const PaymentValidationErrors& aErrors,
150 ErrorResult& aRv);
152 void ConvertPaymentMethodErrors(JSContext* aCx,
153 const PaymentValidationErrors& aErrors,
154 ErrorResult& aRv) const;
156 nsresult DispatchUpdateEvent(const nsAString& aType);
158 private:
159 bool mCompleteCalled;
160 PaymentRequest* mRequest;
161 nsString mRequestId;
162 nsString mMethodName;
163 ResponseData mDetails;
164 nsString mShippingOption;
165 nsString mPayerName;
166 nsString mPayerEmail;
167 nsString mPayerPhone;
168 RefPtr<PaymentAddress> mShippingAddress;
169 // Promise for "PaymentResponse::Complete"
170 RefPtr<Promise> mPromise;
171 // Timer for timing out if the page doesn't call
172 // complete()
173 nsCOMPtr<nsITimer> mTimer;
174 // Promise for "PaymentResponse::Retry"
175 RefPtr<Promise> mRetryPromise;
178 } // namespace mozilla::dom
180 #endif // mozilla_dom_PaymentResponse_h