/work/obj-fuzz/dist/include/mozilla/dom/PaymentRequest.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #ifndef mozilla_dom_PaymentRequest_h |
8 | | #define mozilla_dom_PaymentRequest_h |
9 | | |
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" |
18 | | |
19 | | namespace mozilla { |
20 | | namespace dom { |
21 | | |
22 | | class EventHandlerNonNull; |
23 | | class PaymentAddress; |
24 | | class PaymentRequestChild; |
25 | | class PaymentResponse; |
26 | | |
27 | | class PaymentRequest final |
28 | | : public DOMEventTargetHelper |
29 | | , public PromiseNativeHandler |
30 | | , public nsIDocumentActivity |
31 | | { |
32 | | public: |
33 | | NS_DECL_ISUPPORTS_INHERITED |
34 | | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(PaymentRequest, |
35 | | DOMEventTargetHelper) |
36 | | NS_DECL_NSIDOCUMENTACTIVITY |
37 | | |
38 | | virtual JSObject* WrapObject(JSContext* aCx, |
39 | | JS::Handle<JSObject*> aGivenProto) override; |
40 | | |
41 | | static already_AddRefed<PaymentRequest> CreatePaymentRequest( |
42 | | nsPIDOMWindowInner* aWindow, |
43 | | nsresult& aRv); |
44 | | |
45 | | static bool PrefEnabled(JSContext* aCx, JSObject* aObj); |
46 | | |
47 | | static nsresult IsValidStandardizedPMI(const nsAString& aIdentifier, |
48 | | nsAString& aErrorMsg); |
49 | | |
50 | | static nsresult IsValidPaymentMethodIdentifier(const nsAString& aIdentifier, |
51 | | nsAString& aErrorMsg); |
52 | | |
53 | | static nsresult IsValidMethodData( |
54 | | JSContext* aCx, |
55 | | const Sequence<PaymentMethodData>& aMethodData, |
56 | | nsAString& aErrorMsg); |
57 | | |
58 | | static nsresult IsValidNumber(const nsAString& aItem, |
59 | | const nsAString& aStr, |
60 | | nsAString& aErrorMsg); |
61 | | static nsresult IsNonNegativeNumber(const nsAString& aItem, |
62 | | const nsAString& aStr, |
63 | | nsAString& aErrorMsg); |
64 | | |
65 | | static nsresult IsValidCurrencyAmount(const nsAString& aItem, |
66 | | const PaymentCurrencyAmount& aAmount, |
67 | | const bool aIsTotalItem, |
68 | | nsAString& aErrorMsg); |
69 | | |
70 | | static nsresult IsValidCurrency(const nsAString& aItem, |
71 | | const nsAString& aCurrency, |
72 | | nsAString& aErrorMsg); |
73 | | |
74 | | static nsresult IsValidDetailsInit(const PaymentDetailsInit& aDetails, |
75 | | const bool aRequestShipping, |
76 | | nsAString& aErrorMsg); |
77 | | |
78 | | static nsresult IsValidDetailsUpdate(const PaymentDetailsUpdate& aDetails, |
79 | | const bool aRequestShipping); |
80 | | |
81 | | static nsresult IsValidDetailsBase(const PaymentDetailsBase& aDetails, |
82 | | const bool aRequestShipping, |
83 | | nsAString& aErrorMsg); |
84 | | |
85 | | static already_AddRefed<PaymentRequest> Constructor( |
86 | | const GlobalObject& aGlobal, |
87 | | const Sequence<PaymentMethodData>& aMethodData, |
88 | | const PaymentDetailsInit& aDetails, |
89 | | const PaymentOptions& aOptions, |
90 | | ErrorResult& aRv); |
91 | | |
92 | | already_AddRefed<Promise> CanMakePayment(ErrorResult& aRv); |
93 | | void RespondCanMakePayment(bool aResult); |
94 | | |
95 | | already_AddRefed<Promise> Show( |
96 | | const Optional<OwningNonNull<Promise>>& detailsPromise, |
97 | | ErrorResult& aRv); |
98 | | void RespondShowPayment(const nsAString& aMethodName, |
99 | | const nsAString& aDetails, |
100 | | const nsAString& aPayerName, |
101 | | const nsAString& aPayerEmail, |
102 | | const nsAString& aPayerPhone, |
103 | | nsresult aRv); |
104 | | void RejectShowPayment(nsresult aRejectReason); |
105 | | void RespondComplete(); |
106 | | |
107 | | already_AddRefed<Promise> Abort(ErrorResult& aRv); |
108 | | void RespondAbortPayment(bool aResult); |
109 | | |
110 | | nsresult RetryPayment(JSContext* aCx, const PaymentValidationErrors& aErrors); |
111 | | |
112 | | void GetId(nsAString& aRetVal) const; |
113 | | void GetInternalId(nsAString& aRetVal); |
114 | | void SetId(const nsAString& aId); |
115 | | |
116 | | bool Equals(const nsAString& aInternalId) const; |
117 | | |
118 | | bool ReadyForUpdate(); |
119 | | bool IsUpdating() const { return mUpdating; } |
120 | | void SetUpdating(bool aUpdating); |
121 | | |
122 | | already_AddRefed<PaymentResponse> GetResponse() const; |
123 | | |
124 | | already_AddRefed<PaymentAddress> GetShippingAddress() const; |
125 | | // Update mShippingAddress and fire shippingaddresschange event |
126 | | nsresult UpdateShippingAddress(const nsAString& aCountry, |
127 | | const nsTArray<nsString>& aAddressLine, |
128 | | const nsAString& aRegion, |
129 | | const nsAString& aCity, |
130 | | const nsAString& aDependentLocality, |
131 | | const nsAString& aPostalCode, |
132 | | const nsAString& aSortingCode, |
133 | | const nsAString& aOrganization, |
134 | | const nsAString& aRecipient, |
135 | | const nsAString& aPhone); |
136 | | |
137 | | void SetShippingOption(const nsAString& aShippingOption); |
138 | | void GetShippingOption(nsAString& aRetVal) const; |
139 | | void GetOptions(PaymentOptions& aRetVal) const; |
140 | | void SetOptions(const PaymentOptions& aOptions); |
141 | | nsresult UpdateShippingOption(const nsAString& aShippingOption); |
142 | | |
143 | | nsresult UpdatePayment(JSContext* aCx, |
144 | | const PaymentDetailsUpdate& aDetails, |
145 | | bool aDeferredShow); |
146 | | void AbortUpdate(nsresult aRv, bool aDeferredShow); |
147 | | |
148 | | void SetShippingType(const Nullable<PaymentShippingType>& aShippingType); |
149 | | Nullable<PaymentShippingType> GetShippingType() const; |
150 | | |
151 | | inline void ShippingWasRequested() { mRequestShipping = true; } |
152 | | |
153 | | void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override; |
154 | | void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override; |
155 | | |
156 | | IMPL_EVENT_HANDLER(merchantvalidation); |
157 | | IMPL_EVENT_HANDLER(shippingaddresschange); |
158 | | IMPL_EVENT_HANDLER(shippingoptionchange); |
159 | | IMPL_EVENT_HANDLER(paymentmethodchange); |
160 | | |
161 | 0 | void SetIPC(PaymentRequestChild* aChild) { mIPC = aChild; } |
162 | | |
163 | | PaymentRequestChild* GetIPC() const { return mIPC; } |
164 | | |
165 | | private: |
166 | | PaymentOptions mOptions; |
167 | | |
168 | | protected: |
169 | | ~PaymentRequest(); |
170 | | |
171 | | void RegisterActivityObserver(); |
172 | | void UnregisterActivityObserver(); |
173 | | |
174 | | nsresult DispatchUpdateEvent(const nsAString& aType); |
175 | | |
176 | | nsresult DispatchMerchantValidationEvent(const nsAString& aType); |
177 | | |
178 | | PaymentRequest(nsPIDOMWindowInner* aWindow, const nsAString& aInternalId); |
179 | | |
180 | | // Id for internal identification |
181 | | nsString mInternalId; |
182 | | // Id for communicating with merchant side |
183 | | // mId is initialized to details.id if it exists |
184 | | // otherwise, mId has the same value as mInternalId. |
185 | | nsString mId; |
186 | | // Promise for "PaymentRequest::CanMakePayment" |
187 | | RefPtr<Promise> mResultPromise; |
188 | | // Promise for "PaymentRequest::Show" |
189 | | RefPtr<Promise> mAcceptPromise; |
190 | | // Promise for "PaymentRequest::Abort" |
191 | | RefPtr<Promise> mAbortPromise; |
192 | | // Resolve mAcceptPromise with mResponse if user accepts the request. |
193 | | RefPtr<PaymentResponse> mResponse; |
194 | | // The redacted shipping address. |
195 | | RefPtr<PaymentAddress> mShippingAddress; |
196 | | // The full shipping address to be used in the response upon payment. |
197 | | RefPtr<PaymentAddress> mFullShippingAddress; |
198 | | // It is populated when the user chooses a shipping option. |
199 | | nsString mShippingOption; |
200 | | |
201 | | Nullable<PaymentShippingType> mShippingType; |
202 | | |
203 | | // "true" when there is a pending updateWith() call to update the payment |
204 | | // request and "false" otherwise. |
205 | | bool mUpdating; |
206 | | |
207 | | // Whether shipping was requested. This models [[options]].requestShipping, |
208 | | // but we don't actually store the full [[options]] internal slot. |
209 | | bool mRequestShipping; |
210 | | |
211 | | // True if the user passed a promise to show, causing us to defer telling the |
212 | | // front end about it. |
213 | | bool mDeferredShow; |
214 | | |
215 | | // The error is set in AbortUpdate(). The value is NS_OK by default. |
216 | | nsresult mUpdateError; |
217 | | |
218 | | enum |
219 | | { |
220 | | eUnknown, |
221 | | eCreated, |
222 | | eInteractive, |
223 | | eClosed |
224 | | } mState; |
225 | | |
226 | | PaymentRequestChild* mIPC; |
227 | | }; |
228 | | } // namespace dom |
229 | | } // namespace mozilla |
230 | | |
231 | | #endif // mozilla_dom_PaymentRequest_h |