summaryrefslogtreecommitdiffstats
path: root/dom/presentation/PresentationDeviceInfoManager.js
blob: 29e7d370cec5e454b5772ad45ea8c9b5f8eb6fa1 (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
/* 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;

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

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

const PRESENTATIONDEVICEINFOMANAGER_CID = Components.ID("{1bd66bef-f643-4be3-b690-0c656353eafd}");
const PRESENTATIONDEVICEINFOMANAGER_CONTRACTID = "@mozilla.org/presentation-device/deviceInfo;1";

XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
                                   "@mozilla.org/childprocessmessagemanager;1",
                                   "nsIMessageSender");

function PresentationDeviceInfoManager() {}

PresentationDeviceInfoManager.prototype = {
  __proto__: DOMRequestIpcHelper.prototype,

  classID: PRESENTATIONDEVICEINFOMANAGER_CID,
  contractID: PRESENTATIONDEVICEINFOMANAGER_CONTRACTID,
  QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference,
                                         Ci.nsIObserver,
                                         Ci.nsIDOMGlobalPropertyInitializer]),

  receiveMessage: function(aMsg) {
    if (!aMsg || !aMsg.data) {
      return;
    }

    let data = aMsg.data;

    log("receive aMsg: " + aMsg.name);
    switch (aMsg.name) {
      case "PresentationDeviceInfoManager:OnDeviceChange": {
        let detail = {
          detail: {
            type: data.type,
            deviceInfo: data.deviceInfo,
          }
        };
        let event = new this._window.CustomEvent("devicechange", Cu.cloneInto(detail, this._window));
        this.__DOM_IMPL__.dispatchEvent(event);
        break;
      }
      case "PresentationDeviceInfoManager:GetAll:Result:Ok": {
        let resolver = this.takePromiseResolver(data.requestId);

        if (!resolver) {
          return;
        }

        resolver.resolve(Cu.cloneInto(data.devices, this._window));
        break;
      }
      case "PresentationDeviceInfoManager:GetAll:Result:Error": {
        let resolver = this.takePromiseResolver(data.requestId);

        if (!resolver) {
          return;
        }

        resolver.reject(data.error);
        break;
      }
    }
  },

  init: function(aWin) {
    log("init");
    this.initDOMRequestHelper(aWin, [
      {name: "PresentationDeviceInfoManager:OnDeviceChange", weakRef: true},
      {name: "PresentationDeviceInfoManager:GetAll:Result:Ok", weakRef: true},
      {name: "PresentationDeviceInfoManager:GetAll:Result:Error", weakRef: true},
    ]);
  },

  uninit: function() {
    log("uninit");
    let self = this;

    this.forEachPromiseResolver(function(aKey) {
      self.takePromiseResolver(aKey).reject("PresentationDeviceInfoManager got destroyed");
    });
  },

  get ondevicechange() {
    return this.__DOM_IMPL__.getEventHandler("ondevicechange");
  },

  set ondevicechange(aHandler) {
    this.__DOM_IMPL__.setEventHandler("ondevicechange", aHandler);
  },

  getAll: function() {
    log("getAll");
    let self = this;
    return this.createPromiseWithId(function(aResolverId) {
      cpmm.sendAsyncMessage("PresentationDeviceInfoManager:GetAll", {
        requestId: aResolverId,
      });
    });
  },

  forceDiscovery: function() {
    cpmm.sendAsyncMessage("PresentationDeviceInfoManager:ForceDiscovery");
  },
};

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