summaryrefslogtreecommitdiffstats
path: root/dom/presentation/PresentationDeviceInfoManager.jsm
blob: 205982b9c03b263ef5625a88d18df84a02ca0d97 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* 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} = Components;

this.EXPORTED_SYMBOLS = ["PresentationDeviceInfoService"];

function log(aMsg) {
  //dump("PresentationDeviceInfoManager.jsm: " + aMsg + "\n");
}

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

XPCOMUtils.defineLazyServiceGetter(this, "presentationDeviceManager",
                                   "@mozilla.org/presentation-device/manager;1",
                                   "nsIPresentationDeviceManager");

XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
                                   "@mozilla.org/parentprocessmessagemanager;1",
                                   "nsIMessageBroadcaster");

this.PresentationDeviceInfoService = {
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIMessageListener,
                                         Ci.nsIObserver]),

  init: function() {
    log("init");
    ppmm.addMessageListener("PresentationDeviceInfoManager:GetAll", this);
    ppmm.addMessageListener("PresentationDeviceInfoManager:ForceDiscovery", this);
    Services.obs.addObserver(this, "presentation-device-change", false);
  },

  getAll: function(aData, aMm) {
    log("getAll");
    let deviceArray = presentationDeviceManager.getAvailableDevices().QueryInterface(Ci.nsIArray);
    if (!deviceArray) {
      aData.error = "DataError";
      aMm.sendAsyncMessage("PresentationDeviceInfoManager:GetAll:Result:Error", aData);
      return;
    }

    aData.devices = [];
    for (let i = 0; i < deviceArray.length; i++) {
      let device = deviceArray.queryElementAt(i, Ci.nsIPresentationDevice);
      aData.devices.push({
        id: device.id,
        name: device.name,
        type: device.type,
      });
    }
    aMm.sendAsyncMessage("PresentationDeviceInfoManager:GetAll:Result:Ok", aData);
  },

  forceDiscovery: function() {
    log("forceDiscovery");
    presentationDeviceManager.forceDiscovery();
  },

  observe: function(aSubject, aTopic, aData) {
    log("observe: " + aTopic);

    let device = aSubject.QueryInterface(Ci.nsIPresentationDevice);
    let data = {
      type: aData,
      deviceInfo: {
        id: device.id,
        name: device.name,
        type: device.type,
      },
    };
    ppmm.broadcastAsyncMessage("PresentationDeviceInfoManager:OnDeviceChange", data);
  },

  receiveMessage: function(aMessage) {
    if (!aMessage.target.assertPermission("presentation-device-manage")) {
      debug("receive message " + aMessage.name +
            " from a content process with no 'presentation-device-manage' privileges.");
      return null;
    }

    let msg = aMessage.data || {};
    let mm = aMessage.target;

    log("receiveMessage: " + aMessage.name);
    switch (aMessage.name) {
      case "PresentationDeviceInfoManager:GetAll": {
        this.getAll(msg, mm);
        break;
      }
      case "PresentationDeviceInfoManager:ForceDiscovery": {
        this.forceDiscovery();
        break;
      }
    }
  },
};

this.PresentationDeviceInfoService.init();