summaryrefslogtreecommitdiffstats
path: root/browser/fxr/content/permissions.js
blob: a8f7ece131f613581e5e817052efccc6a3d5ab8f (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
 * 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-globals-from fxrui.js */

/**
 * Code to manage Permissions UI
 *
 * FxR on Desktop only supports granting permission for
 * - Location
 * - Camera
 * - Microphone
 * Any other permissions are automatically denied.
 *
 */

// Base class for managing permissions in FxR on PC
class FxrPermissionPromptPrototype {
  constructor(aRequest, aBrowser, aCallback) {
    this.request = aRequest;
    this.targetBrowser = aBrowser;
    this.responseCallback = aCallback;
  }

  showPrompt() {
    // For now, all permissions default to denied. Testing for allow must be
    // done manually until UI is finished:
    // Bug 1594840 - Add UI for Web Permissions in FxR for Desktop
    this.defaultDeny();
  }

  defaultDeny() {
    this.handleResponse(false);
  }

  handleResponse(allowed) {
    if (allowed) {
      this.allow();
    } else {
      this.deny();
    }

    this.responseCallback();
  }
}

// WebRTC-specific class implementation
class FxrWebRTCPrompt extends FxrPermissionPromptPrototype {
  showPrompt() {
    for (let typeName of this.request.requestTypes) {
      if (typeName !== "Microphone" && typeName !== "Camera") {
        // Only Microphone and Camera requests are allowed. Automatically deny
        // any other request.
        this.defaultDeny();
        return;
      }
    }

    super.showPrompt();
  }

  allow() {
    let { audioDevices, videoDevices } = this.request;

    let principal =
      Services.scriptSecurityManager.createContentPrincipalFromOrigin(
        this.request.origin
      );

    // For now, collect the first audio and video device by default. User
    // selection will be enabled later:
    // Bug 1594841 - Add UI to select device for WebRTC in FxR for Desktop
    let allowedDevices = [];

    if (audioDevices.length) {
      allowedDevices.push(audioDevices[0].deviceIndex);
    }

    if (videoDevices.length) {
      Services.perms.addFromPrincipal(
        principal,
        "MediaManagerVideo",
        Services.perms.ALLOW_ACTION,
        Services.perms.EXPIRE_SESSION
      );
      allowedDevices.push(videoDevices[0].deviceIndex);
    }

    // WebRTCChild doesn't currently care which actor
    // this is sent to and just uses the windowID.
    this.targetBrowser.sendMessageToActor(
      "webrtc:Allow",
      {
        callID: this.request.callID,
        windowID: this.request.windowID,
        devices: allowedDevices,
      },
      "WebRTC"
    );
  }

  deny() {
    this.targetBrowser.sendMessageToActor(
      "webrtc:Deny",
      {
        callID: this.request.callID,
        windowID: this.request.windowID,
      },
      "WebRTC"
    );
  }
}

// Implementation for other, non-WebRTC web permission prompts
class FxrContentPrompt extends FxrPermissionPromptPrototype {
  showPrompt() {
    // Only allow exactly one permission request here.
    let types = this.request.types.QueryInterface(Ci.nsIArray);
    if (types.length != 1) {
      this.defaultDeny();
      return;
    }

    // Only Location is supported from this type of request
    let type = types.queryElementAt(0, Ci.nsIContentPermissionType).type;
    if (type !== "geolocation") {
      this.defaultDeny();
      return;
    }

    // Override type so that it can be more easily interpreted by the code
    // for the prompt.
    type = "Location";
    super.showPrompt();
  }

  allow() {
    this.request.allow();
  }

  deny() {
    this.request.cancel();
  }
}