summaryrefslogtreecommitdiffstats
path: root/mobile/android/actors/GeckoViewPermissionProcessChild.sys.mjs
blob: 2c9d271bbd091f334b5833761784880b68e8fdca (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* 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";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

const lazy = {};

XPCOMUtils.defineLazyServiceGetter(
  lazy,
  "MediaManagerService",
  "@mozilla.org/mediaManagerService;1",
  "nsIMediaManagerService"
);

const STATUS_RECORDING = "recording";
const STATUS_INACTIVE = "inactive";
const TYPE_CAMERA = "camera";
const TYPE_MICROPHONE = "microphone";

export class GeckoViewPermissionProcessChild extends JSProcessActorChild {
  getActor(window) {
    return window.windowGlobalChild.getActor("GeckoViewPermission");
  }

  /* ----------  nsIObserver  ---------- */
  async observe(aSubject, aTopic, aData) {
    switch (aTopic) {
      case "getUserMedia:ask-device-permission": {
        await this.sendQuery("AskDevicePermission", aData);
        Services.obs.notifyObservers(
          aSubject,
          "getUserMedia:got-device-permission"
        );
        break;
      }
      case "getUserMedia:request": {
        const { callID } = aSubject;
        const allowedDevices = await this.handleMediaRequest(aSubject);
        Services.obs.notifyObservers(
          allowedDevices,
          allowedDevices
            ? "getUserMedia:response:allow"
            : "getUserMedia:response:deny",
          callID
        );
        break;
      }
      case "PeerConnection:request": {
        Services.obs.notifyObservers(
          null,
          "PeerConnection:response:allow",
          aSubject.callID
        );
        break;
      }
      case "recording-device-events": {
        this.handleRecordingDeviceEvents(aSubject);
        break;
      }
    }
  }

  handleRecordingDeviceEvents(aRequest) {
    aRequest.QueryInterface(Ci.nsIPropertyBag2);
    const contentWindow = aRequest.getProperty("window");
    const devices = [];

    const getStatusString = function (activityStatus) {
      switch (activityStatus) {
        case lazy.MediaManagerService.STATE_CAPTURE_ENABLED:
        case lazy.MediaManagerService.STATE_CAPTURE_DISABLED:
          return STATUS_RECORDING;
        case lazy.MediaManagerService.STATE_NOCAPTURE:
          return STATUS_INACTIVE;
        default:
          throw new Error("Unexpected activityStatus value");
      }
    };

    const hasCamera = {};
    const hasMicrophone = {};
    const screen = {};
    const window = {};
    const browser = {};
    const mediaDevices = {};
    lazy.MediaManagerService.mediaCaptureWindowState(
      contentWindow,
      hasCamera,
      hasMicrophone,
      screen,
      window,
      browser,
      mediaDevices
    );
    var cameraStatus = getStatusString(hasCamera.value);
    var microphoneStatus = getStatusString(hasMicrophone.value);
    if (hasCamera.value != lazy.MediaManagerService.STATE_NOCAPTURE) {
      devices.push({
        type: TYPE_CAMERA,
        status: cameraStatus,
      });
    }
    if (hasMicrophone.value != lazy.MediaManagerService.STATE_NOCAPTURE) {
      devices.push({
        type: TYPE_MICROPHONE,
        status: microphoneStatus,
      });
    }
    this.getActor(contentWindow).mediaRecordingStatusChanged(devices);
  }

  async handleMediaRequest(aRequest) {
    const constraints = aRequest.getConstraints();
    const { devices, windowID } = aRequest;
    const window = Services.wm.getOuterWindowWithId(windowID);
    if (window.closed) {
      return null;
    }

    // Release the request first
    aRequest = undefined;

    const sources = devices.map(device => {
      device = device.QueryInterface(Ci.nsIMediaDevice);
      return {
        type: device.type,
        id: device.rawId,
        rawId: device.rawId,
        name: device.rawName, // unfiltered device name to show to the user
        mediaSource: device.mediaSource,
      };
    });

    if (
      constraints.video &&
      !sources.some(source => source.type === "videoinput")
    ) {
      console.error("Media device error: no video source");
      return null;
    } else if (
      constraints.audio &&
      !sources.some(source => source.type === "audioinput")
    ) {
      console.error("Media device error: no audio source");
      return null;
    }

    const response = await this.getActor(window).getMediaPermission({
      uri: window.document.documentURI,
      video: constraints.video
        ? sources.filter(source => source.type === "videoinput")
        : null,
      audio: constraints.audio
        ? sources.filter(source => source.type === "audioinput")
        : null,
    });

    if (!response) {
      // Rejected.
      return null;
    }

    const allowedDevices = Cc["@mozilla.org/array;1"].createInstance(
      Ci.nsIMutableArray
    );
    if (constraints.video) {
      const video = devices.find(device => response.video === device.rawId);
      if (!video) {
        console.error("Media device error: invalid video id");
        return null;
      }
      await this.getActor(window).addCameraPermission();
      allowedDevices.appendElement(video);
    }
    if (constraints.audio) {
      const audio = devices.find(device => response.audio === device.rawId);
      if (!audio) {
        console.error("Media device error: invalid audio id");
        return null;
      }
      allowedDevices.appendElement(audio);
    }
    return allowedDevices;
  }
}

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