michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = []; michael@0: michael@0: let Cc = Components.classes; michael@0: let Ci = Components.interfaces; michael@0: let Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: function handleRequest(aSubject, aTopic, aData) { michael@0: let { windowID, callID } = aSubject; michael@0: let constraints = aSubject.getConstraints(); michael@0: let contentWindow = Services.wm.getOuterWindowWithId(windowID); michael@0: michael@0: contentWindow.navigator.mozGetUserMediaDevices( michael@0: constraints, michael@0: function (devices) { michael@0: prompt(contentWindow, callID, constraints.audio, michael@0: constraints.video || constraints.picture, michael@0: devices); michael@0: }, michael@0: function (error) { michael@0: denyRequest(callID, error); michael@0: }); michael@0: } michael@0: michael@0: function prompt(aWindow, aCallID, aAudioRequested, aVideoRequested, aDevices) { michael@0: let audioDevices = []; michael@0: let videoDevices = []; michael@0: for (let device of aDevices) { michael@0: device = device.QueryInterface(Ci.nsIMediaDevice); michael@0: switch (device.type) { michael@0: case "audio": michael@0: if (aAudioRequested) { michael@0: audioDevices.push(device); michael@0: } michael@0: break; michael@0: michael@0: case "video": michael@0: if (aVideoRequested) { michael@0: videoDevices.push(device); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (audioDevices.length == 0 && videoDevices.length == 0) { michael@0: denyRequest(aCallID); michael@0: return; michael@0: } michael@0: michael@0: let params = { michael@0: videoDevices: videoDevices, michael@0: audioDevices: audioDevices, michael@0: out: null michael@0: }; michael@0: aWindow.openDialog("chrome://webapprt/content/getUserMediaDialog.xul", "", michael@0: "chrome, dialog, modal", params).focus(); michael@0: michael@0: if (!params.out) { michael@0: denyRequest(aCallID); michael@0: return; michael@0: } michael@0: michael@0: let allowedDevices = Cc["@mozilla.org/supports-array;1"]. michael@0: createInstance(Ci.nsISupportsArray); michael@0: let videoIndex = params.out.video; michael@0: let audioIndex = params.out.audio; michael@0: michael@0: if (videoIndex != -1) { michael@0: allowedDevices.AppendElement(videoDevices[videoIndex]); michael@0: } michael@0: michael@0: if (audioIndex != -1) { michael@0: allowedDevices.AppendElement(audioDevices[audioIndex]); michael@0: } michael@0: michael@0: if (allowedDevices.Count()) { michael@0: Services.obs.notifyObservers(allowedDevices, "getUserMedia:response:allow", michael@0: aCallID); michael@0: } else { michael@0: denyRequest(aCallID); michael@0: } michael@0: } michael@0: michael@0: function denyRequest(aCallID, aError) { michael@0: let msg = null; michael@0: if (aError) { michael@0: msg = Cc["@mozilla.org/supports-string;1"]. michael@0: createInstance(Ci.nsISupportsString); michael@0: msg.data = aError; michael@0: } michael@0: michael@0: Services.obs.notifyObservers(msg, "getUserMedia:response:deny", aCallID); michael@0: } michael@0: michael@0: Services.obs.addObserver(handleRequest, "getUserMedia:request", false);