tor-browser

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

GeckoViewPermissionProcessParent.sys.mjs (2138B)


      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
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 import { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs";
      6 
      7 // See: http://developer.android.com/reference/android/Manifest.permission.html
      8 const PERM_CAMERA = "android.permission.CAMERA";
      9 const PERM_RECORD_AUDIO = "android.permission.RECORD_AUDIO";
     10 
     11 export class GeckoViewPermissionProcessParent extends JSProcessActorParent {
     12  async askDevicePermission(aType) {
     13    const perms = [];
     14    if (aType === "video" || aType === "all") {
     15      perms.push(PERM_CAMERA);
     16    }
     17    if (aType === "audio" || aType === "all") {
     18      perms.push(PERM_RECORD_AUDIO);
     19    }
     20 
     21    try {
     22      // This looks sketchy but it's fine: Android needs the audio/video
     23      // permission to enumerate devices, which Gecko wants to do even before
     24      // we expose the list to web pages.
     25      // So really it doesn't matter what the request source is, because we
     26      // will, separately, issue a windowId-specific request to let the webpage
     27      // actually have access to the list of devices. So even if the source of
     28      // *this* request is incorrect, no actual harm will be done, as the user
     29      // will always receive the request with the right origin after this.
     30      const window = Services.wm.getMostRecentWindow("navigator:geckoview");
     31      const windowActor = window.browsingContext.currentWindowGlobal.getActor(
     32        "GeckoViewPermission"
     33      );
     34      await windowActor.getAppPermissions(perms);
     35    } catch (error) {
     36      // We can't really do anything here so we pretend we got the permission.
     37      warn`Error getting app permission: ${error}`;
     38    }
     39  }
     40 
     41  receiveMessage(aMessage) {
     42    debug`receiveMessage ${aMessage.name}`;
     43 
     44    switch (aMessage.name) {
     45      case "AskDevicePermission": {
     46        return this.askDevicePermission(aMessage.data);
     47      }
     48    }
     49 
     50    return super.receiveMessage(aMessage);
     51  }
     52 }
     53 
     54 const { debug, warn } = GeckoViewUtils.initLogging(
     55  "GeckoViewPermissionProcess"
     56 );