|
| 1 | +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +/* import-globals-from fxrui.js */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Code to manage Permissions UI |
| 10 | + * |
| 11 | + * FxR on Desktop only supports granting permission for |
| 12 | + * - Location |
| 13 | + * - Camera |
| 14 | + * - Microphone |
| 15 | + * Any other permissions are automatically denied. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +// Base class for managing permissions in FxR on PC |
| 20 | +class FxrPermissionPromptPrototype { |
| 21 | + constructor(aRequest, aBrowser, aCallback) { |
| 22 | + this.request = aRequest; |
| 23 | + this.targetBrowser = aBrowser; |
| 24 | + this.responseCallback = aCallback; |
| 25 | + } |
| 26 | + |
| 27 | + showPrompt() { |
| 28 | + // For now, all permissions default to denied. Testing for allow must be |
| 29 | + // done manually until UI is finished: |
| 30 | + // Bug 1594840 - Add UI for Web Permissions in FxR for Desktop |
| 31 | + this.defaultDeny(); |
| 32 | + } |
| 33 | + |
| 34 | + defaultDeny() { |
| 35 | + this.handleResponse(false); |
| 36 | + } |
| 37 | + |
| 38 | + handleResponse(allowed) { |
| 39 | + if (allowed) { |
| 40 | + this.allow(); |
| 41 | + } else { |
| 42 | + this.deny(); |
| 43 | + } |
| 44 | + |
| 45 | + this.responseCallback(); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// WebRTC-specific class implementation |
| 50 | +class FxrWebRTCPrompt extends FxrPermissionPromptPrototype { |
| 51 | + showPrompt() { |
| 52 | + for (let typeName of this.request.requestTypes) { |
| 53 | + if (typeName !== "Microphone" && typeName !== "Camera") { |
| 54 | + // Only Microphone and Camera requests are allowed. Automatically deny |
| 55 | + // any other request. |
| 56 | + this.defaultDeny(); |
| 57 | + return; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + super.showPrompt(); |
| 62 | + } |
| 63 | + |
| 64 | + allow() { |
| 65 | + let { audioDevices, videoDevices } = this.request; |
| 66 | + |
| 67 | + let principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin( |
| 68 | + this.request.origin |
| 69 | + ); |
| 70 | + |
| 71 | + // For now, collect the first audio and video device by default. User |
| 72 | + // selection will be enabled later: |
| 73 | + // Bug 1594841 - Add UI to select device for WebRTC in FxR for Desktop |
| 74 | + let allowedDevices = []; |
| 75 | + |
| 76 | + if (audioDevices.length) { |
| 77 | + allowedDevices.push(audioDevices[0].deviceIndex); |
| 78 | + } |
| 79 | + |
| 80 | + if (videoDevices.length) { |
| 81 | + Services.perms.addFromPrincipal( |
| 82 | + principal, |
| 83 | + "MediaManagerVideo", |
| 84 | + Services.perms.ALLOW_ACTION, |
| 85 | + Services.perms.EXPIRE_SESSION |
| 86 | + ); |
| 87 | + allowedDevices.push(videoDevices[0].deviceIndex); |
| 88 | + } |
| 89 | + |
| 90 | + this.targetBrowser.messageManager.sendAsyncMessage("webrtc:Allow", { |
| 91 | + callID: this.request.callID, |
| 92 | + windowID: this.request.windowID, |
| 93 | + devices: allowedDevices, |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + deny() { |
| 98 | + this.targetBrowser.messageManager.sendAsyncMessage("webrtc:Deny", { |
| 99 | + callID: this.request.callID, |
| 100 | + windowID: this.request.windowID, |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Implementation for other, non-WebRTC web permission prompts |
| 106 | +class FxrContentPrompt extends FxrPermissionPromptPrototype { |
| 107 | + showPrompt() { |
| 108 | + // Only allow exactly one permission request here. |
| 109 | + let types = this.request.types.QueryInterface(Ci.nsIArray); |
| 110 | + if (types.length != 1) { |
| 111 | + this.defaultDeny(); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + // Only Location is supported from this type of request |
| 116 | + let type = types.queryElementAt(0, Ci.nsIContentPermissionType).type; |
| 117 | + if (type !== "geolocation") { |
| 118 | + this.defaultDeny(); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + // Override type so that it can be more easily interpreted by the code |
| 123 | + // for the prompt. |
| 124 | + type = "Location"; |
| 125 | + super.showPrompt(); |
| 126 | + } |
| 127 | + |
| 128 | + allow() { |
| 129 | + this.request.allow(); |
| 130 | + } |
| 131 | + |
| 132 | + deny() { |
| 133 | + this.request.cancel(); |
| 134 | + } |
| 135 | +} |
0 commit comments