summaryrefslogtreecommitdiffstats
path: root/mobile/android/components/PresentationDevicePrompt.js
blob: e3e06337366df639ca04c4b7ccfdd846fda59e7d (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* 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";

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

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

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

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

const kPRESENTATIONDEVICEPROMPT_CONTRACTID = "@mozilla.org/presentation-device/prompt;1";
const kPRESENTATIONDEVICEPROMPT_CID        = Components.ID("{388bd149-c919-4a43-b646-d7ec57877689}");

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

// nsIPresentationDevicePrompt
function PresentationDevicePrompt() {
  debug("PresentationDevicePrompt init");
}

PresentationDevicePrompt.prototype = {
  classID: kPRESENTATIONDEVICEPROMPT_CID,
  contractID: kPRESENTATIONDEVICEPROMPT_CONTRACTID,
  classDescription: "Fennec Presentation Device Prompt",
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationDevicePrompt]),

  _devices: [],   // Store all available presentation devices
  _request: null, // Store the request from presentation api

  _getString: function(aName) {
    debug("_getString");

    if (!this.bundle) {
      this.bundle = Services.strings.createBundle("chrome://browser/locale/devicePrompt.properties");
    }
    return this.bundle.GetStringFromName(aName);
  },

  _loadDevices: function(requestURLs) {
    debug("_loadDevices");

    let deviceManager = Cc["@mozilla.org/presentation-device/manager;1"]
                          .getService(Ci.nsIPresentationDeviceManager);
    let devices = deviceManager.getAvailableDevices(requestURLs).QueryInterface(Ci.nsIArray);

    // Re-load the available devices
    this._devices = [];
    for (let i = 0; i < devices.length; i++) {
      let device = devices.queryElementAt(i, Ci.nsIPresentationDevice);
      this._devices.push(device);
    }
  },

  _getPromptMenu: function(aDevices) {
    debug("_getPromptMenu");

    return aDevices.map(function(device) {
      return { label: device.name };
    });
  },

  _getPrompt: function(aTitle, aMenu) {
    debug("_getPrompt");

    let p = new Prompt({
      title: aTitle,
    });

    p.setSingleChoiceItems(aMenu);

    return p;
  },

  _showPrompt: function(aPrompt, aCallback) {
    debug("_showPrompt");

    aPrompt.show(function(data) {
      let buttonIndex = data.button;
      aCallback(buttonIndex);
    });
  },

  _selectDevice: function(aIndex) {
    debug("_selectDevice");

    if (!this._request) {
      return;
    }

    if (aIndex < 0) {                    // Cancel request if no selected device,
      this._request.cancel(Cr.NS_ERROR_DOM_NOT_ALLOWED_ERR);
      return;
    } else if (!this._devices.length) {  // or there is no available devices
      this._request.cancel(Cr.NS_ERROR_DOM_NOT_FOUND_ERR);
      return;
    }

    this._request.select(this._devices[aIndex]);
  },

  // This will be fired when window.PresentationRequest(URL).start() is called
  promptDeviceSelection: function(aRequest) {
    debug("promptDeviceSelection");

    // Load available presentation devices into this._devices
    this._loadDevices(aRequest.requestURLs);

    if (!this._devices.length) { // Cancel request if no available device
      aRequest.cancel(Cr.NS_ERROR_DOM_NOT_FOUND_ERR);
      return;
    }

    this._request = aRequest;

    let prompt = this._getPrompt(this._getString("deviceMenu.title"),
                                 this._getPromptMenu(this._devices));

    this._showPrompt(prompt, this._selectDevice.bind(this));

    UITelemetry.addEvent("show.1", "dialog", null, "prompt_device_selection");
  },
};

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