/src/mozilla-central/dom/payments/PaymentRequestData.cpp
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 | | #include "mozilla/dom/PaymentRequestBinding.h" |
8 | | #include "nsArrayUtils.h" |
9 | | #include "nsIMutableArray.h" |
10 | | #include "nsISupportsPrimitives.h" |
11 | | #include "nsUnicharUtils.h" |
12 | | #include "PaymentRequestData.h" |
13 | | #include "PaymentRequestUtils.h" |
14 | | |
15 | | namespace mozilla { |
16 | | namespace dom { |
17 | | namespace payments { |
18 | | |
19 | | /* PaymentMethodData */ |
20 | | |
21 | | NS_IMPL_ISUPPORTS(PaymentMethodData, |
22 | | nsIPaymentMethodData) |
23 | | |
24 | | PaymentMethodData::PaymentMethodData(const nsAString& aSupportedMethods, |
25 | | const nsAString& aData) |
26 | | : mSupportedMethods(aSupportedMethods) |
27 | | , mData(aData) |
28 | 0 | { |
29 | 0 | } |
30 | | |
31 | | nsresult |
32 | | PaymentMethodData::Create(const IPCPaymentMethodData& aIPCMethodData, |
33 | | nsIPaymentMethodData** aMethodData) |
34 | 0 | { |
35 | 0 | NS_ENSURE_ARG_POINTER(aMethodData); |
36 | 0 | nsCOMPtr<nsIPaymentMethodData> methodData = |
37 | 0 | new PaymentMethodData(aIPCMethodData.supportedMethods(), aIPCMethodData.data()); |
38 | 0 | methodData.forget(aMethodData); |
39 | 0 | return NS_OK; |
40 | 0 | } |
41 | | |
42 | | NS_IMETHODIMP |
43 | | PaymentMethodData::GetSupportedMethods(nsAString& aSupportedMethods) |
44 | 0 | { |
45 | 0 | aSupportedMethods = mSupportedMethods; |
46 | 0 | return NS_OK; |
47 | 0 | } |
48 | | |
49 | | NS_IMETHODIMP |
50 | | PaymentMethodData::GetData(JSContext* aCx, JS::MutableHandleValue aData) |
51 | 0 | { |
52 | 0 | if (mData.IsEmpty()) { |
53 | 0 | aData.set(JS::NullValue()); |
54 | 0 | return NS_OK; |
55 | 0 | } |
56 | 0 | nsresult rv = DeserializeToJSValue(mData, aCx ,aData); |
57 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
58 | 0 | return rv; |
59 | 0 | } |
60 | 0 | return NS_OK; |
61 | 0 | } |
62 | | |
63 | | /* PaymentCurrencyAmount */ |
64 | | |
65 | | NS_IMPL_ISUPPORTS(PaymentCurrencyAmount, |
66 | | nsIPaymentCurrencyAmount) |
67 | | |
68 | | PaymentCurrencyAmount::PaymentCurrencyAmount(const nsAString& aCurrency, |
69 | | const nsAString& aValue) |
70 | | : mValue(aValue) |
71 | 0 | { |
72 | 0 | /* |
73 | 0 | * According to the spec |
74 | 0 | * https://w3c.github.io/payment-request/#validity-checkers |
75 | 0 | * Set amount.currency to the result of ASCII uppercasing amount.currency. |
76 | 0 | */ |
77 | 0 | ToUpperCase(aCurrency, mCurrency); |
78 | 0 | } |
79 | | |
80 | | nsresult |
81 | | PaymentCurrencyAmount::Create(const IPCPaymentCurrencyAmount& aIPCAmount, |
82 | | nsIPaymentCurrencyAmount** aAmount) |
83 | 0 | { |
84 | 0 | NS_ENSURE_ARG_POINTER(aAmount); |
85 | 0 | nsCOMPtr<nsIPaymentCurrencyAmount> amount = |
86 | 0 | new PaymentCurrencyAmount(aIPCAmount.currency(), aIPCAmount.value()); |
87 | 0 | amount.forget(aAmount); |
88 | 0 | return NS_OK; |
89 | 0 | } |
90 | | |
91 | | NS_IMETHODIMP |
92 | | PaymentCurrencyAmount::GetCurrency(nsAString& aCurrency) |
93 | 0 | { |
94 | 0 | aCurrency = mCurrency; |
95 | 0 | return NS_OK; |
96 | 0 | } |
97 | | |
98 | | NS_IMETHODIMP |
99 | | PaymentCurrencyAmount::GetValue(nsAString& aValue) |
100 | 0 | { |
101 | 0 | aValue = mValue; |
102 | 0 | return NS_OK; |
103 | 0 | } |
104 | | |
105 | | /* PaymentItem */ |
106 | | |
107 | | NS_IMPL_ISUPPORTS(PaymentItem, |
108 | | nsIPaymentItem) |
109 | | |
110 | | PaymentItem::PaymentItem(const nsAString& aLabel, |
111 | | nsIPaymentCurrencyAmount* aAmount, |
112 | | const bool aPending, |
113 | | const nsAString& aType) |
114 | | : mLabel(aLabel) |
115 | | , mAmount(aAmount) |
116 | | , mPending(aPending) |
117 | | , mType(aType) |
118 | 0 | { |
119 | 0 | } |
120 | | |
121 | | nsresult |
122 | | PaymentItem::Create(const IPCPaymentItem& aIPCItem, nsIPaymentItem** aItem) |
123 | 0 | { |
124 | 0 | NS_ENSURE_ARG_POINTER(aItem); |
125 | 0 | nsCOMPtr<nsIPaymentCurrencyAmount> amount; |
126 | 0 | nsresult rv = PaymentCurrencyAmount::Create(aIPCItem.amount(), |
127 | 0 | getter_AddRefs(amount)); |
128 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
129 | 0 | return rv; |
130 | 0 | } |
131 | 0 | nsCOMPtr<nsIPaymentItem> item = |
132 | 0 | new PaymentItem(aIPCItem.label(), amount, aIPCItem.pending(), aIPCItem.type()); |
133 | 0 | item.forget(aItem); |
134 | 0 | return NS_OK; |
135 | 0 | } |
136 | | |
137 | | NS_IMETHODIMP |
138 | | PaymentItem::GetLabel(nsAString& aLabel) |
139 | 0 | { |
140 | 0 | aLabel = mLabel; |
141 | 0 | return NS_OK; |
142 | 0 | } |
143 | | |
144 | | NS_IMETHODIMP |
145 | | PaymentItem::GetAmount(nsIPaymentCurrencyAmount** aAmount) |
146 | 0 | { |
147 | 0 | NS_ENSURE_ARG_POINTER(aAmount); |
148 | 0 | MOZ_ASSERT(mAmount); |
149 | 0 | nsCOMPtr<nsIPaymentCurrencyAmount> amount = mAmount; |
150 | 0 | amount.forget(aAmount); |
151 | 0 | return NS_OK; |
152 | 0 | } |
153 | | |
154 | | NS_IMETHODIMP |
155 | | PaymentItem::GetPending(bool* aPending) |
156 | 0 | { |
157 | 0 | NS_ENSURE_ARG_POINTER(aPending); |
158 | 0 | *aPending = mPending; |
159 | 0 | return NS_OK; |
160 | 0 | } |
161 | | |
162 | | NS_IMETHODIMP |
163 | | PaymentItem::GetType(nsAString& aType) |
164 | 0 | { |
165 | 0 | aType = mType; |
166 | 0 | return NS_OK; |
167 | 0 | } |
168 | | |
169 | | /* PaymentDetailsModifier */ |
170 | | |
171 | | NS_IMPL_ISUPPORTS(PaymentDetailsModifier, |
172 | | nsIPaymentDetailsModifier) |
173 | | |
174 | | PaymentDetailsModifier::PaymentDetailsModifier(const nsAString& aSupportedMethods, |
175 | | nsIPaymentItem* aTotal, |
176 | | nsIArray* aAdditionalDisplayItems, |
177 | | const nsAString& aData) |
178 | | : mSupportedMethods(aSupportedMethods) |
179 | | , mTotal(aTotal) |
180 | | , mAdditionalDisplayItems(aAdditionalDisplayItems) |
181 | | , mData(aData) |
182 | 0 | { |
183 | 0 | } |
184 | | |
185 | | nsresult |
186 | | PaymentDetailsModifier::Create(const IPCPaymentDetailsModifier& aIPCModifier, |
187 | | nsIPaymentDetailsModifier** aModifier) |
188 | 0 | { |
189 | 0 | NS_ENSURE_ARG_POINTER(aModifier); |
190 | 0 | nsCOMPtr<nsIPaymentItem> total; |
191 | 0 | nsresult rv = PaymentItem::Create(aIPCModifier.total(), getter_AddRefs(total)); |
192 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
193 | 0 | return rv; |
194 | 0 | } |
195 | 0 | |
196 | 0 | nsCOMPtr<nsIArray> displayItems; |
197 | 0 | if (aIPCModifier.additionalDisplayItemsPassed()) { |
198 | 0 | nsCOMPtr<nsIMutableArray> items = do_CreateInstance(NS_ARRAY_CONTRACTID); |
199 | 0 | MOZ_ASSERT(items); |
200 | 0 | for (const IPCPaymentItem& item : aIPCModifier.additionalDisplayItems()) { |
201 | 0 | nsCOMPtr<nsIPaymentItem> additionalItem; |
202 | 0 | rv = PaymentItem::Create(item, getter_AddRefs(additionalItem)); |
203 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
204 | 0 | return rv; |
205 | 0 | } |
206 | 0 | rv = items->AppendElement(additionalItem); |
207 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
208 | 0 | return rv; |
209 | 0 | } |
210 | 0 | } |
211 | 0 | displayItems = items.forget(); |
212 | 0 | } |
213 | 0 | nsCOMPtr<nsIPaymentDetailsModifier> modifier = |
214 | 0 | new PaymentDetailsModifier(aIPCModifier.supportedMethods(), |
215 | 0 | total, |
216 | 0 | displayItems, |
217 | 0 | aIPCModifier.data()); |
218 | 0 | modifier.forget(aModifier); |
219 | 0 | return NS_OK; |
220 | 0 | } |
221 | | |
222 | | NS_IMETHODIMP |
223 | | PaymentDetailsModifier::GetSupportedMethods(nsAString& aSupportedMethods) |
224 | 0 | { |
225 | 0 | aSupportedMethods = mSupportedMethods; |
226 | 0 | return NS_OK; |
227 | 0 | } |
228 | | |
229 | | NS_IMETHODIMP |
230 | | PaymentDetailsModifier::GetTotal(nsIPaymentItem** aTotal) |
231 | 0 | { |
232 | 0 | NS_ENSURE_ARG_POINTER(aTotal); |
233 | 0 | MOZ_ASSERT(mTotal); |
234 | 0 | nsCOMPtr<nsIPaymentItem> total = mTotal; |
235 | 0 | total.forget(aTotal); |
236 | 0 | return NS_OK; |
237 | 0 | } |
238 | | |
239 | | NS_IMETHODIMP |
240 | | PaymentDetailsModifier::GetAdditionalDisplayItems(nsIArray** aAdditionalDisplayItems) |
241 | 0 | { |
242 | 0 | NS_ENSURE_ARG_POINTER(aAdditionalDisplayItems); |
243 | 0 | nsCOMPtr<nsIArray> additionalItems = mAdditionalDisplayItems; |
244 | 0 | additionalItems.forget(aAdditionalDisplayItems); |
245 | 0 | return NS_OK; |
246 | 0 | } |
247 | | |
248 | | NS_IMETHODIMP |
249 | | PaymentDetailsModifier::GetData(JSContext* aCx, JS::MutableHandleValue aData) |
250 | 0 | { |
251 | 0 | if (mData.IsEmpty()) { |
252 | 0 | aData.set(JS::NullValue()); |
253 | 0 | return NS_OK; |
254 | 0 | } |
255 | 0 | nsresult rv = DeserializeToJSValue(mData, aCx ,aData); |
256 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
257 | 0 | return rv; |
258 | 0 | } |
259 | 0 | return NS_OK; |
260 | 0 | } |
261 | | |
262 | | /* PaymentShippingOption */ |
263 | | |
264 | | NS_IMPL_ISUPPORTS(PaymentShippingOption, |
265 | | nsIPaymentShippingOption) |
266 | | |
267 | | PaymentShippingOption::PaymentShippingOption(const nsAString& aId, |
268 | | const nsAString& aLabel, |
269 | | nsIPaymentCurrencyAmount* aAmount, |
270 | | const bool aSelected) |
271 | | : mId(aId) |
272 | | , mLabel(aLabel) |
273 | | , mAmount(aAmount) |
274 | | , mSelected(aSelected) |
275 | 0 | { |
276 | 0 | } |
277 | | |
278 | | nsresult |
279 | | PaymentShippingOption::Create(const IPCPaymentShippingOption& aIPCOption, |
280 | | nsIPaymentShippingOption** aOption) |
281 | 0 | { |
282 | 0 | NS_ENSURE_ARG_POINTER(aOption); |
283 | 0 | nsCOMPtr<nsIPaymentCurrencyAmount> amount; |
284 | 0 | nsresult rv = PaymentCurrencyAmount::Create(aIPCOption.amount(), getter_AddRefs(amount)); |
285 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
286 | 0 | return rv; |
287 | 0 | } |
288 | 0 | nsCOMPtr<nsIPaymentShippingOption> option = |
289 | 0 | new PaymentShippingOption(aIPCOption.id(), aIPCOption.label(), amount, aIPCOption.selected()); |
290 | 0 | option.forget(aOption); |
291 | 0 | return NS_OK; |
292 | 0 | } |
293 | | |
294 | | NS_IMETHODIMP |
295 | | PaymentShippingOption::GetId(nsAString& aId) |
296 | 0 | { |
297 | 0 | aId = mId; |
298 | 0 | return NS_OK; |
299 | 0 | } |
300 | | |
301 | | NS_IMETHODIMP |
302 | | PaymentShippingOption::GetLabel(nsAString& aLabel) |
303 | 0 | { |
304 | 0 | aLabel = mLabel; |
305 | 0 | return NS_OK; |
306 | 0 | } |
307 | | |
308 | | NS_IMETHODIMP |
309 | | PaymentShippingOption::GetAmount(nsIPaymentCurrencyAmount** aAmount) |
310 | 0 | { |
311 | 0 | NS_ENSURE_ARG_POINTER(aAmount); |
312 | 0 | MOZ_ASSERT(mAmount); |
313 | 0 | nsCOMPtr<nsIPaymentCurrencyAmount> amount = mAmount; |
314 | 0 | amount.forget(aAmount); |
315 | 0 | return NS_OK; |
316 | 0 | } |
317 | | |
318 | | NS_IMETHODIMP |
319 | | PaymentShippingOption::GetSelected(bool* aSelected) |
320 | 0 | { |
321 | 0 | NS_ENSURE_ARG_POINTER(aSelected); |
322 | 0 | *aSelected = mSelected; |
323 | 0 | return NS_OK; |
324 | 0 | } |
325 | | |
326 | | NS_IMETHODIMP |
327 | | PaymentShippingOption::SetSelected(bool aSelected) |
328 | 0 | { |
329 | 0 | mSelected = aSelected; |
330 | 0 | return NS_OK; |
331 | 0 | } |
332 | | |
333 | | /* PaymentDetails */ |
334 | | |
335 | | NS_IMPL_ISUPPORTS(PaymentDetails, |
336 | | nsIPaymentDetails) |
337 | | |
338 | | PaymentDetails::PaymentDetails(const nsAString& aId, |
339 | | nsIPaymentItem* aTotalItem, |
340 | | nsIArray* aDisplayItems, |
341 | | nsIArray* aShippingOptions, |
342 | | nsIArray* aModifiers, |
343 | | const nsAString& aError, |
344 | | const nsAString& aShippingAddressErrors, |
345 | | const nsAString& aPayerErrors, |
346 | | const nsAString& aPaymentMethodErrors) |
347 | | : mId(aId) |
348 | | , mTotalItem(aTotalItem) |
349 | | , mDisplayItems(aDisplayItems) |
350 | | , mShippingOptions(aShippingOptions) |
351 | | , mModifiers(aModifiers) |
352 | | , mError(aError) |
353 | | , mShippingAddressErrors(aShippingAddressErrors) |
354 | | , mPayerErrors(aPayerErrors) |
355 | | , mPaymentMethodErrors(aPaymentMethodErrors) |
356 | 0 | { |
357 | 0 | } |
358 | | |
359 | | nsresult |
360 | | PaymentDetails::Create(const IPCPaymentDetails& aIPCDetails, |
361 | | nsIPaymentDetails** aDetails) |
362 | 0 | { |
363 | 0 | NS_ENSURE_ARG_POINTER(aDetails); |
364 | 0 |
|
365 | 0 | nsCOMPtr<nsIPaymentItem> total; |
366 | 0 | nsresult rv = PaymentItem::Create(aIPCDetails.total(), getter_AddRefs(total)); |
367 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
368 | 0 | return rv; |
369 | 0 | } |
370 | 0 | |
371 | 0 | nsCOMPtr<nsIArray> displayItems; |
372 | 0 | nsCOMPtr<nsIMutableArray> items = do_CreateInstance(NS_ARRAY_CONTRACTID); |
373 | 0 | MOZ_ASSERT(items); |
374 | 0 | for (const IPCPaymentItem& displayItem : aIPCDetails.displayItems()) { |
375 | 0 | nsCOMPtr<nsIPaymentItem> item; |
376 | 0 | rv = PaymentItem::Create(displayItem, getter_AddRefs(item)); |
377 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
378 | 0 | return rv; |
379 | 0 | } |
380 | 0 | rv = items->AppendElement(item); |
381 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
382 | 0 | return rv; |
383 | 0 | } |
384 | 0 | } |
385 | 0 | displayItems = items.forget(); |
386 | 0 |
|
387 | 0 | nsCOMPtr<nsIArray> shippingOptions; |
388 | 0 | nsCOMPtr<nsIMutableArray> options = do_CreateInstance(NS_ARRAY_CONTRACTID); |
389 | 0 | MOZ_ASSERT(options); |
390 | 0 | for (const IPCPaymentShippingOption& shippingOption : aIPCDetails.shippingOptions()) { |
391 | 0 | nsCOMPtr<nsIPaymentShippingOption> option; |
392 | 0 | rv = PaymentShippingOption::Create(shippingOption, getter_AddRefs(option)); |
393 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
394 | 0 | return rv; |
395 | 0 | } |
396 | 0 | rv = options->AppendElement(option); |
397 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
398 | 0 | return rv; |
399 | 0 | } |
400 | 0 | } |
401 | 0 | shippingOptions = options.forget(); |
402 | 0 |
|
403 | 0 | nsCOMPtr<nsIArray> modifiers; |
404 | 0 | nsCOMPtr<nsIMutableArray> detailsModifiers = do_CreateInstance(NS_ARRAY_CONTRACTID); |
405 | 0 | MOZ_ASSERT(detailsModifiers); |
406 | 0 | for (const IPCPaymentDetailsModifier& modifier : aIPCDetails.modifiers()) { |
407 | 0 | nsCOMPtr<nsIPaymentDetailsModifier> detailsModifier; |
408 | 0 | rv = PaymentDetailsModifier::Create(modifier, getter_AddRefs(detailsModifier)); |
409 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
410 | 0 | return rv; |
411 | 0 | } |
412 | 0 | rv = detailsModifiers->AppendElement(detailsModifier); |
413 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
414 | 0 | return rv; |
415 | 0 | } |
416 | 0 | } |
417 | 0 | modifiers = detailsModifiers.forget(); |
418 | 0 |
|
419 | 0 | nsCOMPtr<nsIPaymentDetails> details = |
420 | 0 | new PaymentDetails(aIPCDetails.id(), total, displayItems, shippingOptions, |
421 | 0 | modifiers, aIPCDetails.error(), |
422 | 0 | aIPCDetails.shippingAddressErrors(), |
423 | 0 | aIPCDetails.payerErrors(), |
424 | 0 | aIPCDetails.paymentMethodErrors()); |
425 | 0 |
|
426 | 0 | details.forget(aDetails); |
427 | 0 | return NS_OK; |
428 | 0 | } |
429 | | |
430 | | NS_IMETHODIMP |
431 | | PaymentDetails::GetId(nsAString& aId) |
432 | 0 | { |
433 | 0 | aId = mId; |
434 | 0 | return NS_OK; |
435 | 0 | } |
436 | | |
437 | | NS_IMETHODIMP |
438 | | PaymentDetails::GetTotalItem(nsIPaymentItem** aTotalItem) |
439 | 0 | { |
440 | 0 | NS_ENSURE_ARG_POINTER(aTotalItem); |
441 | 0 | MOZ_ASSERT(mTotalItem); |
442 | 0 | nsCOMPtr<nsIPaymentItem> total = mTotalItem; |
443 | 0 | total.forget(aTotalItem); |
444 | 0 | return NS_OK; |
445 | 0 | } |
446 | | |
447 | | NS_IMETHODIMP |
448 | | PaymentDetails::GetDisplayItems(nsIArray** aDisplayItems) |
449 | 0 | { |
450 | 0 | NS_ENSURE_ARG_POINTER(aDisplayItems); |
451 | 0 | nsCOMPtr<nsIArray> displayItems = mDisplayItems; |
452 | 0 | displayItems.forget(aDisplayItems); |
453 | 0 | return NS_OK; |
454 | 0 | } |
455 | | |
456 | | NS_IMETHODIMP |
457 | | PaymentDetails::GetShippingOptions(nsIArray** aShippingOptions) |
458 | 0 | { |
459 | 0 | NS_ENSURE_ARG_POINTER(aShippingOptions); |
460 | 0 | nsCOMPtr<nsIArray> options = mShippingOptions; |
461 | 0 | options.forget(aShippingOptions); |
462 | 0 | return NS_OK; |
463 | 0 | } |
464 | | |
465 | | NS_IMETHODIMP |
466 | | PaymentDetails::GetModifiers(nsIArray** aModifiers) |
467 | 0 | { |
468 | 0 | NS_ENSURE_ARG_POINTER(aModifiers); |
469 | 0 | nsCOMPtr<nsIArray> modifiers = mModifiers; |
470 | 0 | modifiers.forget(aModifiers); |
471 | 0 | return NS_OK; |
472 | 0 | } |
473 | | |
474 | | NS_IMETHODIMP |
475 | | PaymentDetails::GetError(nsAString& aError) |
476 | 0 | { |
477 | 0 | aError = mError; |
478 | 0 | return NS_OK; |
479 | 0 | } |
480 | | |
481 | | NS_IMETHODIMP |
482 | | PaymentDetails::GetShippingAddressErrors(JSContext* aCx, JS::MutableHandleValue aErrors) |
483 | 0 | { |
484 | 0 | AddressErrors errors; |
485 | 0 | errors.Init(mShippingAddressErrors); |
486 | 0 | if (!ToJSValue(aCx, errors, aErrors)) { |
487 | 0 | return NS_ERROR_FAILURE; |
488 | 0 | } |
489 | 0 | return NS_OK; |
490 | 0 | } |
491 | | |
492 | | NS_IMETHODIMP |
493 | | PaymentDetails::GetPayer(JSContext* aCx, JS::MutableHandleValue aErrors) |
494 | 0 | { |
495 | 0 | PayerErrorFields errors; |
496 | 0 | errors.Init(mPayerErrors); |
497 | 0 | if (!ToJSValue(aCx, errors, aErrors)) { |
498 | 0 | return NS_ERROR_FAILURE; |
499 | 0 | } |
500 | 0 | return NS_OK; |
501 | 0 | } |
502 | | |
503 | | NS_IMETHODIMP |
504 | | PaymentDetails::GetPaymentMethod(JSContext* aCx, JS::MutableHandleValue aErrors) |
505 | 0 | { |
506 | 0 | if (mPaymentMethodErrors.IsEmpty()) { |
507 | 0 | aErrors.set(JS::NullValue()); |
508 | 0 | return NS_OK; |
509 | 0 | } |
510 | 0 | nsresult rv = DeserializeToJSValue(mPaymentMethodErrors, aCx ,aErrors); |
511 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
512 | 0 | return rv; |
513 | 0 | } |
514 | 0 | return NS_OK; |
515 | 0 | } |
516 | | |
517 | | nsresult |
518 | | PaymentDetails::Update(nsIPaymentDetails* aDetails, const bool aRequestShipping) |
519 | 0 | { |
520 | 0 | MOZ_ASSERT(aDetails); |
521 | 0 | /* |
522 | 0 | * According to the spec [1], update the attributes if they present in new |
523 | 0 | * details (i.e., PaymentDetailsUpdate); otherwise, keep original value. |
524 | 0 | * Note |id| comes only from initial details (i.e., PaymentDetailsInit) and |
525 | 0 | * |error| only from new details. |
526 | 0 | * |
527 | 0 | * [1] https://www.w3.org/TR/payment-request/#updatewith-method |
528 | 0 | */ |
529 | 0 |
|
530 | 0 | nsresult rv = aDetails->GetTotalItem(getter_AddRefs(mTotalItem)); |
531 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
532 | 0 | return rv; |
533 | 0 | } |
534 | 0 | |
535 | 0 | nsCOMPtr<nsIArray> displayItems; |
536 | 0 | rv = aDetails->GetDisplayItems(getter_AddRefs(displayItems)); |
537 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
538 | 0 | return rv; |
539 | 0 | } |
540 | 0 | if (displayItems) { |
541 | 0 | mDisplayItems = displayItems; |
542 | 0 | } |
543 | 0 |
|
544 | 0 | if (aRequestShipping) { |
545 | 0 | nsCOMPtr<nsIArray> shippingOptions; |
546 | 0 | rv = aDetails->GetShippingOptions(getter_AddRefs(shippingOptions)); |
547 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
548 | 0 | return rv; |
549 | 0 | } |
550 | 0 | mShippingOptions = shippingOptions; |
551 | 0 | } |
552 | 0 |
|
553 | 0 | nsCOMPtr<nsIArray> modifiers; |
554 | 0 | rv = aDetails->GetModifiers(getter_AddRefs(modifiers)); |
555 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
556 | 0 | return rv; |
557 | 0 | } |
558 | 0 | if (modifiers) { |
559 | 0 | mModifiers = modifiers; |
560 | 0 | } |
561 | 0 |
|
562 | 0 | rv = aDetails->GetError(mError); |
563 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
564 | 0 | return rv; |
565 | 0 | } |
566 | 0 | |
567 | 0 | PaymentDetails* rowDetails = static_cast<PaymentDetails*>(aDetails); |
568 | 0 | MOZ_ASSERT(rowDetails); |
569 | 0 | mShippingAddressErrors = rowDetails->GetShippingAddressErrors(); |
570 | 0 | mPayerErrors = rowDetails->GetPayer(); |
571 | 0 | mPaymentMethodErrors = rowDetails->GetPaymentMethod(); |
572 | 0 |
|
573 | 0 | return NS_OK; |
574 | 0 |
|
575 | 0 | } |
576 | | |
577 | | const nsString& |
578 | | PaymentDetails::GetShippingAddressErrors() const |
579 | 0 | { |
580 | 0 | return mShippingAddressErrors; |
581 | 0 | } |
582 | | |
583 | | const nsString& |
584 | | PaymentDetails::GetPayer() const |
585 | 0 | { |
586 | 0 | return mPayerErrors; |
587 | 0 | } |
588 | | |
589 | | const nsString& |
590 | | PaymentDetails::GetPaymentMethod() const |
591 | 0 | { |
592 | 0 | return mPaymentMethodErrors; |
593 | 0 | } |
594 | | |
595 | | nsresult |
596 | | PaymentDetails::UpdateErrors(const nsAString& aError, |
597 | | const nsAString& aPayerErrors, |
598 | | const nsAString& aPaymentMethodErrors, |
599 | | const nsAString& aShippingAddressErrors) |
600 | 0 | { |
601 | 0 | mError = aError; |
602 | 0 | mPayerErrors = aPayerErrors; |
603 | 0 | mPaymentMethodErrors = aPaymentMethodErrors; |
604 | 0 | mShippingAddressErrors = aShippingAddressErrors; |
605 | 0 | return NS_OK; |
606 | 0 | } |
607 | | |
608 | | /* PaymentOptions */ |
609 | | |
610 | | NS_IMPL_ISUPPORTS(PaymentOptions, |
611 | | nsIPaymentOptions) |
612 | | |
613 | | PaymentOptions::PaymentOptions(const bool aRequestPayerName, |
614 | | const bool aRequestPayerEmail, |
615 | | const bool aRequestPayerPhone, |
616 | | const bool aRequestShipping, |
617 | | const nsAString& aShippingType) |
618 | | : mRequestPayerName(aRequestPayerName) |
619 | | , mRequestPayerEmail(aRequestPayerEmail) |
620 | | , mRequestPayerPhone(aRequestPayerPhone) |
621 | | , mRequestShipping(aRequestShipping) |
622 | | , mShippingType(aShippingType) |
623 | 0 | { |
624 | 0 | } |
625 | | |
626 | | nsresult |
627 | | PaymentOptions::Create(const IPCPaymentOptions& aIPCOptions, |
628 | | nsIPaymentOptions** aOptions) |
629 | 0 | { |
630 | 0 | NS_ENSURE_ARG_POINTER(aOptions); |
631 | 0 |
|
632 | 0 | nsCOMPtr<nsIPaymentOptions> options = |
633 | 0 | new PaymentOptions(aIPCOptions.requestPayerName(), |
634 | 0 | aIPCOptions.requestPayerEmail(), |
635 | 0 | aIPCOptions.requestPayerPhone(), |
636 | 0 | aIPCOptions.requestShipping(), |
637 | 0 | aIPCOptions.shippingType()); |
638 | 0 | options.forget(aOptions); |
639 | 0 | return NS_OK; |
640 | 0 | } |
641 | | |
642 | | NS_IMETHODIMP |
643 | | PaymentOptions::GetRequestPayerName(bool* aRequestPayerName) |
644 | 0 | { |
645 | 0 | NS_ENSURE_ARG_POINTER(aRequestPayerName); |
646 | 0 | *aRequestPayerName = mRequestPayerName; |
647 | 0 | return NS_OK; |
648 | 0 | } |
649 | | |
650 | | NS_IMETHODIMP |
651 | | PaymentOptions::GetRequestPayerEmail(bool* aRequestPayerEmail) |
652 | 0 | { |
653 | 0 | NS_ENSURE_ARG_POINTER(aRequestPayerEmail); |
654 | 0 | *aRequestPayerEmail = mRequestPayerEmail; |
655 | 0 | return NS_OK; |
656 | 0 | } |
657 | | |
658 | | NS_IMETHODIMP |
659 | | PaymentOptions::GetRequestPayerPhone(bool* aRequestPayerPhone) |
660 | 0 | { |
661 | 0 | NS_ENSURE_ARG_POINTER(aRequestPayerPhone); |
662 | 0 | *aRequestPayerPhone = mRequestPayerPhone; |
663 | 0 | return NS_OK; |
664 | 0 | } |
665 | | |
666 | | NS_IMETHODIMP |
667 | | PaymentOptions::GetRequestShipping(bool* aRequestShipping) |
668 | 0 | { |
669 | 0 | NS_ENSURE_ARG_POINTER(aRequestShipping); |
670 | 0 | *aRequestShipping = mRequestShipping; |
671 | 0 | return NS_OK; |
672 | 0 | } |
673 | | |
674 | | NS_IMETHODIMP |
675 | | PaymentOptions::GetShippingType(nsAString& aShippingType) |
676 | 0 | { |
677 | 0 | aShippingType = mShippingType; |
678 | 0 | return NS_OK; |
679 | 0 | } |
680 | | |
681 | | /* PaymentReqeust */ |
682 | | |
683 | | NS_IMPL_ISUPPORTS(PaymentRequest, |
684 | | nsIPaymentRequest) |
685 | | |
686 | | PaymentRequest::PaymentRequest(const uint64_t aTabId, |
687 | | const nsAString& aRequestId, |
688 | | nsIPrincipal* aTopLevelPrincipal, |
689 | | nsIArray* aPaymentMethods, |
690 | | nsIPaymentDetails* aPaymentDetails, |
691 | | nsIPaymentOptions* aPaymentOptions, |
692 | | const nsAString& aShippingOption) |
693 | | : mTabId(aTabId) |
694 | | , mRequestId(aRequestId) |
695 | | , mTopLevelPrincipal(aTopLevelPrincipal) |
696 | | , mPaymentMethods(aPaymentMethods) |
697 | | , mPaymentDetails(aPaymentDetails) |
698 | | , mPaymentOptions(aPaymentOptions) |
699 | | , mShippingOption(aShippingOption) |
700 | | , mState(eCreated) |
701 | 0 | { |
702 | 0 | } |
703 | | |
704 | | NS_IMETHODIMP |
705 | | PaymentRequest::GetTabId(uint64_t* aTabId) |
706 | 0 | { |
707 | 0 | NS_ENSURE_ARG_POINTER(aTabId); |
708 | 0 | *aTabId = mTabId; |
709 | 0 | return NS_OK; |
710 | 0 | } |
711 | | |
712 | | NS_IMETHODIMP |
713 | | PaymentRequest::GetTopLevelPrincipal(nsIPrincipal** aTopLevelPrincipal) |
714 | 0 | { |
715 | 0 | NS_ENSURE_ARG_POINTER(aTopLevelPrincipal); |
716 | 0 | MOZ_ASSERT(mTopLevelPrincipal); |
717 | 0 | nsCOMPtr<nsIPrincipal> principal = mTopLevelPrincipal; |
718 | 0 | principal.forget(aTopLevelPrincipal); |
719 | 0 | return NS_OK; |
720 | 0 | } |
721 | | |
722 | | NS_IMETHODIMP |
723 | | PaymentRequest::GetRequestId(nsAString& aRequestId) |
724 | 0 | { |
725 | 0 | aRequestId = mRequestId; |
726 | 0 | return NS_OK; |
727 | 0 | } |
728 | | |
729 | | NS_IMETHODIMP |
730 | | PaymentRequest::GetPaymentMethods(nsIArray** aPaymentMethods) |
731 | 0 | { |
732 | 0 | NS_ENSURE_ARG_POINTER(aPaymentMethods); |
733 | 0 | MOZ_ASSERT(mPaymentMethods); |
734 | 0 | nsCOMPtr<nsIArray> methods = mPaymentMethods; |
735 | 0 | methods.forget(aPaymentMethods); |
736 | 0 | return NS_OK; |
737 | 0 | } |
738 | | |
739 | | NS_IMETHODIMP |
740 | | PaymentRequest::GetPaymentDetails(nsIPaymentDetails** aPaymentDetails) |
741 | 0 | { |
742 | 0 | NS_ENSURE_ARG_POINTER(aPaymentDetails); |
743 | 0 | MOZ_ASSERT(mPaymentDetails); |
744 | 0 | nsCOMPtr<nsIPaymentDetails> details = mPaymentDetails; |
745 | 0 | details.forget(aPaymentDetails); |
746 | 0 | return NS_OK; |
747 | 0 | } |
748 | | |
749 | | NS_IMETHODIMP |
750 | | PaymentRequest::GetPaymentOptions(nsIPaymentOptions** aPaymentOptions) |
751 | 0 | { |
752 | 0 | NS_ENSURE_ARG_POINTER(aPaymentOptions); |
753 | 0 | MOZ_ASSERT(mPaymentOptions); |
754 | 0 | nsCOMPtr<nsIPaymentOptions> options = mPaymentOptions; |
755 | 0 | options.forget(aPaymentOptions); |
756 | 0 | return NS_OK; |
757 | 0 | } |
758 | | |
759 | | NS_IMETHODIMP |
760 | | PaymentRequest::GetShippingOption(nsAString& aShippingOption) |
761 | 0 | { |
762 | 0 | aShippingOption = mShippingOption; |
763 | 0 | return NS_OK; |
764 | 0 | } |
765 | | |
766 | | nsresult |
767 | | PaymentRequest::UpdatePaymentDetails(nsIPaymentDetails* aPaymentDetails, |
768 | | const nsAString& aShippingOption) |
769 | 0 | { |
770 | 0 | MOZ_ASSERT(aPaymentDetails); |
771 | 0 | bool requestShipping; |
772 | 0 | nsresult rv = mPaymentOptions->GetRequestShipping(&requestShipping); |
773 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
774 | 0 | return rv; |
775 | 0 | } |
776 | 0 | mShippingOption = aShippingOption; |
777 | 0 |
|
778 | 0 | PaymentDetails* rowDetails = static_cast<PaymentDetails*>(mPaymentDetails.get()); |
779 | 0 | MOZ_ASSERT(rowDetails); |
780 | 0 | return rowDetails->Update(aPaymentDetails, requestShipping); |
781 | 0 | } |
782 | | |
783 | | void |
784 | | PaymentRequest::SetCompleteStatus(const nsAString& aCompleteStatus) |
785 | 0 | { |
786 | 0 | mCompleteStatus = aCompleteStatus; |
787 | 0 | } |
788 | | |
789 | | nsresult |
790 | | PaymentRequest::UpdateErrors(const nsAString& aError, |
791 | | const nsAString& aPayerErrors, |
792 | | const nsAString& aPaymentMethodErrors, |
793 | | const nsAString& aShippingAddressErrors) |
794 | 0 | { |
795 | 0 | PaymentDetails* rowDetails = static_cast<PaymentDetails*>(mPaymentDetails.get()); |
796 | 0 | MOZ_ASSERT(rowDetails); |
797 | 0 | return rowDetails->UpdateErrors(aError, |
798 | 0 | aPayerErrors, |
799 | 0 | aPaymentMethodErrors, |
800 | 0 | aShippingAddressErrors); |
801 | 0 | } |
802 | | |
803 | | NS_IMETHODIMP |
804 | | PaymentRequest::GetCompleteStatus(nsAString& aCompleteStatus) |
805 | 0 | { |
806 | 0 | aCompleteStatus = mCompleteStatus; |
807 | 0 | return NS_OK; |
808 | 0 | } |
809 | | |
810 | | /* PaymentAddress */ |
811 | | |
812 | | NS_IMPL_ISUPPORTS(PaymentAddress, nsIPaymentAddress) |
813 | | |
814 | | NS_IMETHODIMP |
815 | | PaymentAddress::Init(const nsAString& aCountry, |
816 | | nsIArray* aAddressLine, |
817 | | const nsAString& aRegion, |
818 | | const nsAString& aCity, |
819 | | const nsAString& aDependentLocality, |
820 | | const nsAString& aPostalCode, |
821 | | const nsAString& aSortingCode, |
822 | | const nsAString& aOrganization, |
823 | | const nsAString& aRecipient, |
824 | | const nsAString& aPhone) |
825 | 0 | { |
826 | 0 | mCountry = aCountry; |
827 | 0 | mAddressLine = aAddressLine; |
828 | 0 | mRegion = aRegion; |
829 | 0 | mCity = aCity; |
830 | 0 | mDependentLocality = aDependentLocality; |
831 | 0 | mPostalCode = aPostalCode; |
832 | 0 | mSortingCode = aSortingCode; |
833 | 0 | mOrganization = aOrganization; |
834 | 0 | mRecipient = aRecipient; |
835 | 0 | mPhone = aPhone; |
836 | 0 | return NS_OK; |
837 | 0 | } |
838 | | |
839 | | NS_IMETHODIMP |
840 | | PaymentAddress::GetCountry(nsAString& aCountry) |
841 | 0 | { |
842 | 0 | aCountry = mCountry; |
843 | 0 | return NS_OK; |
844 | 0 | } |
845 | | |
846 | | NS_IMETHODIMP |
847 | | PaymentAddress::GetAddressLine(nsIArray** aAddressLine) |
848 | 0 | { |
849 | 0 | NS_ENSURE_ARG_POINTER(aAddressLine); |
850 | 0 | nsCOMPtr<nsIArray> addressLine = mAddressLine; |
851 | 0 | addressLine.forget(aAddressLine); |
852 | 0 | return NS_OK; |
853 | 0 | } |
854 | | |
855 | | NS_IMETHODIMP |
856 | | PaymentAddress::GetRegion(nsAString& aRegion) |
857 | 0 | { |
858 | 0 | aRegion = mRegion; |
859 | 0 | return NS_OK; |
860 | 0 | } |
861 | | |
862 | | NS_IMETHODIMP |
863 | | PaymentAddress::GetCity(nsAString& aCity) |
864 | 0 | { |
865 | 0 | aCity = mCity; |
866 | 0 | return NS_OK; |
867 | 0 | } |
868 | | |
869 | | NS_IMETHODIMP |
870 | | PaymentAddress::GetDependentLocality(nsAString& aDependentLocality) |
871 | 0 | { |
872 | 0 | aDependentLocality = mDependentLocality; |
873 | 0 | return NS_OK; |
874 | 0 | } |
875 | | |
876 | | NS_IMETHODIMP |
877 | | PaymentAddress::GetPostalCode(nsAString& aPostalCode) |
878 | 0 | { |
879 | 0 | aPostalCode = mPostalCode; |
880 | 0 | return NS_OK; |
881 | 0 | } |
882 | | |
883 | | NS_IMETHODIMP |
884 | | PaymentAddress::GetSortingCode(nsAString& aSortingCode) |
885 | 0 | { |
886 | 0 | aSortingCode = mSortingCode; |
887 | 0 | return NS_OK; |
888 | 0 | } |
889 | | |
890 | | NS_IMETHODIMP |
891 | | PaymentAddress::GetOrganization(nsAString& aOrganization) |
892 | 0 | { |
893 | 0 | aOrganization = mOrganization; |
894 | 0 | return NS_OK; |
895 | 0 | } |
896 | | |
897 | | NS_IMETHODIMP |
898 | | PaymentAddress::GetRecipient(nsAString& aRecipient) |
899 | 0 | { |
900 | 0 | aRecipient = mRecipient; |
901 | 0 | return NS_OK; |
902 | 0 | } |
903 | | |
904 | | NS_IMETHODIMP |
905 | | PaymentAddress::GetPhone(nsAString& aPhone) |
906 | 0 | { |
907 | 0 | aPhone = mPhone; |
908 | 0 | return NS_OK; |
909 | 0 | } |
910 | | |
911 | | } // end of namespace payment |
912 | | } // end of namespace dom |
913 | | } // end of namespace mozilla |