Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | XPCOMUtils.defineLazyModuleGetter(this, "Notifications", "resource://gre/modules/Notifications.jsm"); |
michael@0 | 7 | |
michael@0 | 8 | var WebrtcUI = { |
michael@0 | 9 | _notificationId: null, |
michael@0 | 10 | |
michael@0 | 11 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 12 | if (aTopic === "getUserMedia:request") { |
michael@0 | 13 | this.handleRequest(aSubject, aTopic, aData); |
michael@0 | 14 | } else if (aTopic === "recording-device-events") { |
michael@0 | 15 | switch (aData) { |
michael@0 | 16 | case "shutdown": |
michael@0 | 17 | case "starting": |
michael@0 | 18 | this.notify(); |
michael@0 | 19 | break; |
michael@0 | 20 | } |
michael@0 | 21 | } |
michael@0 | 22 | }, |
michael@0 | 23 | |
michael@0 | 24 | notify: function() { |
michael@0 | 25 | let windows = MediaManagerService.activeMediaCaptureWindows; |
michael@0 | 26 | let count = windows.Count(); |
michael@0 | 27 | let msg = {}; |
michael@0 | 28 | if (count == 0) { |
michael@0 | 29 | if (this._notificationId) { |
michael@0 | 30 | Notifications.cancel(this._notificationId); |
michael@0 | 31 | this._notificationId = null; |
michael@0 | 32 | } |
michael@0 | 33 | } else { |
michael@0 | 34 | let notificationOptions = { |
michael@0 | 35 | title: Strings.brand.GetStringFromName("brandShortName"), |
michael@0 | 36 | when: null, // hide the date row |
michael@0 | 37 | light: [0xFF9500FF, 1000, 1000], |
michael@0 | 38 | ongoing: true |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | let cameraActive = false; |
michael@0 | 42 | let audioActive = false; |
michael@0 | 43 | for (let i = 0; i < count; i++) { |
michael@0 | 44 | let win = windows.GetElementAt(i); |
michael@0 | 45 | let hasAudio = {}; |
michael@0 | 46 | let hasVideo = {}; |
michael@0 | 47 | MediaManagerService.mediaCaptureWindowState(win, hasVideo, hasAudio); |
michael@0 | 48 | if (hasVideo.value) cameraActive = true; |
michael@0 | 49 | if (hasAudio.value) audioActive = true; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | if (cameraActive && audioActive) { |
michael@0 | 53 | notificationOptions.message = Strings.browser.GetStringFromName("getUserMedia.sharingCameraAndMicrophone.message2"); |
michael@0 | 54 | notificationOptions.icon = "drawable:alert_mic_camera"; |
michael@0 | 55 | } else if (cameraActive) { |
michael@0 | 56 | notificationOptions.message = Strings.browser.GetStringFromName("getUserMedia.sharingCamera.message2"); |
michael@0 | 57 | notificationOptions.icon = "drawable:alert_camera"; |
michael@0 | 58 | } else if (audioActive) { |
michael@0 | 59 | notificationOptions.message = Strings.browser.GetStringFromName("getUserMedia.sharingMicrophone.message2"); |
michael@0 | 60 | notificationOptions.icon = "drawable:alert_mic"; |
michael@0 | 61 | } else { |
michael@0 | 62 | // somethings wrong. lets throw |
michael@0 | 63 | throw "Couldn't find any cameras or microphones being used" |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | if (this._notificationId) |
michael@0 | 67 | Notifications.update(this._notificationId, notificationOptions); |
michael@0 | 68 | else |
michael@0 | 69 | this._notificationId = Notifications.create(notificationOptions); |
michael@0 | 70 | if (count > 1) |
michael@0 | 71 | msg.count = count; |
michael@0 | 72 | } |
michael@0 | 73 | }, |
michael@0 | 74 | |
michael@0 | 75 | handleRequest: function handleRequest(aSubject, aTopic, aData) { |
michael@0 | 76 | let constraints = aSubject.getConstraints(); |
michael@0 | 77 | let contentWindow = Services.wm.getOuterWindowWithId(aSubject.windowID); |
michael@0 | 78 | |
michael@0 | 79 | contentWindow.navigator.mozGetUserMediaDevices( |
michael@0 | 80 | constraints, |
michael@0 | 81 | function (devices) { |
michael@0 | 82 | WebrtcUI.prompt(contentWindow, aSubject.callID, constraints.audio, |
michael@0 | 83 | constraints.video, devices); |
michael@0 | 84 | }, |
michael@0 | 85 | function (error) { |
michael@0 | 86 | Cu.reportError(error); |
michael@0 | 87 | }, |
michael@0 | 88 | aSubject.innerWindowID); |
michael@0 | 89 | }, |
michael@0 | 90 | |
michael@0 | 91 | getDeviceButtons: function(audioDevices, videoDevices, aCallID) { |
michael@0 | 92 | return [{ |
michael@0 | 93 | label: Strings.browser.GetStringFromName("getUserMedia.denyRequest.label"), |
michael@0 | 94 | callback: function() { |
michael@0 | 95 | Services.obs.notifyObservers(null, "getUserMedia:response:deny", aCallID); |
michael@0 | 96 | } |
michael@0 | 97 | }, |
michael@0 | 98 | { |
michael@0 | 99 | label: Strings.browser.GetStringFromName("getUserMedia.shareRequest.label"), |
michael@0 | 100 | callback: function(checked /* ignored */, inputs) { |
michael@0 | 101 | let allowedDevices = Cc["@mozilla.org/supports-array;1"].createInstance(Ci.nsISupportsArray); |
michael@0 | 102 | |
michael@0 | 103 | let audioId = 0; |
michael@0 | 104 | if (inputs && inputs.audioDevice != undefined) |
michael@0 | 105 | audioId = inputs.audioDevice; |
michael@0 | 106 | if (audioDevices[audioId]) |
michael@0 | 107 | allowedDevices.AppendElement(audioDevices[audioId]); |
michael@0 | 108 | |
michael@0 | 109 | let videoId = 0; |
michael@0 | 110 | if (inputs && inputs.videoDevice != undefined) |
michael@0 | 111 | videoId = inputs.videoDevice; |
michael@0 | 112 | if (videoDevices[videoId]) |
michael@0 | 113 | allowedDevices.AppendElement(videoDevices[videoId]); |
michael@0 | 114 | |
michael@0 | 115 | Services.obs.notifyObservers(allowedDevices, "getUserMedia:response:allow", aCallID); |
michael@0 | 116 | } |
michael@0 | 117 | }]; |
michael@0 | 118 | }, |
michael@0 | 119 | |
michael@0 | 120 | // Get a list of string names for devices. Ensures that none of the strings are blank |
michael@0 | 121 | _getList: function(aDevices, aType) { |
michael@0 | 122 | let defaultCount = 0; |
michael@0 | 123 | return aDevices.map(function(device) { |
michael@0 | 124 | // if this is a Camera input, convert the name to something readable |
michael@0 | 125 | let res = /Camera\ \d+,\ Facing (front|back)/.exec(device.name); |
michael@0 | 126 | if (res) |
michael@0 | 127 | return Strings.browser.GetStringFromName("getUserMedia." + aType + "." + res[1] + "Camera"); |
michael@0 | 128 | |
michael@0 | 129 | if (device.name.startsWith("&") && device.name.endsWith(";")) |
michael@0 | 130 | return Strings.browser.GetStringFromName(device.name.substring(1, device.name.length -1)); |
michael@0 | 131 | |
michael@0 | 132 | if (device.name.trim() == "") { |
michael@0 | 133 | defaultCount++; |
michael@0 | 134 | return Strings.browser.formatStringFromName("getUserMedia." + aType + ".default", [defaultCount], 1); |
michael@0 | 135 | } |
michael@0 | 136 | return device.name |
michael@0 | 137 | }, this); |
michael@0 | 138 | }, |
michael@0 | 139 | |
michael@0 | 140 | _addDevicesToOptions: function(aDevices, aType, aOptions, extraOptions) { |
michael@0 | 141 | if (aDevices.length) { |
michael@0 | 142 | |
michael@0 | 143 | // Filter out empty items from the list |
michael@0 | 144 | let list = this._getList(aDevices, aType); |
michael@0 | 145 | if (extraOptions) |
michael@0 | 146 | list = list.concat(extraOptions); |
michael@0 | 147 | |
michael@0 | 148 | if (list.length > 0) { |
michael@0 | 149 | aOptions.inputs.push({ |
michael@0 | 150 | id: aType, |
michael@0 | 151 | type: "menulist", |
michael@0 | 152 | label: Strings.browser.GetStringFromName("getUserMedia." + aType + ".prompt"), |
michael@0 | 153 | values: list |
michael@0 | 154 | }); |
michael@0 | 155 | |
michael@0 | 156 | } |
michael@0 | 157 | } |
michael@0 | 158 | }, |
michael@0 | 159 | |
michael@0 | 160 | prompt: function prompt(aContentWindow, aCallID, aAudioRequested, |
michael@0 | 161 | aVideoRequested, aDevices) { |
michael@0 | 162 | let audioDevices = []; |
michael@0 | 163 | let videoDevices = []; |
michael@0 | 164 | for (let device of aDevices) { |
michael@0 | 165 | device = device.QueryInterface(Ci.nsIMediaDevice); |
michael@0 | 166 | switch (device.type) { |
michael@0 | 167 | case "audio": |
michael@0 | 168 | if (aAudioRequested) |
michael@0 | 169 | audioDevices.push(device); |
michael@0 | 170 | break; |
michael@0 | 171 | case "video": |
michael@0 | 172 | if (aVideoRequested) |
michael@0 | 173 | videoDevices.push(device); |
michael@0 | 174 | break; |
michael@0 | 175 | } |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | let requestType; |
michael@0 | 179 | if (audioDevices.length && videoDevices.length) |
michael@0 | 180 | requestType = "CameraAndMicrophone"; |
michael@0 | 181 | else if (audioDevices.length) |
michael@0 | 182 | requestType = "Microphone"; |
michael@0 | 183 | else if (videoDevices.length) |
michael@0 | 184 | requestType = "Camera"; |
michael@0 | 185 | else |
michael@0 | 186 | return; |
michael@0 | 187 | |
michael@0 | 188 | let host = aContentWindow.document.documentURIObject.host; |
michael@0 | 189 | let requestor = BrowserApp.manifest ? "'" + BrowserApp.manifest.name + "'" : host; |
michael@0 | 190 | let message = Strings.browser.formatStringFromName("getUserMedia.share" + requestType + ".message", [ requestor ], 1); |
michael@0 | 191 | |
michael@0 | 192 | let options = { inputs: [] }; |
michael@0 | 193 | // if the users only option would be to select "No Audio" or "No Video" |
michael@0 | 194 | // i.e. we're only showing audio or only video and there is only one device for that type |
michael@0 | 195 | // don't bother showing a menulist to select from |
michael@0 | 196 | var extraItems = null; |
michael@0 | 197 | if (videoDevices.length > 1 || audioDevices.length > 0) { |
michael@0 | 198 | // Only show the No Video option if there are also Audio devices to choose from |
michael@0 | 199 | if (audioDevices.length > 0) |
michael@0 | 200 | extraItems = [ Strings.browser.GetStringFromName("getUserMedia.videoDevice.none") ]; |
michael@0 | 201 | this._addDevicesToOptions(videoDevices, "videoDevice", options, extraItems); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | if (audioDevices.length > 1 || videoDevices.length > 0) { |
michael@0 | 205 | // Only show the No Audio option if there are also Video devices to choose from |
michael@0 | 206 | if (videoDevices.length > 0) |
michael@0 | 207 | extraItems = [ Strings.browser.GetStringFromName("getUserMedia.audioDevice.none") ]; |
michael@0 | 208 | this._addDevicesToOptions(audioDevices, "audioDevice", options, extraItems); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | let buttons = this.getDeviceButtons(audioDevices, videoDevices, aCallID); |
michael@0 | 212 | |
michael@0 | 213 | NativeWindow.doorhanger.show(message, "webrtc-request", buttons, BrowserApp.selectedTab.id, options); |
michael@0 | 214 | } |
michael@0 | 215 | } |