summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_devices_get_user_media_grace.js
blob: 0df69bb9dadb6bfddb3933b45789bf435071dec8 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* 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/. */

"use strict";

requestLongerTimeout(2);

const permissionError =
  "error: NotAllowedError: The request is not allowed " +
  "by the user agent or the platform in the current context.";

const SAME_ORIGIN = "https://example.com";
const CROSS_ORIGIN = "https://example.org";

const PATH = "/browser/browser/base/content/test/webrtc/get_user_media.html";
const PATH2 = "/browser/browser/base/content/test/webrtc/get_user_media2.html";

const GRACE_PERIOD_MS = 3000;
const WAIT_PERIOD_MS = GRACE_PERIOD_MS + 500;

// We're inherently testing timeouts (grace periods)
/* eslint-disable mozilla/no-arbitrary-setTimeout */
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
const perms = SitePermissions;

// These tests focus on camera and microphone, so we define some helpers.

async function prompt(audio, video) {
  let observerPromise = expectObserverCalled("getUserMedia:request");
  let promise = promisePopupNotificationShown("webRTC-shareDevices");
  await promiseRequestDevice(audio, video);
  await promise;
  await observerPromise;
  const expectedDeviceSelectorTypes = [
    audio && "microphone",
    video && "camera",
  ].filter(x => x);
  checkDeviceSelectors(expectedDeviceSelectorTypes);
}

async function allow(audio, video) {
  let indicator = promiseIndicatorWindow();
  let observerPromise1 = expectObserverCalled("getUserMedia:response:allow");
  let observerPromise2 = expectObserverCalled("recording-device-events");
  await promiseMessage("ok", () => {
    PopupNotifications.panel.firstElementChild.button.click();
  });
  await observerPromise1;
  await observerPromise2;
  Assert.deepEqual(
    Object.assign({ audio: false, video: false }, await getMediaCaptureState()),
    { audio, video },
    `expected ${video ? "camera " : ""} ${audio ? "microphone " : ""}shared`
  );
  await indicator;
  await checkSharingUI({ audio, video });
}

async function deny(action) {
  let observerPromise1 = expectObserverCalled("getUserMedia:response:deny");
  let observerPromise2 = expectObserverCalled("recording-window-ended");
  await promiseMessage(permissionError, () => {
    activateSecondaryAction(action);
  });
  await observerPromise1;
  await observerPromise2;
  await checkNotSharing();
}

async function noPrompt(audio, video) {
  let observerPromises = [
    expectObserverCalled("getUserMedia:request"),
    expectObserverCalled("getUserMedia:response:allow"),
    expectObserverCalled("recording-device-events"),
  ];
  let promise = promiseMessage("ok");
  await promiseRequestDevice(audio, video);
  await promise;
  await Promise.all(observerPromises);
  await promiseNoPopupNotification("webRTC-shareDevices");
  Assert.deepEqual(
    Object.assign({ audio: false, video: false }, await getMediaCaptureState()),
    { audio, video },
    `expected ${video ? "camera " : ""} ${audio ? "microphone " : ""}shared`
  );
  await checkSharingUI({ audio, video });
}

async function navigate(browser, url) {
  await disableObserverVerification();
  let loaded = BrowserTestUtils.browserLoaded(browser, false, url);
  await SpecialPowers.spawn(
    browser,
    [url],
    u => (content.document.location = u)
  );
  await loaded;
  await enableObserverVerification();
}

