tor-browser

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

GeckoViewPermissionParent.sys.mjs (2464B)


      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 import { GeckoViewActorParent } from "resource://gre/modules/GeckoViewActorParent.sys.mjs";
      7 
      8 export class GeckoViewPermissionParent extends GeckoViewActorParent {
      9  _appPermissions = {};
     10 
     11  async getAppPermissions(aPermissions) {
     12    const perms = aPermissions.filter(perm => !this._appPermissions[perm]);
     13    if (!perms.length) {
     14      return Promise.resolve(/* granted */ true);
     15    }
     16 
     17    const granted = await this.eventDispatcher.sendRequestForResult({
     18      type: "GeckoView:AndroidPermission",
     19      perms,
     20    });
     21 
     22    if (granted) {
     23      for (const perm of perms) {
     24        this._appPermissions[perm] = true;
     25      }
     26    }
     27 
     28    return granted;
     29  }
     30 
     31  addCameraPermission() {
     32    const principal =
     33      Services.scriptSecurityManager.createContentPrincipalFromOrigin(
     34        this.browsingContext.top.currentWindowGlobal.documentPrincipal.origin
     35      );
     36 
     37    // Although the lifetime is "session" it will be removed upon
     38    // use so it's more of a one-shot.
     39    Services.perms.addFromPrincipal(
     40      principal,
     41      "MediaManagerVideo",
     42      Services.perms.ALLOW_ACTION,
     43      Services.perms.EXPIRE_SESSION
     44    );
     45 
     46    return null;
     47  }
     48 
     49  receiveMessage(aMessage) {
     50    debug`receiveMessage ${aMessage.name}`;
     51 
     52    switch (aMessage.name) {
     53      case "GetAppPermissions": {
     54        return this.getAppPermissions(aMessage.data);
     55      }
     56      case "AddCameraPermission": {
     57        return this.addCameraPermission();
     58      }
     59      case "GeckoView:MediaPermission": {
     60        return this.eventDispatcher.sendRequestForResult({
     61          ...aMessage.data,
     62          type: "GeckoView:MediaPermission",
     63        });
     64      }
     65      case "GeckoView:MediaRecordingStatusChanged": {
     66        return this.eventDispatcher.sendRequest({
     67          ...aMessage.data,
     68          type: "GeckoView:MediaRecordingStatusChanged",
     69        });
     70      }
     71      case "GeckoView:ContentPermission": {
     72        return this.eventDispatcher.sendRequestForResult({
     73          ...aMessage.data,
     74          type: "GeckoView:ContentPermission",
     75        });
     76      }
     77    }
     78 
     79    return super.receiveMessage(aMessage);
     80  }
     81 }
     82 
     83 const { debug, warn } = GeckoViewUtils.initLogging("GeckoViewPermissionParent");