summaryrefslogtreecommitdiffstats
path: root/mobile/android/actors/GeckoViewPermissionProcessParent.sys.mjs
blob: 47b98602a5a88d56c1a5c22ef4a77fadef61cdf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs";

// See: http://developer.android.com/reference/android/Manifest.permission.html
const PERM_CAMERA = "android.permission.CAMERA";
const PERM_RECORD_AUDIO = "android.permission.RECORD_AUDIO";

export class GeckoViewPermissionProcessParent extends JSProcessActorParent {
  async askDevicePermission(aType) {
    const perms = [];
    if (aType === "video" || aType === "all") {
      perms.push(PERM_CAMERA);
    }
    if (aType === "audio" || aType === "all") {
      perms.push(PERM_RECORD_AUDIO);
    }

    try {
      // This looks sketchy but it's fine: Android needs the audio/video
      // permission to enumerate devices, which Gecko wants to do even before
      // we expose the list to web pages.
      // So really it doesn't matter what the request source is, because we
      // will, separately, issue a windowId-specific request to let the webpage
      // actually have access to the list of devices. So even if the source of
      // *this* request is incorrect, no actual harm will be done, as the user
      // will always receive the request with the right origin after this.
      const window = Services.wm.getMostRecentWindow("navigator:geckoview");
      const windowActor = window.browsingContext.currentWindowGlobal.getActor(
        "GeckoViewPermission"
      );
      await windowActor.getAppPermissions(perms);
    } catch (error) {
      // We can't really do anything here so we pretend we got the permission.
      warn`Error getting app permission: ${error}`;
    }
  }

  receiveMessage(aMessage) {
    debug`receiveMessage ${aMessage.name}`;

    switch (aMessage.name) {
      case "AskDevicePermission": {
        return this.askDevicePermission(aMessage.data);
      }
    }

    return super.receiveMessage(aMessage);
  }
}

const { debug, warn } = GeckoViewUtils.initLogging(
  "GeckoViewPermissionProcess"
);