var gTests = [
  {
    desc: "getUserMedia camera+mic survives track.stop but not past grace",
    run: async function checkAudioVideoGracePastStop() {
      await prompt(true, true);
      await allow(true, true);

      info(
        "After closing all streams, gUM(camera+mic) returns a stream " +
          "without prompting within grace period."
      );
      await closeStream();
      await checkNotSharingWithinGracePeriod();
      await noPrompt(true, true);

      info(
        "After closing all streams, gUM(mic) returns a stream " +
          "without prompting within grace period."
      );
      await closeStream();
      await checkNotSharingWithinGracePeriod();
      await noPrompt(true, false);

      info(
        "After closing all streams, gUM(camera) returns a stream " +
          "without prompting within grace period."
      );
      await closeStream();
      await checkNotSharingWithinGracePeriod();
      await noPrompt(false, true);

      info("gUM(screen) still causes a prompt.");
      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "screen");
      await promise;
      await observerPromise;

      is(
        PopupNotifications.getNotification("webRTC-shareDevices").anchorID,
        "webRTC-shareScreen-notification-icon",
        "anchored to device icon"
      );
      checkDeviceSelectors(["screen"]);

      observerPromise = expectObserverCalled("getUserMedia:response:deny");
      await promiseMessage(permissionError, () => {
        activateSecondaryAction(kActionDeny);
      });
      await observerPromise;
      perms.removeFromPrincipal(null, "screen", gBrowser.selectedBrowser);

      await closeStream();
      info("Closed stream. Waiting past grace period.");
      await checkNotSharingWithinGracePeriod();
      await wait(WAIT_PERIOD_MS);
      await checkNotSharing();

      info("After grace period expires, gUM(camera) causes a prompt.");
      await prompt(false, true);
      await deny(kActionDeny);
      perms.removeFromPrincipal(null, "camera", gBrowser.selectedBrowser);

      info("After grace period expires, gUM(mic) causes a prompt.");
      await prompt(true, false);
      await deny(kActionDeny);
      perms.removeFromPrincipal(null, "microphone", gBrowser.selectedBrowser);
    },
  },

  {
    desc: "getUserMedia camera+mic survives page reload but not past grace",
    run: async function checkAudioVideoGracePastReload(browser) {
      await prompt(true, true);
      await allow(true, true);
      await closeStream();

      await reloadFromContent();
      info(
        "After page reload, gUM(camera+mic) returns a stream " +
          "without prompting within grace period."
      );
      await checkNotSharingWithinGracePeriod();
      await noPrompt(true, true);
      await closeStream();

      await reloadAsUser();
      info(
        "After user page reload, gUM(camera+mic) returns a stream " +
          "without prompting within grace period."
      );
      await checkNotSharingWithinGracePeriod();
      await noPrompt(true, true);

      info("gUM(screen) still causes a prompt.");
      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, null, "screen");
      await promise;
      await observerPromise;

      is(
        PopupNotifications.getNotification("webRTC-shareDevices").anchorID,
        "webRTC-shareScreen-notification-icon",
        "anchored to device icon"
      );
      checkDeviceSelectors(["screen"]);

      observerPromise = expectObserverCalled("getUserMedia:response:deny");
      await promiseMessage(permissionError, () => {
        activateSecondaryAction(kActionDeny);
      });
      await observerPromise;
      perms.removeFromPrincipal(null, "screen", gBrowser.selectedBrowser);

      await closeStream();
      info("Closed stream. Waiting past grace period.");
      await checkNotSharingWithinGracePeriod();
      await wait(WAIT_PERIOD_MS);
      await checkNotSharing();

      info("After grace period expires, gUM(camera) causes a prompt.");
      await prompt(false, true);
      await deny(kActionDeny);
      perms.removeFromPrincipal(null, "camera", gBrowser.selectedBrowser);

      info("After grace period expires, gUM(mic) causes a prompt.");
      await prompt(true, false);
      await deny(kActionDeny);
      perms.removeFromPrincipal(null, "microphone", gBrowser.selectedBrowser);
    },
  },

  {
    desc: "getUserMedia camera+mic grace period does not carry over to new tab",
    run: async function checkAudioVideoGraceEndsNewTab() {
      await prompt(true, true);
      await allow(true, true);

      info("Open same page in a new tab");
      await disableObserverVerification();
      await BrowserTestUtils.withNewTab(SAME_ORIGIN + PATH, async browser => {
        info("In new tab, gUM(camera+mic) causes a prompt.");
        await prompt(true, true);
      });
      info("Closed tab");
      await enableObserverVerification();
      await closeStream();
      info("Closed stream. Waiting past grace period.");
      await checkNotSharingWithinGracePeriod();
      await wait(WAIT_PERIOD_MS);
      await checkNotSharing();

      info("After grace period expires, gUM(camera+mic) causes a prompt.");
      await prompt(true, true);
      await deny(kActionDeny);
      perms.removeFromPrincipal(null, "camera", gBrowser.selectedBrowser);
      perms.removeFromPrincipal(null, "microphone", gBrowser.selectedBrowser);
    },
  },

  {
    desc: "getUserMedia camera+mic survives navigation but not past grace",
    run: async function checkAudioVideoGracePastNavigation(browser) {
      // Use longer grace period in this test to accommodate navigation
      const LONG_GRACE_PERIOD_MS = 9000;
      const LONG_WAIT_PERIOD_MS = LONG_GRACE_PERIOD_MS + 500;
      await SpecialPowers.pushPrefEnv({
        set: [
          ["privacy.webrtc.deviceGracePeriodTimeoutMs", LONG_GRACE_PERIOD_MS],
        ],
      });
      await prompt(true, true);
      await allow(true, true);
      await closeStream();

      info("Navigate to a second same-origin page");
      await navigate(browser, SAME_ORIGIN + PATH2);
      info(
        "After navigating to second same-origin page, gUM(camera+mic) " +
          "returns a stream without prompting within grace period."
      );
      await checkNotSharingWithinGracePeriod();
      await noPrompt(true, true);
      await closeStream();

      info("Closed stream. Waiting past grace period.");
      await checkNotSharingWithinGracePeriod();
      await wait(LONG_WAIT_PERIOD_MS);
      await checkNotSharing();

      info("After grace period expires, gUM(camera+mic) causes a prompt.");
      await prompt(true, true);
      await allow(true, true);

      info("Navigate to a different-origin page");
      await navigate(browser, CROSS_ORIGIN + PATH2);
      info(
        "After navigating to a different-origin page, gUM(camera+mic) " +
          "causes a prompt."
      );
      await prompt(true, true);
      await deny(kActionDeny);
      perms.removeFromPrincipal(null, "camera", gBrowser.selectedBrowser);
      perms.removeFromPrincipal(null, "microphone", gBrowser.selectedBrowser);

      info("Navigate back to the first page");
      await navigate(browser, SAME_ORIGIN + PATH);
      info(
        "After navigating back to the first page, gUM(camera+mic) " +
          "returns a stream without prompting within grace period."
      );
      await checkNotSharingWithinGracePeriod();
      await noPrompt(true, true);
      await closeStream();
      info("Closed stream. Waiting past grace period.");
      await checkNotSharingWithinGracePeriod();
      await wait(LONG_WAIT_PERIOD_MS);
      await checkNotSharing();

      info("After grace period expires, gUM(camera+mic) causes a prompt.");
      await prompt(true, true);
      await deny(kActionDeny);
      perms.removeFromPrincipal(null, "camera", gBrowser.selectedBrowser);
      perms.removeFromPrincipal(null, "microphone", gBrowser.selectedBrowser);
    },
  },

  {
    desc: "getUserMedia camera+mic grace period cleared on permission block",
    run: async function checkAudioVideoGraceEndsNewTab(browser) {
      await SpecialPowers.pushPrefEnv({
        set: [["privacy.webrtc.deviceGracePeriodTimeoutMs", 10000]],
      });
      info("Set up longer camera grace period.");
      await prompt(false, true);
      await allow(false, true);
      await closeStream();
      let principal = gBrowser.selectedBrowser.contentPrincipal;
      info("Request both to get prompted so we can block both.");
      await prompt(true, true);
      // We need to remember this decision to set a block permission here and not just 'Not now' the request, see Bug:1609578
      await deny(kActionNever);
      // Clear the block so we can prompt again.
      perms.removeFromPrincipal(principal, "camera", gBrowser.selectedBrowser);
      perms.removeFromPrincipal(
        principal,
        "microphone",
        gBrowser.selectedBrowser
      );

      info("Revoking permission clears camera grace period.");
      await prompt(false, true);
      await deny(kActionDeny);
      perms.removeFromPrincipal(null, "camera", gBrowser.selectedBrowser);

      info("Set up longer microphone grace period.");
      await prompt(true, false);
      await allow(true, false);
      await closeStream();

      info("Request both to get prompted so we can block both.");
      await prompt(true, true);
      // We need to remember this decision to be able to set a block permission here
      await deny(kActionNever);
      perms.removeFromPrincipal(principal, "camera", gBrowser.selectedBrowser);
      perms.removeFromPrincipal(
        principal,
        "microphone",
        gBrowser.selectedBrowser
      );

      info("Revoking permission clears microphone grace period.");
      await prompt(true, false);
      // We need to remember this decision to be able to set a block permission here
      await deny(kActionNever);
      perms.removeFromPrincipal(null, "microphone", gBrowser.selectedBrowser);
    },
  },
];

add_task(async function test() {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.webrtc.deviceGracePeriodTimeoutMs", GRACE_PERIOD_MS]],
  });
  await runTests(gTests);
});