tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

permissions.js (3687B)


      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 =
     68      Services.scriptSecurityManager.createContentPrincipalFromOrigin(
     69        this.request.origin
     70      );
     71 
     72    // For now, collect the first audio and video device by default. User
     73    // selection will be enabled later:
     74    // Bug 1594841 - Add UI to select device for WebRTC in FxR for Desktop
     75    let allowedDevices = [];
     76 
     77    if (audioDevices.length) {
     78      allowedDevices.push(audioDevices[0].deviceIndex);
     79    }
     80 
     81    if (videoDevices.length) {
     82      Services.perms.addFromPrincipal(
     83        principal,
     84        "MediaManagerVideo",
     85        Services.perms.ALLOW_ACTION,
     86        Services.perms.EXPIRE_SESSION
     87      );
     88      allowedDevices.push(videoDevices[0].deviceIndex);
     89    }
     90 
     91    // WebRTCChild doesn't currently care which actor
     92    // this is sent to and just uses the windowID.
     93    this.targetBrowser.sendMessageToActor(
     94      "webrtc:Allow",
     95      {
     96        callID: this.request.callID,
     97        windowID: this.request.windowID,
     98        devices: allowedDevices,
     99      },
    100      "WebRTC"
    101    );
    102  }
    103 
    104  deny() {
    105    this.targetBrowser.sendMessageToActor(
    106      "webrtc:Deny",
    107      {
    108        callID: this.request.callID,
    109        windowID: this.request.windowID,
    110      },
    111      "WebRTC"
    112    );
    113  }
    114 }
    115 
    116 // Implementation for other, non-WebRTC web permission prompts
    117 class FxrContentPrompt extends FxrPermissionPromptPrototype {
    118  showPrompt() {
    119    // Only allow exactly one permission request here.
    120    let types = this.request.types.QueryInterface(Ci.nsIArray);
    121    if (types.length != 1) {
    122      this.defaultDeny();
    123      return;
    124    }
    125 
    126    // Only Location is supported from this type of request
    127    let type = types.queryElementAt(0, Ci.nsIContentPermissionType).type;
    128    if (type !== "geolocation") {
    129      this.defaultDeny();
    130      return;
    131    }
    132 
    133    // Override type so that it can be more easily interpreted by the code
    134    // for the prompt.
    135    type = "Location";
    136    super.showPrompt();
    137  }
    138 
    139  allow() {
    140    this.request.allow();
    141  }
    142 
    143  deny() {
    144    this.request.cancel();
    145  }
    146 }