summaryrefslogtreecommitdiffstats
path: root/browser/components/payments/PaymentUIService.jsm
blob: a0b60d8c893a38a3206f03a7df41f8df4574b4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Singleton service acting as glue between the DOM APIs and the payment dialog UI.
 *
 * Communication from the DOM to the UI happens via the nsIPaymentUIService interface.
 * The UI talks to the DOM code via the nsIPaymentRequestService interface.
 * PaymentUIService is started by the DOM code lazily.
 *
 * For now the UI is shown in a native dialog but that is likely to change.
 * Tests should try to avoid relying on that implementation detail.
 */

"use strict";

const XHTML_NS = "http://www.w3.org/1999/xhtml";

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);

ChromeUtils.defineModuleGetter(
  this,
  "BrowserWindowTracker",
  "resource:///modules/BrowserWindowTracker.jsm"
);

XPCOMUtils.defineLazyServiceGetter(
  this,
  "paymentSrv",
  "@mozilla.org/dom/payments/payment-request-service;1",
  "nsIPaymentRequestService"
);

function PaymentUIService() {
  this.wrappedJSObject = this;
  XPCOMUtils.defineLazyGetter(this, "log", () => {
    let { ConsoleAPI } = ChromeUtils.import(
      "resource://gre/modules/Console.jsm"
    );
    return new ConsoleAPI({
      maxLogLevelPref: "dom.payments.loglevel",
      prefix: "Payment UI Service",
    });
  });
  this.log.debug("constructor");
}

