dom/payment/Payment.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/payment/Payment.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
    1.11 +
    1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
    1.15 +
    1.16 +const PAYMENTCONTENTHELPER_CID =
    1.17 +  Components.ID("{a920adc0-c36e-4fd0-8de0-aac1ac6ebbd0}");
    1.18 +
    1.19 +const PAYMENT_IPC_MSG_NAMES = ["Payment:Success",
    1.20 +                               "Payment:Failed"];
    1.21 +
    1.22 +const PREF_DEBUG = "dom.payment.debug";
    1.23 +
    1.24 +XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
    1.25 +                                   "@mozilla.org/childprocessmessagemanager;1",
    1.26 +                                   "nsIMessageSender");
    1.27 +
    1.28 +function PaymentContentHelper() {
    1.29 +};
    1.30 +
    1.31 +PaymentContentHelper.prototype = {
    1.32 +  __proto__: DOMRequestIpcHelper.prototype,
    1.33 +
    1.34 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsINavigatorPayment,
    1.35 +                                         Ci.nsIDOMGlobalPropertyInitializer,
    1.36 +                                         Ci.nsISupportsWeakReference,
    1.37 +                                         Ci.nsIObserver]),
    1.38 +  classID:        PAYMENTCONTENTHELPER_CID,
    1.39 +  classInfo:      XPCOMUtils.generateCI({
    1.40 +    classID: PAYMENTCONTENTHELPER_CID,
    1.41 +    contractID: "@mozilla.org/payment/content-helper;1",
    1.42 +    classDescription: "Payment Content Helper",
    1.43 +    flags: Ci.nsIClassInfo.DOM_OBJECT,
    1.44 +    interfaces: [Ci.nsINavigatorPayment]
    1.45 +  }),
    1.46 +
    1.47 +  // nsINavigatorPayment
    1.48 +
    1.49 +  pay: function pay(aJwts) {
    1.50 +    let request = this.createRequest();
    1.51 +    let requestId = this.getRequestId(request);
    1.52 +
    1.53 +    let docShell = this._window.QueryInterface(Ci.nsIInterfaceRequestor)
    1.54 +                   .getInterface(Ci.nsIWebNavigation)
    1.55 +                   .QueryInterface(Ci.nsIDocShell);
    1.56 +    if (!docShell.isActive) {
    1.57 +      if (this._debug) {
    1.58 +        this.LOG("The caller application is a background app. No request " +
    1.59 +                  "will be sent");
    1.60 +      }
    1.61 +      let runnable = {
    1.62 +        run: function run() {
    1.63 +          Services.DOMRequest.fireError(request, "BACKGROUND_APP");
    1.64 +        }
    1.65 +      }
    1.66 +      Services.tm.currentThread.dispatch(runnable,
    1.67 +                                         Ci.nsIThread.DISPATCH_NORMAL);
    1.68 +      return request;
    1.69 +    }
    1.70 +
    1.71 +    if (!Array.isArray(aJwts)) {
    1.72 +      aJwts = [aJwts];
    1.73 +    }
    1.74 +
    1.75 +    cpmm.sendAsyncMessage("Payment:Pay", {
    1.76 +      jwts: aJwts,
    1.77 +      requestId: requestId
    1.78 +    });
    1.79 +    return request;
    1.80 +  },
    1.81 +
    1.82 +  // nsIDOMGlobalPropertyInitializer
    1.83 +
    1.84 +  init: function(aWindow) {
    1.85 +    try {
    1.86 +      if (!Services.prefs.getBoolPref("dom.mozPay.enabled")) {
    1.87 +        return null;
    1.88 +      }
    1.89 +    } catch (e) {
    1.90 +      return null;
    1.91 +    }
    1.92 +
    1.93 +    this._window = aWindow;
    1.94 +    this.initDOMRequestHelper(aWindow, PAYMENT_IPC_MSG_NAMES);
    1.95 +
    1.96 +    try {
    1.97 +      this._debug =
    1.98 +        Services.prefs.getPrefType(PREF_DEBUG) == Ci.nsIPrefBranch.PREF_BOOL
    1.99 +        && Services.prefs.getBoolPref(PREF_DEBUG);
   1.100 +    } catch(e) {
   1.101 +      this._debug = false;
   1.102 +    }
   1.103 +
   1.104 +    return this.pay.bind(this);
   1.105 +  },
   1.106 +
   1.107 +  // nsIFrameMessageListener
   1.108 +
   1.109 +  receiveMessage: function receiveMessage(aMessage) {
   1.110 +    let name = aMessage.name;
   1.111 +    let msg = aMessage.json;
   1.112 +    if (this._debug) {
   1.113 +      this.LOG("Received message '" + name + "': " + JSON.stringify(msg));
   1.114 +    }
   1.115 +    let requestId = msg.requestId;
   1.116 +    let request = this.takeRequest(requestId);
   1.117 +    if (!request) {
   1.118 +      return;
   1.119 +    }
   1.120 +    switch (name) {
   1.121 +      case "Payment:Success":
   1.122 +        Services.DOMRequest.fireSuccess(request, msg.result);
   1.123 +        break;
   1.124 +      case "Payment:Failed":
   1.125 +        Services.DOMRequest.fireError(request, msg.errorMsg);
   1.126 +        break;
   1.127 +    }
   1.128 +  },
   1.129 +
   1.130 +  LOG: function LOG(s) {
   1.131 +    if (!this._debug) {
   1.132 +      return;
   1.133 +    }
   1.134 +    dump("-*- PaymentContentHelper: " + s + "\n");
   1.135 +  }
   1.136 +};
   1.137 +
   1.138 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentContentHelper]);

mercurial