dom/payment/Payment.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
     9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    10 Cu.import("resource://gre/modules/Services.jsm");
    11 Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
    13 const PAYMENTCONTENTHELPER_CID =
    14   Components.ID("{a920adc0-c36e-4fd0-8de0-aac1ac6ebbd0}");
    16 const PAYMENT_IPC_MSG_NAMES = ["Payment:Success",
    17                                "Payment:Failed"];
    19 const PREF_DEBUG = "dom.payment.debug";
    21 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
    22                                    "@mozilla.org/childprocessmessagemanager;1",
    23                                    "nsIMessageSender");
    25 function PaymentContentHelper() {
    26 };
    28 PaymentContentHelper.prototype = {
    29   __proto__: DOMRequestIpcHelper.prototype,
    31   QueryInterface: XPCOMUtils.generateQI([Ci.nsINavigatorPayment,
    32                                          Ci.nsIDOMGlobalPropertyInitializer,
    33                                          Ci.nsISupportsWeakReference,
    34                                          Ci.nsIObserver]),
    35   classID:        PAYMENTCONTENTHELPER_CID,
    36   classInfo:      XPCOMUtils.generateCI({
    37     classID: PAYMENTCONTENTHELPER_CID,
    38     contractID: "@mozilla.org/payment/content-helper;1",
    39     classDescription: "Payment Content Helper",
    40     flags: Ci.nsIClassInfo.DOM_OBJECT,
    41     interfaces: [Ci.nsINavigatorPayment]
    42   }),
    44   // nsINavigatorPayment
    46   pay: function pay(aJwts) {
    47     let request = this.createRequest();
    48     let requestId = this.getRequestId(request);
    50     let docShell = this._window.QueryInterface(Ci.nsIInterfaceRequestor)
    51                    .getInterface(Ci.nsIWebNavigation)
    52                    .QueryInterface(Ci.nsIDocShell);
    53     if (!docShell.isActive) {
    54       if (this._debug) {
    55         this.LOG("The caller application is a background app. No request " +
    56                   "will be sent");
    57       }
    58       let runnable = {
    59         run: function run() {
    60           Services.DOMRequest.fireError(request, "BACKGROUND_APP");
    61         }
    62       }
    63       Services.tm.currentThread.dispatch(runnable,
    64                                          Ci.nsIThread.DISPATCH_NORMAL);
    65       return request;
    66     }
    68     if (!Array.isArray(aJwts)) {
    69       aJwts = [aJwts];
    70     }
    72     cpmm.sendAsyncMessage("Payment:Pay", {
    73       jwts: aJwts,
    74       requestId: requestId
    75     });
    76     return request;
    77   },
    79   // nsIDOMGlobalPropertyInitializer
    81   init: function(aWindow) {
    82     try {
    83       if (!Services.prefs.getBoolPref("dom.mozPay.enabled")) {
    84         return null;
    85       }
    86     } catch (e) {
    87       return null;
    88     }
    90     this._window = aWindow;
    91     this.initDOMRequestHelper(aWindow, PAYMENT_IPC_MSG_NAMES);
    93     try {
    94       this._debug =
    95         Services.prefs.getPrefType(PREF_DEBUG) == Ci.nsIPrefBranch.PREF_BOOL
    96         && Services.prefs.getBoolPref(PREF_DEBUG);
    97     } catch(e) {
    98       this._debug = false;
    99     }
   101     return this.pay.bind(this);
   102   },
   104   // nsIFrameMessageListener
   106   receiveMessage: function receiveMessage(aMessage) {
   107     let name = aMessage.name;
   108     let msg = aMessage.json;
   109     if (this._debug) {
   110       this.LOG("Received message '" + name + "': " + JSON.stringify(msg));
   111     }
   112     let requestId = msg.requestId;
   113     let request = this.takeRequest(requestId);
   114     if (!request) {
   115       return;
   116     }
   117     switch (name) {
   118       case "Payment:Success":
   119         Services.DOMRequest.fireSuccess(request, msg.result);
   120         break;
   121       case "Payment:Failed":
   122         Services.DOMRequest.fireError(request, msg.errorMsg);
   123         break;
   124     }
   125   },
   127   LOG: function LOG(s) {
   128     if (!this._debug) {
   129       return;
   130     }
   131     dump("-*- PaymentContentHelper: " + s + "\n");
   132   }
   133 };
   135 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentContentHelper]);

mercurial