Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / payments / BasicCardPayment.cpp
blob403260cd915f09fc570a552c744a5f3b0a805503
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 #include "BasicCardPayment.h"
8 #include "PaymentAddress.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/ErrorResult.h"
11 #include "nsArrayUtils.h"
13 namespace mozilla::dom {
14 namespace {
15 bool IsValidNetwork(const nsAString& aNetwork) {
16 return aNetwork.Equals(u"amex"_ns) || aNetwork.Equals(u"cartebancaire"_ns) ||
17 aNetwork.Equals(u"diners"_ns) || aNetwork.Equals(u"discover"_ns) ||
18 aNetwork.Equals(u"jcb"_ns) || aNetwork.Equals(u"mastercard"_ns) ||
19 aNetwork.Equals(u"mir"_ns) || aNetwork.Equals(u"unionpay"_ns) ||
20 aNetwork.Equals(u"visa"_ns);
22 } // end of namespace
24 StaticRefPtr<BasicCardService> gBasicCardService;
26 already_AddRefed<BasicCardService> BasicCardService::GetService() {
27 if (!gBasicCardService) {
28 gBasicCardService = new BasicCardService();
29 ClearOnShutdown(&gBasicCardService);
31 RefPtr<BasicCardService> service = gBasicCardService;
32 return service.forget();
35 bool BasicCardService::IsBasicCardPayment(const nsAString& aSupportedMethods) {
36 return aSupportedMethods.Equals(u"basic-card"_ns);
39 bool BasicCardService::IsValidBasicCardRequest(JSContext* aCx, JSObject* aData,
40 nsAString& aErrorMsg) {
41 if (!aData) {
42 return true;
44 JS::Rooted<JS::Value> data(aCx, JS::ObjectValue(*aData));
46 BasicCardRequest request;
47 if (!request.Init(aCx, data)) {
48 aErrorMsg.AssignLiteral(
49 "Fail to convert methodData.data to BasicCardRequest.");
50 return false;
53 for (const nsString& network : request.mSupportedNetworks) {
54 if (!IsValidNetwork(network)) {
55 aErrorMsg.Assign(network + u" is not an valid network."_ns);
56 return false;
59 return true;
62 bool BasicCardService::IsValidExpiryMonth(const nsAString& aExpiryMonth) {
63 // ExpiryMonth can only be
64 // 1. empty string
65 // 2. 01 ~ 12
66 if (aExpiryMonth.IsEmpty()) {
67 return true;
69 if (aExpiryMonth.Length() != 2) {
70 return false;
72 // can only be 00 ~ 09
73 if (aExpiryMonth.CharAt(0) == '0') {
74 if (aExpiryMonth.CharAt(1) < '0' || aExpiryMonth.CharAt(1) > '9') {
75 return false;
77 return true;
79 // can only be 11 or 12
80 if (aExpiryMonth.CharAt(0) == '1') {
81 if (aExpiryMonth.CharAt(1) != '1' && aExpiryMonth.CharAt(1) != '2') {
82 return false;
84 return true;
86 return false;
89 bool BasicCardService::IsValidExpiryYear(const nsAString& aExpiryYear) {
90 // ExpiryYear can only be
91 // 1. empty string
92 // 2. 0000 ~ 9999
93 if (!aExpiryYear.IsEmpty()) {
94 if (aExpiryYear.Length() != 4) {
95 return false;
97 for (uint32_t index = 0; index < 4; ++index) {
98 if (aExpiryYear.CharAt(index) < '0' || aExpiryYear.CharAt(index) > '9') {
99 return false;
103 return true;
106 void BasicCardService::CheckForValidBasicCardErrors(JSContext* aCx,
107 JSObject* aData,
108 ErrorResult& aRv) {
109 MOZ_ASSERT(aData, "Don't pass null data");
110 JS::Rooted<JS::Value> data(aCx, JS::ObjectValue(*aData));
112 // XXXbz Just because aData converts to BasicCardErrors right now doesn't mean
113 // it will if someone tries again! Should we be replacing aData with a
114 // conversion of the BasicCardErrors dictionary to a JS object in a clean
115 // compartment or something?
116 BasicCardErrors bcError;
117 if (!bcError.Init(aCx, data)) {
118 aRv.NoteJSContextException(aCx);
121 } // namespace mozilla::dom