tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CurrencyAmountValidationChromeScript.js (2155B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* Any copyright is dedicated to the Public Domain.
      3   http://creativecommons.org/publicdomain/zero/1.0/ */
      4 
      5 /* eslint-env mozilla/chrome-script */
      6 
      7 "use strict";
      8 
      9 const { XPCOMUtils } = ChromeUtils.importESModule(
     10  "resource://gre/modules/XPCOMUtils.sys.mjs"
     11 );
     12 
     13 const paymentSrv = Cc[
     14  "@mozilla.org/dom/payments/payment-request-service;1"
     15 ].getService(Ci.nsIPaymentRequestService);
     16 
     17 const InvalidDetailsUIService = {
     18  showPayment(requestId) {
     19    paymentSrv.changeShippingOption(requestId, "");
     20  },
     21  abortPayment(requestId) {
     22    const abortResponse = Cc[
     23      "@mozilla.org/dom/payments/payment-abort-action-response;1"
     24    ].createInstance(Ci.nsIPaymentAbortActionResponse);
     25    abortResponse.init(requestId, Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED);
     26    paymentSrv.respondPayment(
     27      abortResponse.QueryInterface(Ci.nsIPaymentActionResponse)
     28    );
     29  },
     30  completePayment(requestId) {},
     31  updatePayment(requestId) {},
     32  closePayment(requestId) {},
     33  QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]),
     34 };
     35 
     36 function checkLowerCaseCurrency() {
     37  const paymentEnum = paymentSrv.enumerate();
     38  if (!paymentEnum.hasMoreElements()) {
     39    const msg =
     40      "PaymentRequestService should have at least one payment request.";
     41    sendAsyncMessage("test-fail", msg);
     42  }
     43  for (let payRequest of paymentEnum) {
     44    if (!payRequest) {
     45      sendAsyncMessage("test-fail", "Fail to get existing payment request.");
     46      break;
     47    }
     48    const { currency } = payRequest.paymentDetails.totalItem.amount;
     49    if (currency != "USD") {
     50      const msg =
     51        "Currency of PaymentItem total should be 'USD', but got ${currency}";
     52      sendAsyncMessage("check-complete");
     53    }
     54  }
     55  paymentSrv.cleanup();
     56  sendAsyncMessage("check-complete");
     57 }
     58 
     59 addMessageListener("check-lower-case-currency", checkLowerCaseCurrency);
     60 
     61 addMessageListener("set-update-with-invalid-details-ui-service", () => {
     62  paymentSrv.setTestingUIService(
     63    InvalidDetailsUIService.QueryInterface(Ci.nsIPaymentUIService)
     64  );
     65 });
     66 
     67 addMessageListener("teardown", () => sendAsyncMessage("teardown-complete"));