PaymentUIService.prototype = {
  classID: Components.ID("{01f8bd55-9017-438b-85ec-7c15d2b35cdc}"),
  QueryInterface: ChromeUtils.generateQI(["nsIPaymentUIService"]),

  // nsIPaymentUIService implementation:

  showPayment(requestId) {
    this.log.debug("showPayment:", requestId);
    let request = paymentSrv.getPaymentRequestById(requestId);
    let merchantBrowser = this.findBrowserByOuterWindowId(
      request.topOuterWindowId
    );
    let chromeWindow = merchantBrowser.ownerGlobal;
    let { gBrowser } = chromeWindow;
    let browserContainer = gBrowser.getBrowserContainer(merchantBrowser);
    let container = chromeWindow.document.createElementNS(XHTML_NS, "div");
    container.dataset.requestId = requestId;
    container.classList.add("paymentDialogContainer");
    container.hidden = true;
    let paymentsBrowser = this._createPaymentFrame(
      chromeWindow.document,
      requestId
    );

    let pdwGlobal = {};
    Services.scriptloader.loadSubScript(
      "chrome://payments/content/paymentDialogWrapper.js",
      pdwGlobal
    );

    paymentsBrowser.paymentDialogWrapper = pdwGlobal.paymentDialogWrapper;

    // Create an <html:div> wrapper to absolutely position the <xul:browser>
    // because XUL elements don't support position:absolute.
    let absDiv = chromeWindow.document.createElementNS(XHTML_NS, "div");
    container.appendChild(absDiv);

    // append the frame to start the loading
    absDiv.appendChild(paymentsBrowser);
    browserContainer.prepend(container);

    // Initialize the wrapper once the <browser> is connected.
    paymentsBrowser.paymentDialogWrapper.init(requestId, paymentsBrowser);

    this._attachBrowserEventListeners(merchantBrowser);

    // Only show the frame and change the UI when the dialog is ready to show.
    paymentsBrowser.addEventListener(
      "tabmodaldialogready",
      function readyToShow() {
        if (!container) {
          // The dialog was closed by the DOM code before it was ready to be shown.
          return;
        }
        container.hidden = false;
        this._showDialog(merchantBrowser);
      }.bind(this),
      {
        once: true,
      }
    );
  },

  abortPayment(requestId) {
    this.log.debug("abortPayment:", requestId);
    let abortResponse = Cc[
      "@mozilla.org/dom/payments/payment-abort-action-response;1"
    ].createInstance(Ci.nsIPaymentAbortActionResponse);
    let found = this.closeDialog(requestId);

    // if `win` is falsy, then we haven't found the dialog, so the abort fails
    // otherwise, the abort is successful
    let response = found
      ? Ci.nsIPaymentActionResponse.ABORT_SUCCEEDED
      : Ci.nsIPaymentActionResponse.ABORT_FAILED;

    abortResponse.init(requestId, response);
    paymentSrv.respondPayment(abortResponse);
  },

  completePayment(requestId) {
    // completeStatus should be one of "timeout", "success", "fail", ""
    let { completeStatus } = paymentSrv.getPaymentRequestById(requestId);
    this.log.debug(
      `completePayment: requestId: ${requestId}, completeStatus: ${completeStatus}`
    );

    let closed;
    switch (completeStatus) {
      case "fail":
      case "timeout":
        break;
      default:
        closed = this.closeDialog(requestId);
        break;
    }

    let paymentFrame;
    if (!closed) {
      // We need to call findDialog before we respond below as getPaymentRequestById
      // may fail due to the request being removed upon completion.
      paymentFrame = this.findDialog(requestId).paymentFrame;
      if (!paymentFrame) {
        this.log.error("completePayment: no dialog found");
        return;
      }
    }

    let responseCode = closed
      ? Ci.nsIPaymentActionResponse.COMPLETE_SUCCEEDED
      : Ci.nsIPaymentActionResponse.COMPLETE_FAILED;
    let completeResponse = Cc[
      "@mozilla.org/dom/payments/payment-complete-action-response;1"
    ].createInstance(Ci.nsIPaymentCompleteActionResponse);
    completeResponse.init(requestId, responseCode);
    paymentSrv.respondPayment(
      completeResponse.QueryInterface(Ci.nsIPaymentActionResponse)
    );

    if (!closed) {
      paymentFrame.paymentDialogWrapper.updateRequest();
    }
  },

  updatePayment(requestId) {
    let { paymentFrame } = this.findDialog(requestId);
    this.log.debug("updatePayment:", requestId);
    if (!paymentFrame) {
      this.log.error("updatePayment: no dialog found");
      return;
    }
    paymentFrame.paymentDialogWrapper.updateRequest();
  },

  closePayment(requestId) {
    this.closeDialog(requestId);
  },

  // other helper methods

  _createPaymentFrame(doc, requestId) {
    let frame = doc.createXULElement("browser");
    frame.classList.add("paymentDialogContainerFrame");
    frame.setAttribute("type", "content");
    frame.setAttribute("remote", "true");
    frame.setAttribute("disablehistory", "true");
    frame.setAttribute("nodefaultsrc", "true");
    frame.setAttribute("transparent", "true");
    frame.setAttribute("selectmenulist", "ContentSelectDropdown");
    frame.setAttribute("autocompletepopup", "PopupAutoComplete");
    return frame;
  },

  _attachBrowserEventListeners(merchantBrowser) {
    merchantBrowser.addEventListener("SwapDocShells", this);
  },

  _showDialog(merchantBrowser) {
    let chromeWindow = merchantBrowser.ownerGlobal;
    // Prevent focusing or interacting with the <browser>.
    merchantBrowser.setAttribute("tabmodalPromptShowing", "true");

    // Darken the merchant content area.
    let tabModalBackground = chromeWindow.document.createXULElement("box");
    tabModalBackground.classList.add(
      "tabModalBackground",
      "paymentDialogBackground"
    );
    // Insert the same way as <tabmodalprompt>.
    merchantBrowser.parentNode.insertBefore(
      tabModalBackground,
      merchantBrowser.nextElementSibling
    );
  },

  /**
   * @param {string} requestId - Payment Request ID of the dialog to close.
   * @returns {boolean} whether the specified dialog was closed.
   */
  closeDialog(requestId) {
    let { browser, dialogContainer, paymentFrame } = this.findDialog(requestId);
    if (!dialogContainer) {
      return false;
    }
    this.log.debug(`closing: ${requestId}`);
    paymentFrame.paymentDialogWrapper.uninit();
    dialogContainer.remove();
    browser.removeEventListener("SwapDocShells", this);

    if (!dialogContainer.hidden) {
      // If the container is no longer hidden then the background was added after
      // `tabmodaldialogready` so remove it.
      browser.parentElement.querySelector(".paymentDialogBackground").remove();

      if (
        !browser.tabModalPromptBox ||
        !browser.tabModalPromptBox.listPrompts().length
      ) {
        browser.removeAttribute("tabmodalPromptShowing");
      }
    }
    return true;
  },

  getDialogContainerForMerchantBrowser(merchantBrowser) {
    return merchantBrowser.ownerGlobal.gBrowser
      .getBrowserContainer(merchantBrowser)
      .querySelector(".paymentDialogContainer");
  },

  findDialog(requestId) {
    for (let win of BrowserWindowTracker.orderedWindows) {
      for (let dialogContainer of win.document.querySelectorAll(
        ".paymentDialogContainer"
      )) {
        if (dialogContainer.dataset.requestId == requestId) {
          return {
            dialogContainer,
            paymentFrame: dialogContainer.querySelector(
              ".paymentDialogContainerFrame"
            ),
            browser: dialogContainer.parentElement.querySelector(
              ".browserStack > browser"
            ),
          };
        }
      }
    }
    return {};
  },

  findBrowserByOuterWindowId(outerWindowId) {
    for (let win of BrowserWindowTracker.orderedWindows) {
      let browser = win.gBrowser.getBrowserForOuterWindowID(outerWindowId);
      if (!browser) {
        continue;
      }
      return browser;
    }

    this.log.error(
      "findBrowserByOuterWindowId: No browser found for outerWindowId:",
      outerWindowId
    );
    return null;
  },

  _moveDialogToNewBrowser(oldBrowser, newBrowser) {
    // Re-attach event listeners to the new browser.
    newBrowser.addEventListener("SwapDocShells", this);

    let dialogContainer = this.getDialogContainerForMerchantBrowser(oldBrowser);
    let newBrowserContainer = newBrowser.ownerGlobal.gBrowser.getBrowserContainer(
      newBrowser
    );

    // Clone the container tree
    let newDialogContainer = newBrowserContainer.ownerDocument.importNode(
      dialogContainer,
      true
    );

    let oldFrame = dialogContainer.querySelector(
      ".paymentDialogContainerFrame"
    );
    let newFrame = newDialogContainer.querySelector(
      ".paymentDialogContainerFrame"
    );

    // We need a document to be synchronously loaded in order to do the swap and
    // there's no point in wasting resources loading a dialog we're going to replace.
    newFrame.setAttribute("src", "about:blank");
    newFrame.setAttribute("nodefaultsrc", "true");

    newBrowserContainer.prepend(newDialogContainer);

    // Force the <browser> to be created so that it'll have a document loaded and frame created.
    // See `ourChildDocument` and `ourFrame` checks in nsFrameLoader::SwapWithOtherLoader.
    /* eslint-disable-next-line no-unused-expressions */
    newFrame.clientTop;

    // Swap the frameLoaders to preserve the frame state
    newFrame.swapFrameLoaders(oldFrame);
    newFrame.paymentDialogWrapper = oldFrame.paymentDialogWrapper;
    newFrame.paymentDialogWrapper.changeAttachedFrame(newFrame);
    dialogContainer.remove();

    this._showDialog(newBrowser);
  },

  handleEvent(event) {
    switch (event.type) {
      case "SwapDocShells": {
        this._moveDialogToNewBrowser(event.target, event.detail);
        break;
      }
    }
  },
};

var EXPORTED_SYMBOLS = ["PaymentUIService"];