Bug 1472338: part 2) Change `clipboard.readText()` to read from the clipboard asynchr...
[gecko.git] / dom / payments / PaymentResponse.h
blob8f717d7349523b7a99b91df722edf9e846b80fc5
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 {
16 namespace dom {
18 class PaymentAddress;
19 class PaymentRequest;
20 struct PaymentValidationErrors;
21 class Promise;
23 class GeneralData final {
24 public:
25 GeneralData() = default;
26 ~GeneralData() = default;
27 nsString data;
30 class BasicCardData final {
31 public:
32 struct Address {
33 nsString country;
34 CopyableTArray<nsString> addressLine;
35 nsString region;
36 nsString regionCode;
37 nsString city;
38 nsString dependentLocality;
39 nsString postalCode;
40 nsString sortingCode;
41 nsString organization;
42 nsString recipient;
43 nsString phone;
45 BasicCardData() = default;
46 ~BasicCardData() = default;
48 nsString cardholderName;
49 nsString cardNumber;
50 nsString expiryMonth;
51 nsString expiryYear;
52 nsString cardSecurityCode;
53 Address billingAddress;
56 class ResponseData final {
57 public:
58 enum Type { Unknown = 0, GeneralResponse = 1, BasicCardResponse };
59 ResponseData() : mType(ResponseData::Unknown) {}
60 explicit ResponseData(const GeneralData& aGeneralData)
61 : mType(GeneralResponse), mGeneralData(aGeneralData) {}
62 explicit ResponseData(const BasicCardData& aBasicCardData)
63 : mType(BasicCardResponse), mBasicCardData(aBasicCardData) {}
64 ResponseData& operator=(const GeneralData& aGeneralData) {
65 mType = GeneralResponse;
66 mGeneralData = aGeneralData;
67 mBasicCardData = BasicCardData();
68 return *this;
70 ResponseData& operator=(const BasicCardData& aBasicCardData) {
71 mType = BasicCardResponse;
72 mGeneralData = GeneralData();
73 mBasicCardData = aBasicCardData;
74 return *this;
76 ~ResponseData() = default;
78 const Type& type() const { return mType; }
79 const GeneralData& generalData() const { return mGeneralData; }
80 const BasicCardData& basicCardData() const { return mBasicCardData; }
82 private:
83 Type mType;
84 GeneralData mGeneralData;
85 BasicCardData mBasicCardData;
88 class PaymentResponse final : public DOMEventTargetHelper,
89 public nsITimerCallback {
90 public:
91 NS_DECL_ISUPPORTS_INHERITED
93 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PaymentResponse,
94 DOMEventTargetHelper)
96 NS_IMETHOD Notify(nsITimer* aTimer) override;
98 PaymentResponse(nsPIDOMWindowInner* aWindow, PaymentRequest* aRequest,
99 const nsAString& aRequestId, const nsAString& aMethodName,
100 const nsAString& aShippingOption,
101 PaymentAddress* aShippingAddress,
102 const ResponseData& aDetails, const nsAString& aPayerName,
103 const nsAString& aPayerEmail, const nsAString& aPayerPhone);
105 virtual JSObject* WrapObject(JSContext* aCx,
106 JS::Handle<JSObject*> aGivenProto) override;
108 void GetRequestId(nsString& aRetVal) const;
110 void GetMethodName(nsString& aRetVal) const;
112 void GetDetails(JSContext* cx, JS::MutableHandle<JSObject*> aRetVal) const;
114 already_AddRefed<PaymentAddress> GetShippingAddress() const;
116 void GetShippingOption(nsString& aRetVal) const;
118 void GetPayerName(nsString& aRetVal) const;
120 void GetPayerEmail(nsString& aRetVal) const;
122 void GetPayerPhone(nsString& aRetVal) const;
124 // Return a raw pointer here to avoid refcounting, but make sure it's safe
125 // (the object should be kept alive by the callee).
126 already_AddRefed<Promise> Complete(PaymentComplete result, ErrorResult& aRv);
128 void RespondComplete();
130 IMPL_EVENT_HANDLER(payerdetailchange);
132 nsresult UpdatePayerDetail(const nsAString& aPayerName,
133 const nsAString& aPayerEmail,
134 const nsAString& aPayerPhone);
136 already_AddRefed<Promise> Retry(JSContext* aCx,
137 const PaymentValidationErrors& errorField,
138 ErrorResult& aRv);
140 void RespondRetry(const nsAString& aMethodName,
141 const nsAString& aShippingOption,
142 PaymentAddress* aShippingAddress,
143 const ResponseData& aDetails, const nsAString& aPayerName,
144 const nsAString& aPayerEmail, const nsAString& aPayerPhone);
145 void RejectRetry(ErrorResult&& aRejectReason);
147 protected:
148 ~PaymentResponse();
150 void ValidatePaymentValidationErrors(const PaymentValidationErrors& aErrors,
151 ErrorResult& aRv);
153 void ConvertPaymentMethodErrors(JSContext* aCx,
154 const PaymentValidationErrors& aErrors,
155 ErrorResult& aRv) const;
157 nsresult DispatchUpdateEvent(const nsAString& aType);
159 private:
160 bool mCompleteCalled;
161 PaymentRequest* mRequest;
162 nsString mRequestId;
163 nsString mMethodName;
164 ResponseData mDetails;
165 nsString mShippingOption;
166 nsString mPayerName;
167 nsString mPayerEmail;
168 nsString mPayerPhone;
169 RefPtr<PaymentAddress> mShippingAddress;
170 // Promise for "PaymentResponse::Complete"
171 RefPtr<Promise> mPromise;
172 // Timer for timing out if the page doesn't call
173 // complete()
174 nsCOMPtr<nsITimer> mTimer;
175 // Promise for "PaymentResponse::Retry"
176 RefPtr<Promise> mRetryPromise;
179 } // namespace dom
180 } // namespace mozilla
182 #endif // mozilla_dom_PaymentResponse_h