Bumping manifests a=b2g-bump
[gecko.git] / webapprt / WebRTCHandler.jsm
blob2f2ba8b517eb3233929f62e9803c86e36fbec039
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 this.EXPORTED_SYMBOLS = [];
9 let Cc = Components.classes;
10 let Ci = Components.interfaces;
11 let Cu = Components.utils;
13 Cu.import("resource://gre/modules/Services.jsm");
14 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
16 function handleRequest(aSubject, aTopic, aData) {
17   let { windowID, callID } = aSubject;
18   let constraints = aSubject.getConstraints();
19   let contentWindow = Services.wm.getOuterWindowWithId(windowID);
21   contentWindow.navigator.mozGetUserMediaDevices(
22     constraints,
23     function (devices) {
24       prompt(contentWindow, callID, constraints.audio,
25              constraints.video || constraints.picture,
26              devices);
27     },
28     function (error) {
29       denyRequest(callID, error);
30     });
33 function prompt(aWindow, aCallID, aAudioRequested, aVideoRequested, aDevices) {
34   let audioDevices = [];
35   let videoDevices = [];
36   for (let device of aDevices) {
37     device = device.QueryInterface(Ci.nsIMediaDevice);
38     switch (device.type) {
39       case "audio":
40         if (aAudioRequested) {
41           audioDevices.push(device);
42         }
43         break;
45       case "video":
46         if (aVideoRequested) {
47           videoDevices.push(device);
48         }
49         break;
50     }
51   }
53   if (audioDevices.length == 0 && videoDevices.length == 0) {
54     denyRequest(aCallID);
55     return;
56   }
58   let params = {
59                  videoDevices: videoDevices,
60                  audioDevices: audioDevices,
61                  out: null
62                };
63   aWindow.openDialog("chrome://webapprt/content/getUserMediaDialog.xul", "",
64                      "chrome, dialog, modal", params).focus();
66   if (!params.out) {
67     denyRequest(aCallID);
68     return;
69   }
71   let allowedDevices = Cc["@mozilla.org/supports-array;1"].
72                        createInstance(Ci.nsISupportsArray);
73   let videoIndex = params.out.video;
74   let audioIndex = params.out.audio;
76   if (videoIndex != -1) {
77     allowedDevices.AppendElement(videoDevices[videoIndex]);
78   }
80   if (audioIndex != -1) {
81     allowedDevices.AppendElement(audioDevices[audioIndex]);
82   }
84   if (allowedDevices.Count()) {
85     Services.obs.notifyObservers(allowedDevices, "getUserMedia:response:allow",
86                                  aCallID);
87   } else {
88     denyRequest(aCallID);
89   }
92 function denyRequest(aCallID, aError) {
93   let msg = null;
94   if (aError) {
95     msg = Cc["@mozilla.org/supports-string;1"].
96           createInstance(Ci.nsISupportsString);
97     msg.data = aError;
98   }
100   Services.obs.notifyObservers(msg, "getUserMedia:response:deny", aCallID);
103 Services.obs.addObserver(handleRequest, "getUserMedia:request", false);