summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/webrtc/browser_devices_get_user_media_in_xorigin_frame.js
blob: 8c0b0476f38a8da39682afb77e603e28cec0de4f (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/* 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/. */
const permissionError =
  "error: NotAllowedError: The request is not allowed " +
  "by the user agent or the platform in the current context.";

const PromptResult = {
  ALLOW: "allow",
  DENY: "deny",
  PROMPT: "prompt",
};

const Perms = Services.perms;

async function promptNoDelegate(aThirdPartyOrgin, audio = true, video = true) {
  // Persistent allowed first party origin
  const uri = gBrowser.selectedBrowser.documentURI;
  if (audio) {
    PermissionTestUtils.add(uri, "microphone", Services.perms.ALLOW_ACTION);
  }
  if (video) {
    PermissionTestUtils.add(uri, "camera", Services.perms.ALLOW_ACTION);
  }

  // Check that we get a prompt.
  const observerPromise = expectObserverCalled("getUserMedia:request");
  const promise = promisePopupNotificationShown("webRTC-shareDevices");
  await promiseRequestDevice(audio, video, "frame4");
  await promise;
  await observerPromise;

  // The 'Remember this decision' checkbox is hidden.
  const notification = PopupNotifications.panel.firstElementChild;
  const checkbox = notification.checkbox;
  ok(!!checkbox, "checkbox is present");
  ok(checkbox.hidden, "checkbox is not visible");
  ok(!checkbox.checked, "checkbox not checked");

  // Check the label of the notification should be the first party
  is(
    PopupNotifications.getNotification("webRTC-shareDevices").options.name,
    uri.host,
    "Use first party's origin"
  );

  // Check the secondName of the notification should be the third party
  is(
    PopupNotifications.getNotification("webRTC-shareDevices").options
      .secondName,
    aThirdPartyOrgin,
    "Use third party's origin as secondName"
  );

  let indicator = promiseIndicatorWindow();
  let observerPromise1 = expectObserverCalled("getUserMedia:response:allow");
  let observerPromise2 = expectObserverCalled("recording-device-events");
  await promiseMessage("ok", () =>
    EventUtils.synthesizeMouseAtCenter(notification.button, {})
  );
  await observerPromise1;
  await observerPromise2;
  let state = await getMediaCaptureState();
  is(
    !!state.audio,
    audio,
    `expected microphone to be ${audio ? "" : "not"} shared`
  );
  is(
    !!state.video,
    video,
    `expected camera to be ${video ? "" : "not"} shared`
  );
  await indicator;
  await checkSharingUI({ audio, video }, undefined, undefined, {
    video: { scope: SitePermissions.SCOPE_PERSISTENT },
    audio: { scope: SitePermissions.SCOPE_PERSISTENT },
  });

  // Cleanup.
  await closeStream(false, "frame4");

  PermissionTestUtils.remove(uri, "camera");
  PermissionTestUtils.remove(uri, "microphone");
}

async function promptNoDelegateScreenSharing(aThirdPartyOrgin) {
  // Persistent allow screen sharing
  const uri = gBrowser.selectedBrowser.documentURI;
  PermissionTestUtils.add(uri, "screen", Services.perms.ALLOW_ACTION);

  const observerPromise = expectObserverCalled("getUserMedia:request");
  const promise = promisePopupNotificationShown("webRTC-shareDevices");
  await promiseRequestDevice(false, true, "frame4", "screen");
  await promise;
  await observerPromise;

  checkDeviceSelectors(["screen"]);
  const notification = PopupNotifications.panel.firstElementChild;

  // The 'Remember this decision' checkbox is hidden.
  const checkbox = notification.checkbox;
  ok(!!checkbox, "checkbox is present");

  if (ALLOW_SILENCING_NOTIFICATIONS) {
    ok(!checkbox.hidden, "Notification silencing checkbox is visible");
  } else {
    ok(checkbox.hidden, "checkbox is not visible");
  }

  ok(!checkbox.checked, "checkbox not checked");

  // Check the label of the notification should be the first party
  is(
    PopupNotifications.getNotification("webRTC-shareDevices").options.name,
    uri.host,
    "Use first party's origin"
  );

  // Check the secondName of the notification should be the third party
  is(
    PopupNotifications.getNotification("webRTC-shareDevices").options
      .secondName,
    aThirdPartyOrgin,
    "Use third party's origin as secondName"
  );

  const menulist = document.getElementById("webRTC-selectWindow-menulist");
  const count = menulist.itemCount;
  menulist.getItemAtIndex(count - 1).doCommand();
  ok(!notification.button.disabled, "Allow button is enabled");

  const indicator = promiseIndicatorWindow();
  const observerPromise1 = expectObserverCalled("getUserMedia:response:allow");
  const observerPromise2 = expectObserverCalled("recording-device-events");
  await promiseMessage("ok", () =>
    EventUtils.synthesizeMouseAtCenter(notification.button, {})
  );
  await observerPromise1;
  await observerPromise2;
  Assert.deepEqual(
    await getMediaCaptureState(),
    { screen: "Screen" },
    "expected screen to be shared"
  );

  await indicator;
  await checkSharingUI({ screen: "Screen" }, undefined, undefined, {
    screen: { scope: SitePermissions.SCOPE_PERSISTENT },
  });
  await closeStream(false, "frame4");

  PermissionTestUtils.remove(uri, "screen");
}

var gTests = [
  {
    desc: "'Always Allow' enabled on third party pages, when origin is explicitly allowed",
    run: async function checkNoAlwaysOnThirdParty() {
      // Initially set both permissions to 'prompt'.
      const uri = gBrowser.selectedBrowser.documentURI;
      PermissionTestUtils.add(uri, "microphone", Services.perms.PROMPT_ACTION);
      PermissionTestUtils.add(uri, "camera", Services.perms.PROMPT_ACTION);

      const observerPromise = expectObserverCalled("getUserMedia:request");
      const promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(true, true, "frame1");
      await promise;
      await observerPromise;
      checkDeviceSelectors(["microphone", "camera"]);

      // The 'Remember this decision' checkbox is visible.
      const notification = PopupNotifications.panel.firstElementChild;
      const checkbox = notification.checkbox;
      ok(!!checkbox, "checkbox is present");
      ok(!checkbox.hidden, "checkbox is visible");
      ok(!checkbox.checked, "checkbox not checked");

      // Check the label of the notification should be the first party
      is(
        PopupNotifications.getNotification("webRTC-shareDevices").options.name,
        uri.host,
        "Use first party's origin"
      );

      const indicator = promiseIndicatorWindow();
      const observerPromise1 = expectObserverCalled(
        "getUserMedia:response:allow"
      );
      const observerPromise2 = expectObserverCalled("recording-device-events");
      await promiseMessage("ok", () =>
        EventUtils.synthesizeMouseAtCenter(notification.button, {})
      );
      await observerPromise1;
      await observerPromise2;
      Assert.deepEqual(
        await getMediaCaptureState(),
        { audio: true, video: true },
        "expected camera and microphone to be shared"
      );
      await indicator;
      await checkSharingUI({ audio: true, video: true });

      // Cleanup.
      await closeStream(false, "frame1");
      PermissionTestUtils.remove(uri, "camera");
      PermissionTestUtils.remove(uri, "microphone");
    },
  },
  {
    desc: "'Always Allow' disabled when sharing screen in third party iframes, when origin is explicitly allowed",
    run: async function checkScreenSharing() {
      const observerPromise = expectObserverCalled("getUserMedia:request");
      const promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(false, true, "frame1", "screen");
      await promise;
      await observerPromise;

      checkDeviceSelectors(["screen"]);
      const notification = PopupNotifications.panel.firstElementChild;

      // The 'Remember this decision' checkbox is visible.
      const checkbox = notification.checkbox;
      ok(!!checkbox, "checkbox is present");
      ok(!checkbox.hidden, "checkbox is visible");
      ok(!checkbox.checked, "checkbox not checked");

      const menulist = document.getElementById("webRTC-selectWindow-menulist");
      const count = menulist.itemCount;
      ok(
        count >= 4,
        "There should be the 'Select Window or Screen' item, a separator and at least one window and one screen"
      );

      const noWindowOrScreenItem = menulist.getItemAtIndex(0);
      ok(
        noWindowOrScreenItem.hasAttribute("selected"),
        "the 'Select Window or Screen' item is selected"
      );
      is(
        menulist.selectedItem,
        noWindowOrScreenItem,
        "'Select Window or Screen' is the selected item"
      );
      is(menulist.value, "-1", "no window or screen is selected by default");
      ok(
        noWindowOrScreenItem.disabled,
        "'Select Window or Screen' item is disabled"
      );
      ok(notification.button.disabled, "Allow button is disabled");
      ok(
        notification.hasAttribute("invalidselection"),
        "Notification is marked as invalid"
      );

      menulist.getItemAtIndex(count - 1).doCommand();
      ok(!notification.button.disabled, "Allow button is enabled");

      const indicator = promiseIndicatorWindow();
      const observerPromise1 = expectObserverCalled(
        "getUserMedia:response:allow"
      );
      const observerPromise2 = expectObserverCalled("recording-device-events");
      await promiseMessage("ok", () =>
        EventUtils.synthesizeMouseAtCenter(notification.button, {})
      );
      await observerPromise1;
      await observerPromise2;
      Assert.deepEqual(
        await getMediaCaptureState(),
        { screen: "Screen" },
        "expected screen to be shared"
      );

      await indicator;
      await checkSharingUI({ screen: "Screen" });
      await closeStream(false, "frame1");
    },
  },

  {
    desc: "getUserMedia use persistent permissions from first party",
    run: async function checkUsePersistentPermissionsFirstParty() {
      async function checkPersistentPermission(
        aPermission,
        aRequestType,
        aIframeId,
        aExpect
      ) {
        info(
          `Test persistent permission ${aPermission} type ${aRequestType} expect ${aExpect}`
        );
        const uri = gBrowser.selectedBrowser.documentURI;
        // Persistent allow/deny for first party uri
        PermissionTestUtils.add(uri, aRequestType, aPermission);

        let audio = aRequestType == "microphone";
        let video = aRequestType == "camera";
        const screen = aRequestType == "screen" ? "screen" : undefined;
        if (screen) {
          audio = false;
          video = true;
        }
        if (aExpect == PromptResult.PROMPT) {
          // Check that we get a prompt.
          const observerPromise = expectObserverCalled("getUserMedia:request");
          const observerPromise1 = expectObserverCalled(
            "getUserMedia:response:deny"
          );
          const observerPromise2 = expectObserverCalled(
            "recording-window-ended"
          );
          const promise = promisePopupNotificationShown("webRTC-shareDevices");
          await promiseRequestDevice(audio, video, aIframeId, screen);
          await promise;
          await observerPromise;

          // Check the label of the notification should be the first party
          is(
            PopupNotifications.getNotification("webRTC-shareDevices").options
              .name,
            uri.host,
            "Use first party's origin"
          );

          // Deny the request to cleanup...
          await promiseMessage(permissionError, () => {
            activateSecondaryAction(kActionDeny);
          });
          await observerPromise1;
          await observerPromise2;
          let browser = gBrowser.selectedBrowser;
          SitePermissions.removeFromPrincipal(null, aRequestType, browser);
        } else if (aExpect == PromptResult.ALLOW) {
          const observerPromise = expectObserverCalled("getUserMedia:request");
          const observerPromise1 = expectObserverCalled(
            "getUserMedia:response:allow"
          );
          const observerPromise2 = expectObserverCalled(
            "recording-device-events"
          );
          const promise = promiseMessage("ok");
          await promiseRequestDevice(audio, video, aIframeId, screen);
          await promise;
          await observerPromise;

          await promiseNoPopupNotification("webRTC-shareDevices");
          await observerPromise1;
          await observerPromise2;

          let expected = {};
          if (audio) {
            expected.audio = audio;
          }
          if (video) {
            expected.video = video;
          }

          Assert.deepEqual(
            await getMediaCaptureState(),
            expected,
            "expected " + Object.keys(expected).join(" and ") + " to be shared"
          );

          await closeStream(false, aIframeId);
        } else if (aExpect == PromptResult.DENY) {
          const promises = [];
          // frame3 disallows by feature Permissions Policy before request.
          if (aIframeId != "frame3") {
            promises.push(
              expectObserverCalled("getUserMedia:request"),
              expectObserverCalled("getUserMedia:response:deny")
            );
          }
          promises.push(
            expectObserverCalled("recording-window-ended"),
            promiseMessage(permissionError),
            promiseRequestDevice(audio, video, aIframeId, screen)
          );
          await Promise.all(promises);
        }

        PermissionTestUtils.remove(uri, aRequestType);
      }

      await checkPersistentPermission(
        Perms.PROMPT_ACTION,
        "camera",
        "frame1",
        PromptResult.PROMPT
      );
      await checkPersistentPermission(
        Perms.DENY_ACTION,
        "camera",
        "frame1",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.ALLOW_ACTION,
        "camera",
        "frame1",
        PromptResult.ALLOW
      );

      await checkPersistentPermission(
        Perms.PROMPT_ACTION,
        "microphone",
        "frame1",
        PromptResult.PROMPT
      );
      await checkPersistentPermission(
        Perms.DENY_ACTION,
        "microphone",
        "frame1",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.ALLOW_ACTION,
        "microphone",
        "frame1",
        PromptResult.ALLOW
      );

      // Wildcard attributes still get delegation when their src is unchanged.
      await checkPersistentPermission(
        Perms.PROMPT_ACTION,
        "camera",
        "frame4",
        PromptResult.PROMPT
      );
      await checkPersistentPermission(
        Perms.DENY_ACTION,
        "camera",
        "frame4",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.ALLOW_ACTION,
        "camera",
        "frame4",
        PromptResult.ALLOW
      );

      // Wildcard attributes still get delegation when their src is unchanged.
      await checkPersistentPermission(
        Perms.PROMPT_ACTION,
        "microphone",
        "frame4",
        PromptResult.PROMPT
      );
      await checkPersistentPermission(
        Perms.DENY_ACTION,
        "microphone",
        "frame4",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.ALLOW_ACTION,
        "microphone",
        "frame4",
        PromptResult.ALLOW
      );

      await checkPersistentPermission(
        Perms.PROMPT_ACTION,
        "screen",
        "frame1",
        PromptResult.PROMPT
      );
      await checkPersistentPermission(
        Perms.DENY_ACTION,
        "screen",
        "frame1",
        PromptResult.DENY
      );
      // Always prompt screen sharing
      await checkPersistentPermission(
        Perms.ALLOW_ACTION,
        "screen",
        "frame1",
        PromptResult.PROMPT
      );
      await checkPersistentPermission(
        Perms.ALLOW_ACTION,
        "screen",
        "frame4",
        PromptResult.PROMPT
      );

      // Denied by default if allow is not defined
      await checkPersistentPermission(
        Perms.PROMPT_ACTION,
        "camera",
        "frame3",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.DENY_ACTION,
        "camera",
        "frame3",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.ALLOW_ACTION,
        "camera",
        "frame3",
        PromptResult.DENY
      );

      await checkPersistentPermission(
        Perms.PROMPT_ACTION,
        "microphone",
        "frame3",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.DENY_ACTION,
        "microphone",
        "frame3",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.ALLOW_ACTION,
        "microphone",
        "frame3",
        PromptResult.DENY
      );

      await checkPersistentPermission(
        Perms.PROMPT_ACTION,
        "screen",
        "frame3",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.DENY_ACTION,
        "screen",
        "frame3",
        PromptResult.DENY
      );
      await checkPersistentPermission(
        Perms.ALLOW_ACTION,
        "screen",
        "frame3",
        PromptResult.DENY
      );
    },
  },

  {
    desc: "getUserMedia use temporary blocked permissions from first party",
    run: async function checkUseTempPermissionsBlockFirstParty() {
      async function checkTempPermission(aRequestType) {
        let browser = gBrowser.selectedBrowser;
        let observerPromise = expectObserverCalled("getUserMedia:request");
        let observerPromise1 = expectObserverCalled(
          "getUserMedia:response:deny"
        );
        let observerPromise2 = expectObserverCalled("recording-window-ended");
        let promise = promisePopupNotificationShown("webRTC-shareDevices");
        let audio = aRequestType == "microphone";
        let video = aRequestType == "camera";
        const screen = aRequestType == "screen" ? "screen" : undefined;
        if (screen) {
          audio = false;
          video = true;
        }

        await promiseRequestDevice(audio, video, null, screen);
        await promise;
        await observerPromise;

        // Temporarily grant/deny from top level
        // Only need to check allow and deny temporary permissions
        await promiseMessage(permissionError, () => {
          activateSecondaryAction(kActionDeny);
        });
        await observerPromise1;
        await observerPromise2;
        await checkNotSharing();

        observerPromise = expectObserverCalled("getUserMedia:request");
        observerPromise1 = expectObserverCalled("getUserMedia:response:deny");
        observerPromise2 = expectObserverCalled("recording-window-ended");
        promise = promiseMessage(permissionError);
        await promiseRequestDevice(audio, video, "frame1", screen);
        await promise;

        await observerPromise;
        await observerPromise1;
        await observerPromise2;

        SitePermissions.removeFromPrincipal(null, aRequestType, browser);
      }

      // At the moment we only save temporary deny
      await checkTempPermission("camera");
      await checkTempPermission("microphone");
      await checkTempPermission("screen");
    },
  },
  {
    desc: "Don't reprompt while actively sharing in maybe unsafe permission delegation",
    run: async function checkNoRepromptNoDelegate() {
      // Change location to ensure that we're treated as potentially unsafe.
      await promiseChangeLocationFrame(
        "frame4",
        "https://test2.example.com/browser/browser/base/content/test/webrtc/get_user_media.html"
      );

      // Check that we get a prompt.
      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(true, true, "frame4");
      await promise;
      await observerPromise;

      // Check the secondName of the notification should be the third party
      is(
        PopupNotifications.getNotification("webRTC-shareDevices").options
          .secondName,
        "test2.example.com",
        "Use third party's origin as secondName"
      );

      const notification = PopupNotifications.panel.firstElementChild;
      let indicator = promiseIndicatorWindow();
      let observerPromise1 = expectObserverCalled(
        "getUserMedia:response:allow"
      );
      let observerPromise2 = expectObserverCalled("recording-device-events");
      await promiseMessage("ok", () =>
        EventUtils.synthesizeMouseAtCenter(notification.button, {})
      );
      await observerPromise1;
      await observerPromise2;

      let state = await getMediaCaptureState();
      is(!!state.audio, true, "expected microphone to be shared");
      is(!!state.video, true, "expected camera to be shared");
      await indicator;
      await checkSharingUI({ audio: true, video: true });

      // Check that we now don't get a prompt.
      observerPromise = expectObserverCalled("getUserMedia:request");
      observerPromise1 = expectObserverCalled("getUserMedia:response:allow");
      observerPromise2 = expectObserverCalled("recording-device-events");
      promise = promiseMessage("ok");
      await promiseRequestDevice(true, true, "frame4");
      await promise;
      await observerPromise;

      await promiseNoPopupNotification("webRTC-shareDevices");
      await observerPromise1;
      await observerPromise2;

      state = await getMediaCaptureState();
      is(!!state.audio, true, "expected microphone to be shared");
      is(!!state.video, true, "expected camera to be shared");
      await checkSharingUI({ audio: true, video: true });

      // Cleanup.
      await closeStream(false, "frame4");
    },
  },
  {
    desc: "Change location, prompt and display both first party and third party origin in maybe unsafe permission delegation",
    run: async function checkPromptNoDelegateChangeLoxation() {
      await promiseChangeLocationFrame(
        "frame4",
        "https://test2.example.com/browser/browser/base/content/test/webrtc/get_user_media.html"
      );
      await promptNoDelegate("test2.example.com");
    },
  },
  {
    desc: "Change location, prompt and display both first party and third party origin when sharing screen in unsafe permission delegation",
    run: async function checkPromptNoDelegateScreenSharingChangeLocation() {
      await promiseChangeLocationFrame(
        "frame4",
        "https://test2.example.com/browser/browser/base/content/test/webrtc/get_user_media.html"
      );
      await promptNoDelegateScreenSharing("test2.example.com");
    },
  },
  {
    desc: "Prompt and display both first party and third party origin and temporary deny in frame does not change permission scope",
    skipObserverVerification: true,
    run: async function checkPromptBothOriginsTempDenyFrame() {
      // Change location to ensure that we're treated as potentially unsafe.
      await promiseChangeLocationFrame(
        "frame4",
        "https://test2.example.com/browser/browser/base/content/test/webrtc/get_user_media.html"
      );

      // Persistent allowed first party origin
      let browser = gBrowser.selectedBrowser;
      let uri = gBrowser.selectedBrowser.documentURI;
      let principal = Services.scriptSecurityManager.createContentPrincipal(
        uri,
        {}
      );

      let observerPromise = expectObserverCalled("getUserMedia:request");
      let promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(true, true);
      await promise;
      await observerPromise;

      // Ensure that checking the 'Remember this decision'
      let notification = PopupNotifications.panel.firstElementChild;
      let checkbox = notification.checkbox;
      ok(!!checkbox, "checkbox is present");
      ok(!checkbox.checked, "checkbox is not checked");
      checkbox.click();

      let indicator = promiseIndicatorWindow();
      let observerPromise1 = expectObserverCalled(
        "getUserMedia:response:allow"
      );
      let observerPromise2 = expectObserverCalled("recording-device-events");
      await promiseMessage("ok", () =>
        EventUtils.synthesizeMouseAtCenter(notification.button, {})
      );
      await observerPromise1;
      await observerPromise2;
      Assert.deepEqual(
        await getMediaCaptureState(),
        { audio: true, video: true },
        "expected camera and microphone to be shared"
      );
      await indicator;
      await checkSharingUI({ audio: true, video: true }, undefined, undefined, {
        audio: { scope: SitePermissions.SCOPE_PERSISTENT },
        video: { scope: SitePermissions.SCOPE_PERSISTENT },
      });
      await closeStream(true);

      // Check that we get a prompt.
      observerPromise = expectObserverCalled("getUserMedia:request");
      promise = promisePopupNotificationShown("webRTC-shareDevices");
      await promiseRequestDevice(true, true, "frame4");
      await promise;
      await observerPromise;

      // The 'Remember this decision' checkbox is hidden.
      notification = PopupNotifications.panel.firstElementChild;
      checkbox = notification.checkbox;
      ok(!!checkbox, "checkbox is present");
      ok(checkbox.hidden, "checkbox is not visible");

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

      await observerPromise1;
      await observerPromise2;
      await checkNotSharing();

      // Make sure we are not changing the scope and state of persistent
      // permission
      let { state, scope } = SitePermissions.getForPrincipal(
        principal,
        "camera",
        browser
      );
      Assert.equal(state, SitePermissions.ALLOW);
      Assert.equal(scope, SitePermissions.SCOPE_PERSISTENT);

      ({ state, scope } = SitePermissions.getForPrincipal(
        principal,
        "microphone",
        browser
      ));
      Assert.equal(state, SitePermissions.ALLOW);
      Assert.equal(scope, SitePermissions.SCOPE_PERSISTENT);

      PermissionTestUtils.remove(uri, "camera");
      PermissionTestUtils.remove(uri, "microphone");
    },
  },
];

add_task(async function test() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["permissions.delegation.enabled", true],
      ["dom.security.featurePolicy.header.enabled", true],
      ["dom.security.featurePolicy.webidl.enabled", true],
    ],
  });

  await runTests(gTests, {
    relativeURI: "get_user_media_in_xorigin_frame.html",
  });
});