webapprt/PaymentUIGlue.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 { interfaces: Ci, utils: Cu } = Components;
     9 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    10 Cu.import("resource://gre/modules/Services.jsm");
    12 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
    13                                   "@mozilla.org/childprocessmessagemanager;1",
    14                                   "nsIMessageSender");
    16 function paymentSuccess(aRequestId) {
    17   return function(aResult) {
    18     closePaymentWindow(aRequestId, function() {
    19       cpmm.sendAsyncMessage("Payment:Success", { requestId: aRequestId,
    20                                                  result: aResult });
    21     });
    22   };
    23 }
    25 function paymentFailed(aRequestId) {
    26   return function(aErrorMsg) {
    27     closePaymentWindow(aRequestId, function() {
    28       cpmm.sendAsyncMessage("Payment:Failed", { requestId: aRequestId,
    29                                                 errorMsg: aErrorMsg });
    30     });
    31   };
    32 }
    34 let payments = {};
    36 function closePaymentWindow(aId, aCallback) {
    37   if (payments[aId]) {
    38     payments[aId].handled = true;
    39     payments[aId].win.close();
    40     payments[aId] = null;
    41   }
    43   aCallback();
    44 }
    46 function PaymentUI() {}
    48 PaymentUI.prototype = {
    49   classID: Components.ID("{ede1124f-72e8-4a31-9567-3270d46f21fb}"),
    50   QueryInterface: XPCOMUtils.generateQI([Ci.nsIPaymentUIGlue]),
    52   confirmPaymentRequest: function(aRequestId, aRequests, aSuccessCb, aErrorCb) {
    53     // If there's only one payment provider that will work, just move on
    54     // without prompting the user.
    55     if (aRequests.length == 1) {
    56       aSuccessCb.onresult(aRequestId, aRequests[0].type);
    57       return;
    58     }
    60     let items = [];
    62     // Otherwise, let the user select a payment provider from a list.
    63     for (let i = 0; i < aRequests.length; i++) {
    64       let request = aRequests[i];
    65       let requestText = request.providerName;
    66       if (request.productPrice && Array.isArray(request.productPrice)) {
    67         // We should guess the user currency and use that instead.
    68         requestText += " (" + request.productPrice[0].amount + " " +
    69                               request.productPrice[0].currency + ")";
    70       }
    71       items.push(requestText);
    72     }
    74     let selected = {};
    76     let bundle = Services.strings.
    77                    createBundle("chrome://webapprt/locale/webapp.properties");
    78     let result = Services.prompt.
    79                    select(null, bundle.GetStringFromName("paymentDialog.title"),
    80                           bundle.GetStringFromName("paymentDialog.message"),
    81                           items.length, items, selected);
    82     if (result) {
    83       aSuccessCb.onresult(aRequestId,
    84                           aRequests[selected.value].type);
    85     } else {
    86       aErrorCb.onresult(aRequestId, "USER_CANCELLED");
    87     }
    88   },
    90   showPaymentFlow: function(aRequestId, aPaymentFlowInfo, aErrorCb) {
    91     let win = Services.ww.
    92                 openWindow(null,
    93                            "chrome://webapprt/content/webapp.xul",
    94                            "_blank",
    95                            "chrome,dialog=no,resizable,scrollbars,centerscreen",
    96                            null);
    98     // Store a reference to the window so that we can close it when the payment
    99     // succeeds or fails.
   100     payments[aRequestId] = { win: win, handled: false };
   102     // Inject paymentSuccess and paymentFailed methods into the document after
   103     // its loaded.
   104     win.addEventListener("DOMContentLoaded", function onDOMContentLoaded() {
   105       win.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
   107       let browserElement = win.document.getElementById("content");
   108       browserElement.
   109         setAttribute("src", aPaymentFlowInfo.uri + aPaymentFlowInfo.jwt);
   111       browserElement.addEventListener("DOMWindowCreated",
   112         function onDOMWindowCreated() {
   113           browserElement.removeEventListener("DOMWindowCreated",
   114                                              onDOMWindowCreated);
   117           win.document.getElementById("content").contentDocument.defaultView
   118              .wrappedJSObject.mozPaymentProvider = {
   119                __exposedProps__: {
   120                  paymentSuccess: 'r',
   121                  paymentFailed: 'r'
   122                },
   123                paymentSuccess: paymentSuccess(aRequestId),
   124                paymentFailed: paymentFailed(aRequestId)
   125              };
   126         }, true);
   127     });
   129     let winObserver = function(aClosedWin, aTopic) {
   130       if (aTopic == "domwindowclosed") {
   131         // Fail the payment if the window is closed.
   132         if (aClosedWin == win) {
   133           Services.ww.unregisterNotification(winObserver);
   134           if (payments[aRequestId] && !payments[aRequestId].handled) {
   135             aErrorCb.onresult(aRequestId, "USER_CANCELLED");
   136           }
   137         }
   138       }
   139     }
   141     Services.ww.registerNotification(winObserver);
   142   },
   144   cleanup: function() {
   145   },
   146 }
   148 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PaymentUI]);

mercurial