summaryrefslogtreecommitdiffstats
path: root/b2g/components/PresentationRequestUIGlue.js
blob: 5c50401de1bc14f8f9d028f596dadc87b553b67b (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

"use strict"

function debug(aMsg) {
  // dump("-*- PresentationRequestUIGlue: " + aMsg + "\n");
}

const { interfaces: Ci, utils: Cu, classes: Cc } = Components;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
                                  "resource://gre/modules/SystemAppProxy.jsm");

function PresentationRequestUIGlue() { }

PresentationRequestUIGlue.prototype = {

  sendRequest: function(aUrl, aSessionId, aDevice) {
    let localDevice;
    try {
      localDevice = aDevice.QueryInterface(Ci.nsIPresentationLocalDevice);
    } catch (e) {}

    if (localDevice) {
      return this.sendTo1UA(aUrl, aSessionId, localDevice.windowId);
    } else {
      return this.sendTo2UA(aUrl, aSessionId);
    }
  },

  // For 1-UA scenario
  sendTo1UA: function(aUrl, aSessionId, aWindowId) {
    return new Promise((aResolve, aReject) => {
      let handler = (evt) => {
        if (evt.type === "unload") {
          SystemAppProxy.removeEventListenerWithId(aWindowId,
                                                   "unload",
                                                   handler);
          SystemAppProxy.removeEventListenerWithId(aWindowId,
                                                   "mozPresentationContentEvent",
                                                   handler);
          aReject();
        }
        if (evt.type === "mozPresentationContentEvent" &&
            evt.detail.id == aSessionId) {
          SystemAppProxy.removeEventListenerWithId(aWindowId,
                                                   "unload",
                                                   handler);
          SystemAppProxy.removeEventListenerWithId(aWindowId,
                                                   "mozPresentationContentEvent",
                                                   handler);
          this.appLaunchCallback(evt.detail, aResolve, aReject);
        }
      };
      // If system(-remote) app is closed.
      SystemAppProxy.addEventListenerWithId(aWindowId,
                                            "unload",
                                            handler);
      // Listen to the result for the opened iframe from front-end.
      SystemAppProxy.addEventListenerWithId(aWindowId,
                                            "mozPresentationContentEvent",
                                            handler);
      SystemAppProxy.sendCustomEventWithId(aWindowId,
                                           "mozPresentationChromeEvent",
                                           { type: "presentation-launch-receiver",
                                             url: aUrl,
                                             id: aSessionId });
    });
  },

  // For 2-UA scenario
  sendTo2UA: function(aUrl, aSessionId) {
    return new Promise((aResolve, aReject) => {
      let handler = (evt) => {
        if (evt.type === "mozPresentationContentEvent" &&
            evt.detail.id == aSessionId) {
          SystemAppProxy.removeEventListener("mozPresentationContentEvent",
                                            handler);
          this.appLaunchCallback(evt.detail, aResolve, aReject);
        }
      };

      // Listen to the result for the opened iframe from front-end.
      SystemAppProxy.addEventListener("mozPresentationContentEvent",
                                      handler);
      SystemAppProxy._sendCustomEvent("mozPresentationChromeEvent",
                                      { type: "presentation-launch-receiver",
                                        url: aUrl,
                                        id: aSessionId });
    });
  },

  appLaunchCallback: function(aDetail, aResolve, aReject) {
    switch(aDetail.type) {
      case "presentation-receiver-launched":
        aResolve(aDetail.frame);
        break;
      case "presentation-receiver-permission-denied":
        aReject();
        break;
    }
  },

  classID: Components.ID("{ccc8a839-0b64-422b-8a60-fb2af0e376d0}"),

  QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationRequestUIGlue])
};

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PresentationRequestUIGlue]);