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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
|
/* 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";
var EXPORTED_SYMBOLS = ["WebRTCParent"];
const { XPCOMUtils } = ChromeUtils.importESModule(
"resource://gre/modules/XPCOMUtils.sys.mjs"
);
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
});
ChromeUtils.defineModuleGetter(
lazy,
"SitePermissions",
"resource:///modules/SitePermissions.jsm"
);
ChromeUtils.defineModuleGetter(
lazy,
"webrtcUI",
"resource:///modules/webrtcUI.jsm"
);
XPCOMUtils.defineLazyServiceGetter(
lazy,
"OSPermissions",
"@mozilla.org/ospermissionrequest;1",
"nsIOSPermissionRequest"
);
// Keep in sync with defines at base_capturer_pipewire.cc
// With PipeWire we can't select which system resource is shared so
// we don't create a window/screen list. Instead we place these constants
// as window name/id so frontend code can identify PipeWire backend
// and does not try to create screen/window preview.
const PIPEWIRE_PORTAL_NAME = "####_PIPEWIRE_PORTAL_####";
const PIPEWIRE_ID = 0xaffffff;
class WebRTCParent extends JSWindowActorParent {
didDestroy() {
// Media stream tracks end on unload, so call stopRecording() on them early
// *before* we go away, to ensure we're working with the right principal.
this.stopRecording(this.manager.outerWindowId);
lazy.webrtcUI.forgetStreamsFromBrowserContext(this.browsingContext);
// Must clear activePerms here to prevent them from being read by laggard
// stopRecording() calls, which due to IPC, may come in *after* navigation.
// This is to prevent granting temporary grace periods to the wrong page.
lazy.webrtcUI.activePerms.delete(this.manager.outerWindowId);
}
getBrowser() {
return this.browsingContext.top.embedderElement;
}
receiveMessage(aMessage) {
switch (aMessage.name) {
case "rtcpeer:Request": {
let params = Object.freeze(
Object.assign(
{
origin: this.manager.documentPrincipal.origin,
},
aMessage.data
)
);
let blockers = Array.from(lazy.webrtcUI.peerConnectionBlockers);
(async function() {
for (let blocker of blockers) {
try {
let result = await blocker(params);
if (result == "deny") {
return false;
}
} catch (err) {
console.error(`error in PeerConnection blocker: ${err.message}`);
}
}
return true;
})().then(decision => {
let message;
if (decision) {
lazy.webrtcUI.emitter.emit("peer-request-allowed", params);
message = "rtcpeer:Allow";
} else {
lazy.webrtcUI.emitter.emit("peer-request-blocked", params);
message = "rtcpeer:Deny";
}
this.sendAsyncMessage(message, {
callID: params.callID,
windowID: params.windowID,
});
});
break;
}
case "rtcpeer:CancelRequest": {
let params = Object.freeze({
origin: this.manager.documentPrincipal.origin,
callID: aMessage.data,
});
lazy.webrtcUI.emitter.emit("peer-request-cancel", params);
break;
}
case "webrtc:Request": {
let data = aMessage.data;
// Record third party origins for telemetry.
let isThirdPartyOrigin =
this.manager.documentPrincipal.origin !=
this.manager.topWindowContext.documentPrincipal.origin;
data.isThirdPartyOrigin = isThirdPartyOrigin;
data.origin = data.shouldDelegatePermission
? this.manager.topWindowContext.documentPrincipal.origin
: this.manager.documentPrincipal.origin;
let browser = this.getBrowser();
if (browser.fxrPermissionPrompt) {
// For Firefox Reality on Desktop, switch to a different mechanism to
// prompt the user since fewer permissions are available and since many
// UI dependencies are not available.
browser.fxrPermissionPrompt(data);
} else {
prompt(this, this.getBrowser(), data);
}
break;
}
case "webrtc:StopRecording":
this.stopRecording(
aMessage.data.windowID,
aMessage.data.mediaSource,
aMessage.data.rawID
);
break;
case "webrtc:CancelRequest": {
let browser = this.getBrowser();
// browser can be null when closing the window
if (browser) {
removePrompt(browser, aMessage.data);
}
break;
}
case "webrtc:UpdateIndicators": {
let { data } = aMessage;
data.documentURI = this.manager.documentURI?.spec;
if (data.windowId) {
if (!data.remove) {
data.principal = this.manager.topWindowContext.documentPrincipal;
}
lazy.webrtcUI.streamAddedOrRemoved(this.browsingContext, data);
}
this.updateIndicators(data);
break;
}
}
}
updateIndicators(aData) {
let browsingContext = this.browsingContext;
let state = lazy.webrtcUI.updateIndicators(browsingContext.top);
let browser = this.getBrowser();
if (!browser) {
return;
}
state.browsingContext = browsingContext;
state.windowId = aData.windowId;
let tabbrowser = browser.ownerGlobal.gBrowser;
if (tabbrowser) {
tabbrowser.updateBrowserSharing(browser, {
webRTC: state,
});
}
}
denyRequest(aRequest) {
this.sendAsyncMessage("webrtc:Deny", {
callID: aRequest.callID,
windowID: aRequest.windowID,
});
}
//
// Deny the request because the browser does not have access to the
// camera or microphone due to OS security restrictions. The user may
// have granted camera/microphone access to the site, but not have
// allowed the browser access in OS settings.
//
denyRequestNoPermission(aRequest) {
this.sendAsyncMessage("webrtc:Deny", {
callID: aRequest.callID,
windowID: aRequest.windowID,
noOSPermission: true,
});
}
//
// Check if we have permission to access the camera or screen-sharing and/or
// microphone at the OS level. Triggers a request to access the device if access
// is needed and the permission state has not yet been determined.
//
async checkOSPermission(camNeeded, micNeeded, scrNeeded) {
// Don't trigger OS permission requests for fake devices. Fake devices don't
// require OS permission and the dialogs are problematic in automated testing
// (where fake devices are used) because they require user interaction.
if (
!scrNeeded &&
Services.prefs.getBoolPref("media.navigator.streams.fake", false)
) {
return true;
}
let camStatus = {},
micStatus = {};
if (camNeeded || micNeeded) {
lazy.OSPermissions.getMediaCapturePermissionState(camStatus, micStatus);
}
if (camNeeded) {
let camPermission = camStatus.value;
let camAccessible = await this.checkAndGetOSPermission(
camPermission,
lazy.OSPermissions.requestVideoCapturePermission
);
if (!camAccessible) {
return false;
}
}
if (micNeeded) {
let micPermission = micStatus.value;
let micAccessible = await this.checkAndGetOSPermission(
micPermission,
lazy.OSPermissions.requestAudioCapturePermission
);
if (!micAccessible) {
return false;
}
}
let scrStatus = {};
if (scrNeeded) {
lazy.OSPermissions.getScreenCapturePermissionState(scrStatus);
if (scrStatus.value == lazy.OSPermissions.PERMISSION_STATE_DENIED) {
lazy.OSPermissions.maybeRequestScreenCapturePermission();
return false;
}
}
return true;
}
//
// Given a device's permission, return true if the device is accessible. If
// the device's permission is not yet determined, request access to the device.
// |requestPermissionFunc| must return a promise that resolves with true
// if the device is accessible and false otherwise.
//
async checkAndGetOSPermission(devicePermission, requestPermissionFunc) {
if (
devicePermission == lazy.OSPermissions.PERMISSION_STATE_DENIED ||
devicePermission == lazy.OSPermissions.PERMISSION_STATE_RESTRICTED
) {
return false;
}
if (devicePermission == lazy.OSPermissions.PERMISSION_STATE_NOTDETERMINED) {
let deviceAllowed = await requestPermissionFunc();
if (!deviceAllowed) {
return false;
}
}
return true;
}
stopRecording(aOuterWindowId, aMediaSource, aRawId) {
for (let { browsingContext, state } of lazy.webrtcUI._streams) {
if (browsingContext == this.browsingContext) {
let { principal } = state;
for (let { mediaSource, rawId } of state.devices) {
if (aRawId && (aRawId != rawId || aMediaSource != mediaSource)) {
continue;
}
// Deactivate this device (no aRawId means all devices).
this.deactivateDevicePerm(
aOuterWindowId,
mediaSource,
rawId,
principal
);
}
}
}
}
/**
* Add a device record to webrtcUI.activePerms, denoting a device as in use.
* Important to call for permission grace periods to work correctly.
*/
activateDevicePerm(aOuterWindowId, aMediaSource, aId) {
if (!lazy.webrtcUI.activePerms.has(this.manager.outerWindowId)) {
lazy.webrtcUI.activePerms.set(this.manager.outerWindowId, new Map());
}
lazy.webrtcUI.activePerms
.get(this.manager.outerWindowId)
.set(aOuterWindowId + aMediaSource + aId, aMediaSource);
}
/**
* Remove a device record from webrtcUI.activePerms, denoting a device as
* no longer in use by the site. Meaning: gUM requests for this device will
* no longer be implicitly granted through the webrtcUI.activePerms mechanism.
*
* However, if webrtcUI.deviceGracePeriodTimeoutMs is defined, the implicit
* grant is extended for an additional period of time through SitePermissions.
*/
deactivateDevicePerm(
aOuterWindowId,
aMediaSource,
aId,
aPermissionPrincipal
) {
// If we don't have active permissions for the given window anymore don't
// set a grace period. This happens if there has been a user revoke and
// webrtcUI clears the permissions.
if (!lazy.webrtcUI.activePerms.has(this.manager.outerWindowId)) {
return;
}
let map = lazy.webrtcUI.activePerms.get(this.manager.outerWindowId);
map.delete(aOuterWindowId + aMediaSource + aId);
// Add a permission grace period for camera and microphone only
if (
(aMediaSource != "camera" && aMediaSource != "microphone") ||
!this.browsingContext.top.embedderElement
) {
return;
}
let gracePeriodMs = lazy.webrtcUI.deviceGracePeriodTimeoutMs;
if (gracePeriodMs > 0) {
// A grace period is extended (even past navigation) to this outer window
// + origin + deviceId only. This avoids re-prompting without the user
// having to persist permission to the site, in a common case of a web
// conference asking them for the camera in a lobby page, before
// navigating to the actual meeting room page. Does not survive tab close.
//
// Caution: since navigation causes deactivation, we may be in the middle
// of one. We must pass in a principal & URI for SitePermissions to use
// instead of browser.currentURI, because the latter may point to a new
// page already, and we must not leak permission to unrelated pages.
//
let permissionName = [aMediaSource, aId].join("^");
lazy.SitePermissions.setForPrincipal(
aPermissionPrincipal,
permissionName,
lazy.SitePermissions.ALLOW,
lazy.SitePermissions.SCOPE_TEMPORARY,
this.browsingContext.top.embedderElement,
gracePeriodMs,
aPermissionPrincipal.URI
);
}
}
/**
* Checks if the principal has sufficient permissions
* to fulfill the given request. If the request can be
* fulfilled, a message is sent to the child
* signaling that WebRTC permissions were given and
* this function will return true.
*/
checkRequestAllowed(aRequest, aPrincipal) {
if (!aRequest.secure) {
return false;
}
// Always prompt for screen sharing
if (aRequest.sharingScreen) {
return false;
}
let {
callID,
windowID,
audioInputDevices,
videoInputDevices,
audioOutputDevices,
hasInherentAudioConstraints,
hasInherentVideoConstraints,
audioOutputId,
} = aRequest;
if (audioOutputDevices?.length) {
// Prompt if a specific device is not requested, available and allowed.
let device = audioOutputDevices.find(({ id }) => id == audioOutputId);
if (
!device ||
!lazy.SitePermissions.getForPrincipal(
aPrincipal,
["speaker", device.id].join("^"),
this.getBrowser()
).state == lazy.SitePermissions.ALLOW
) {
return false;
}
this.sendAsyncMessage("webrtc:Allow", {
callID,
windowID,
devices: [device.deviceIndex],
});
return true;
}
let { perms } = Services;
if (
perms.testExactPermissionFromPrincipal(aPrincipal, "MediaManagerVideo")
) {
perms.removeFromPrincipal(aPrincipal, "MediaManagerVideo");
}
// Don't use persistent permissions from the top-level principal
// if we're in a cross-origin iframe and permission delegation is not
// allowed, or when we're handling a potentially insecure third party
// through a wildcard ("*") allow attribute.
let limited =
(aRequest.isThirdPartyOrigin && !aRequest.shouldDelegatePermission) ||
aRequest.secondOrigin;
let map = lazy.webrtcUI.activePerms.get(this.manager.outerWindowId);
// We consider a camera or mic active if it is active or was active within a
// grace period of milliseconds ago.
const isAllowed = ({ mediaSource, rawId }, permissionID) =>
map?.get(windowID + mediaSource + rawId) ||
(!limited &&
(lazy.SitePermissions.getForPrincipal(aPrincipal, permissionID).state ==
lazy.SitePermissions.ALLOW ||
lazy.SitePermissions.getForPrincipal(
aPrincipal,
[mediaSource, rawId].join("^"),
this.getBrowser()
).state == lazy.SitePermissions.ALLOW));
let microphone;
if (audioInputDevices.length) {
for (let device of audioInputDevices) {
if (isAllowed(device, "microphone")) {
microphone = device;
break;
}
if (hasInherentAudioConstraints) {
// Inherent constraints suggest site is looking for a specific mic
break;
}
// Some sites don't look too hard at what they get, and spam gUM without
// adjusting what they ask for to match what they got last time. To keep
// users in charge and reduce prompts, ignore other constraints by
// returning the most-fit microphone a site already has access to.
}
if (!microphone) {
return false;
}
}
let camera;
if (videoInputDevices.length) {
for (let device of videoInputDevices) {
if (isAllowed(device, "camera")) {
camera = device;
break;
}
if (hasInherentVideoConstraints) {
// Inherent constraints suggest site is looking for a specific camera
break;
}
// Some sites don't look too hard at what they get, and spam gUM without
// adjusting what they ask for to match what they got last time. To keep
// users in charge and reduce prompts, ignore other constraints by
// returning the most-fit camera a site already has access to.
}
if (!camera) {
return false;
}
}
let devices = [];
if (camera) {
perms.addFromPrincipal(
aPrincipal,
"MediaManagerVideo",
perms.ALLOW_ACTION,
perms.EXPIRE_SESSION
);
devices.push(camera.deviceIndex);
this.activateDevicePerm(windowID, camera.mediaSource, camera.rawId);
}
if (microphone) {
devices.push(microphone.deviceIndex);
this.activateDevicePerm(
windowID,
microphone.mediaSource,
microphone.rawId
);
}
this.checkOSPermission(!!camera, !!microphone, false).then(
havePermission => {
if (havePermission) {
this.sendAsyncMessage("webrtc:Allow", { callID, windowID, devices });
} else {
this.denyRequestNoPermission(aRequest);
}
}
);
return true;
}
}
function prompt(aActor, aBrowser, aRequest) {
let {
audioInputDevices,
videoInputDevices,
audioOutputDevices,
sharingScreen,
sharingAudio,
requestTypes,
} = aRequest;
let principal = Services.scriptSecurityManager.createContentPrincipalFromOrigin(
aRequest.origin
);
// For add-on principals, we immediately check for permission instead
// of waiting for the notification to focus. This allows for supporting
// cases such as browserAction popups where no prompt is shown.
if (principal.addonPolicy) {
let isPopup = false;
let isBackground = false;
for (let view of principal.addonPolicy.extension.views) {
if (view.viewType == "popup" && view.xulBrowser == aBrowser) {
isPopup = true;
}
if (view.viewType == "background" && view.xulBrowser == aBrowser) {
isBackground = true;
}
}
// Recording from background pages is considered too sensitive and will
// always be denied.
if (isBackground) {
aActor.denyRequest(aRequest);
return;
}
// If the request comes from a popup, we don't want to show the prompt,
// but we do want to allow the request if the user previously gave permission.
if (isPopup) {
if (!aActor.checkRequestAllowed(aRequest, principal, aBrowser)) {
aActor.denyRequest(aRequest);
}
return;
}
}
// If the user has already denied access once in this tab,
// deny again without even showing the notification icon.
for (const type of requestTypes) {
const permissionID =
type == "AudioCapture" ? "microphone" : type.toLowerCase();
if (
lazy.SitePermissions.getForPrincipal(principal, permissionID, aBrowser)
.state == lazy.SitePermissions.BLOCK
) {
aActor.denyRequest(aRequest);
return;
}
}
let chromeDoc = aBrowser.ownerDocument;
const localization = new Localization(
["browser/webrtcIndicator.ftl", "branding/brand.ftl"],
true
);
/** @type {"Screen" | "Camera" | null} */
let reqVideoInput = null;
if (videoInputDevices.length) {
reqVideoInput = sharingScreen ? "Screen" : "Camera";
}
/** @type {"AudioCapture" | "Microphone" | null} */
let reqAudioInput = null;
if (audioInputDevices.length) {
reqAudioInput = sharingAudio ? "AudioCapture" : "Microphone";
}
const reqAudioOutput = !!audioOutputDevices.length;
const stringId = getPromptMessageId(
reqVideoInput,
reqAudioInput,
reqAudioOutput,
!!aRequest.secondOrigin
);
const message = localization.formatValueSync(stringId, {
origin: "<>",
thirdParty: "{}",
});
let notification; // Used by action callbacks.
const actionL10nIds = [{ id: "webrtc-action-allow" }];
let notificationSilencingEnabled = Services.prefs.getBoolPref(
"privacy.webrtc.allowSilencingNotifications"
);
const isNotNowLabelEnabled = allowedOrActiveCameraOrMicrophone(aBrowser);
let secondaryActions = [];
if (notificationSilencingEnabled && sharingScreen) {
// We want to free up the checkbox at the bottom of the permission
// panel for the notification silencing option, so we use a
// different configuration for the permissions panel when
// notification silencing is enabled.
// If we have a (temporary) allow permission for some mic/cam device
// we offer a 'Not now' label instead of 'Block'.
const id = isNotNowLabelEnabled
? "webrtc-action-not-now"
: "webrtc-action-block";
actionL10nIds.push({ id }, { id: "webrtc-action-always-block" });
secondaryActions = [
{
callback(aState) {
aActor.denyRequest(aRequest);
if (!isNotNowLabelEnabled) {
lazy.SitePermissions.setForPrincipal(
principal,
"screen",
lazy.SitePermissions.BLOCK,
lazy.SitePermissions.SCOPE_TEMPORARY,
notification.browser
);
}
},
},
{
callback(aState) {
aActor.denyRequest(aRequest);
lazy.SitePermissions.setForPrincipal(
principal,
"screen",
lazy.SitePermissions.BLOCK,
lazy.SitePermissions.SCOPE_PERSISTENT,
notification.browser
);
},
},
];
} else {
// We have a (temporary) allow permission for some device
// hence we offer a 'Not now' label instead of 'Block'.
const id = isNotNowLabelEnabled
? "webrtc-action-not-now"
: "webrtc-action-block";
actionL10nIds.push({ id });
secondaryActions = [
{
callback(aState) {
aActor.denyRequest(aRequest);
const isPersistent = aState?.checkboxChecked;
// Choosing 'Not now' will not set a block permission
// we just deny the request. This enables certain use cases
// where sites want to switch devices, but users back out of the permission request
// (See Bug 1609578).
// Selecting 'Remember this decision' and clicking 'Not now' will set a persistent block
if (!isPersistent && isNotNowLabelEnabled) {
return;
}
// Denying a camera / microphone prompt means we set a temporary or
// persistent permission block. There may still be active grace period
// permissions at this point. We need to remove them.
clearTemporaryGrants(
notification.browser,
reqVideoInput === "Camera",
!!reqAudioInput
);
const scope = isPersistent
? lazy.SitePermissions.SCOPE_PERSISTENT
: lazy.SitePermissions.SCOPE_TEMPORARY;
if (reqAudioInput) {
lazy.SitePermissions.setForPrincipal(
principal,
"microphone",
lazy.SitePermissions.BLOCK,
scope,
notification.browser
);
}
if (reqVideoInput) {
lazy.SitePermissions.setForPrincipal(
principal,
sharingScreen ? "screen" : "camera",
lazy.SitePermissions.BLOCK,
scope,
notification.browser
);
}
},
},
];
}
// The formatMessagesSync method returns an array of results
// for each message that was requested, and for the ones with
// attributes, returns an attributes array with objects like:
// { name: "label", value: "somevalue" }
const [mainMessage, ...secondaryMessages] = localization
.formatMessagesSync(actionL10nIds)
.map(msg =>
msg.attributes.reduce(
(acc, { name, value }) => ({ ...acc, [name]: value }),
{}
)
);
const mainAction = {
label: mainMessage.label,
accessKey: mainMessage.accesskey,
// The real callback will be set during the "showing" event. The
// empty function here is so that PopupNotifications.show doesn't
// reject the action.
callback() {},
};
for (let i = 0; i < secondaryActions.length; ++i) {
secondaryActions[i].label = secondaryMessages[i].label;
secondaryActions[i].accessKey = secondaryMessages[i].accesskey;
}
let options = {
name: lazy.webrtcUI.getHostOrExtensionName(principal.URI),
persistent: true,
hideClose: true,
eventCallback(aTopic, aNewBrowser, isCancel) {
if (aTopic == "swapping") {
return true;
}
let doc = this.browser.ownerDocument;
// Clean-up video streams of screensharing previews.
if (
reqVideoInput !== "Screen" ||
aTopic == "dismissed" ||
aTopic == "removed"
) {
let video = doc.getElementById("webRTC-previewVideo");
video.deviceId = null; // Abort previews still being started.
if (video.stream) {
video.stream.getTracks().forEach(t => t.stop());
video.stream = null;
video.src = null;
doc.getElementById("webRTC-preview").hidden = true;
}
let menupopup = doc.getElementById("webRTC-selectWindow-menupopup");
if (menupopup._commandEventListener) {
menupopup.removeEventListener(
"command",
menupopup._commandEventListener
);
menupopup._commandEventListener = null;
}
}
// If the notification has been cancelled (e.g. due to entering full-screen), also cancel the webRTC request
if (aTopic == "removed" && notification && isCancel) {
aActor.denyRequest(aRequest);
}
if (aTopic != "showing") {
return false;
}
// If BLOCK has been set persistently in the permission manager or has
// been set on the tab, then it is handled synchronously before we add
// the notification.
// Handling of ALLOW is delayed until the popupshowing event,
// to avoid granting permissions automatically to background tabs.
if (aActor.checkRequestAllowed(aRequest, principal, aBrowser)) {
this.remove();
return true;
}
function listDevices(menupopup, devices, labelID) {
while (menupopup.lastChild) {
menupopup.removeChild(menupopup.lastChild);
}
let menulist = menupopup.parentNode;
// Removing the child nodes of the menupopup doesn't clear the value
// attribute of the menulist. This can have unfortunate side effects
// when the list is rebuilt with a different content, so we remove
// the value attribute and unset the selectedItem explicitly.
menulist.removeAttribute("value");
menulist.selectedItem = null;
for (let device of devices) {
let item = addDeviceToList(
menupopup,
device.name,
device.deviceIndex
);
if (device.id == aRequest.audioOutputId) {
menulist.selectedItem = item;
}
}
let label = doc.getElementById(labelID);
if (devices.length == 1) {
label.value = devices[0].name;
label.hidden = false;
menulist.hidden = true;
} else {
label.hidden = true;
menulist.hidden = false;
}
}
let notificationElement = doc.getElementById(
"webRTC-shareDevices-notification"
);
function checkDisabledWindowMenuItem() {
let list = doc.getElementById("webRTC-selectWindow-menulist");
let item = list.selectedItem;
if (!item || item.hasAttribute("disabled")) {
notificationElement.setAttribute("invalidselection", "true");
} else {
notificationElement.removeAttribute("invalidselection");
}
}
function listScreenShareDevices(menupopup, devices) {
while (menupopup.lastChild) {
menupopup.removeChild(menupopup.lastChild);
}
// Removing the child nodes of the menupopup doesn't clear the value
// attribute of the menulist. This can have unfortunate side effects
// when the list is rebuilt with a different content, so we remove
// the value attribute and unset the selectedItem explicitly.
menupopup.parentNode.removeAttribute("value");
menupopup.parentNode.selectedItem = null;
// "Select a Window or Screen" is the default because we can't and don't
// want to pick a 'default' window to share (Full screen is "scary").
addDeviceToList(
menupopup,
localization.formatValueSync("webrtc-pick-window-or-screen"),
"-1"
);
menupopup.appendChild(doc.createXULElement("menuseparator"));
let isPipeWire = false;
// Build the list of 'devices'.
let monitorIndex = 1;
for (let i = 0; i < devices.length; ++i) {
let device = devices[i];
let type = device.mediaSource;
let name;
// Building screen list from available screens.
if (type == "screen") {
if (device.name == "Primary Monitor") {
name = localization.formatValueSync("webrtc-share-entire-screen");
} else {
name = localization.formatValueSync("webrtc-share-monitor", {
monitorIndex,
});
++monitorIndex;
}
} else {
name = device.name;
// When we share content by PipeWire add only one item to the device
// list. When it's selected PipeWire portal dialog is opened and
// user confirms actual window/screen sharing there.
// Don't mark it as scary as there's an extra confirmation step by
// PipeWire portal dialog.
if (name == PIPEWIRE_PORTAL_NAME && device.rawId == PIPEWIRE_ID) {
isPipeWire = true;
let item = addDeviceToList(
menupopup,
localization.formatValueSync("webrtc-share-pipe-wire-portal"),
i,
type
);
item.deviceId = device.rawId;
item.mediaSource = type;
// In this case the OS sharing dialog will be the only option and
// can be safely pre-selected.
menupopup.parentNode.selectedItem = item;
menupopup.parentNode.disabled = true;
break;
}
if (type == "application") {
// The application names returned by the platform are of the form:
// <window count>\x1e<application name>
const [count, appName] = name.split("\x1e");
name = localization.formatValueSync("webrtc-share-application", {
appName,
windowCount: parseInt(count),
});
}
}
let item = addDeviceToList(menupopup, name, i, type);
item.deviceId = device.rawId;
item.mediaSource = type;
if (device.scary) {
item.scary = true;
}
}
// Always re-select the "No <type>" item.
doc
.getElementById("webRTC-selectWindow-menulist")
.removeAttribute("value");
doc.getElementById("webRTC-all-windows-shared").hidden = true;
menupopup._commandEventListener = event => {
checkDisabledWindowMenuItem();
let video = doc.getElementById("webRTC-previewVideo");
if (video.stream) {
video.stream.getTracks().forEach(t => t.stop());
video.stream = null;
}
const { deviceId, mediaSource, scary } = event.target;
if (deviceId == undefined) {
doc.getElementById("webRTC-preview").hidden = true;
video.src = null;
return;
}
let warning = doc.getElementById("webRTC-previewWarning");
let warningBox = doc.getElementById("webRTC-previewWarningBox");
warningBox.hidden = !scary;
let chromeWin = doc.defaultView;
if (scary) {
const warnId =
mediaSource == "screen"
? "webrtc-share-screen-warning"
: "webrtc-share-browser-warning";
doc.l10n.setAttributes(warning, warnId);
const learnMore = doc.getElementById(
"webRTC-previewWarning-learnMore"
);
const baseURL = Services.urlFormatter.formatURLPref(
"app.support.baseURL"
);
learnMore.setAttribute("href", baseURL + "screenshare-safety");
doc.l10n.setAttributes(learnMore, "webrtc-share-screen-learn-more");
// On Catalina, we don't want to blow our chance to show the
// OS-level helper prompt to enable screen recording if the user
// intends to reject anyway. OTOH showing it when they click Allow
// is too late. A happy middle is to show it when the user makes a
// choice in the picker. This already happens implicitly if the
// user chooses "Entire desktop", as a side-effect of our preview,
// we just need to also do it if they choose "Firefox". These are
// the lone two options when permission is absent on Catalina.
// Ironically, these are the two sources marked "scary" from a
// web-sharing perspective, which is why this code resides here.
// A restart doesn't appear to be necessary in spite of OS wording.
let scrStatus = {};
lazy.OSPermissions.getScreenCapturePermissionState(scrStatus);
if (scrStatus.value == lazy.OSPermissions.PERMISSION_STATE_DENIED) {
lazy.OSPermissions.maybeRequestScreenCapturePermission();
}
}
let perms = Services.perms;
let chromePrincipal = Services.scriptSecurityManager.getSystemPrincipal();
perms.addFromPrincipal(
chromePrincipal,
"MediaManagerVideo",
perms.ALLOW_ACTION,
perms.EXPIRE_SESSION
);
if (!isPipeWire) {
video.deviceId = deviceId;
let constraints = {
video: { mediaSource, deviceId: { exact: deviceId } },
};
chromeWin.navigator.mediaDevices.getUserMedia(constraints).then(
stream => {
if (video.deviceId != deviceId) {
// The user has selected a different device or closed the panel
// before getUserMedia finished.
stream.getTracks().forEach(t => t.stop());
return;
}
video.srcObject = stream;
video.stream = stream;
doc.getElementById("webRTC-preview").hidden = false;
video.onloadedmetadata = function(e) {
video.play();
};
},
err => {
if (
err.name == "OverconstrainedError" &&
err.constraint == "deviceId"
) {
// Window has disappeared since enumeration, which can happen.
// No preview for you.
return;
}
console.error(
`error in preview: ${err.message} ${err.constraint}`
);
}
);
}
};
menupopup.addEventListener("command", menupopup._commandEventListener);
}
function addDeviceToList(menupopup, deviceName, deviceIndex, type) {
let menuitem = doc.createXULElement("menuitem");
menuitem.setAttribute("value", deviceIndex);
menuitem.setAttribute("label", deviceName);
menuitem.setAttribute("tooltiptext", deviceName);
if (type) {
menuitem.setAttribute("devicetype", type);
}
if (deviceIndex == "-1") {
menuitem.setAttribute("disabled", true);
}
menupopup.appendChild(menuitem);
return menuitem;
}
doc.getElementById("webRTC-selectCamera").hidden =
reqVideoInput !== "Camera";
doc.getElementById("webRTC-selectWindowOrScreen").hidden =
reqVideoInput !== "Screen";
doc.getElementById("webRTC-selectMicrophone").hidden =
reqAudioInput !== "Microphone";
doc.getElementById("webRTC-selectSpeaker").hidden = !reqAudioOutput;
let camMenupopup = doc.getElementById("webRTC-selectCamera-menupopup");
let windowMenupopup = doc.getElementById("webRTC-selectWindow-menupopup");
let micMenupopup = doc.getElementById(
"webRTC-selectMicrophone-menupopup"
);
let speakerMenupopup = doc.getElementById(
"webRTC-selectSpeaker-menupopup"
);
let describedByIDs = ["webRTC-shareDevices-notification-description"];
if (sharingScreen) {
listScreenShareDevices(windowMenupopup, videoInputDevices);
checkDisabledWindowMenuItem();
} else {
let labelID = "webRTC-selectCamera-single-device-label";
listDevices(camMenupopup, videoInputDevices, labelID);
notificationElement.removeAttribute("invalidselection");
if (videoInputDevices.length == 1) {
describedByIDs.push("webRTC-selectCamera-icon", labelID);
}
}
if (!sharingAudio) {
let labelID = "webRTC-selectMicrophone-single-device-label";
listDevices(micMenupopup, audioInputDevices, labelID);
if (audioInputDevices.length == 1) {
describedByIDs.push("webRTC-selectMicrophone-icon", labelID);
}
}
let labelID = "webRTC-selectSpeaker-single-device-label";
listDevices(speakerMenupopup, audioOutputDevices, labelID);
if (audioOutputDevices.length == 1) {
describedByIDs.push("webRTC-selectSpeaker-icon", labelID);
}
// PopupNotifications knows to clear the aria-describedby attribute
// when hiding, so we don't have to worry about cleaning it up ourselves.
chromeDoc.defaultView.PopupNotifications.panel.setAttribute(
"aria-describedby",
describedByIDs.join(" ")
);
this.mainAction.callback = async function(aState) {
let remember = false;
let silenceNotifications = false;
if (notificationSilencingEnabled && sharingScreen) {
silenceNotifications = aState && aState.checkboxChecked;
} else {
remember = aState && aState.checkboxChecked;
}
let allowedDevices = [];
let perms = Services.perms;
if (reqVideoInput) {
let listId = sharingScreen
? "webRTC-selectWindow-menulist"
: "webRTC-selectCamera-menulist";
let videoDeviceIndex = doc.getElementById(listId).value;
let allowVideoDevice = videoDeviceIndex != "-1";
if (allowVideoDevice) {
allowedDevices.push(videoDeviceIndex);
// Session permission will be removed after use
// (it's really one-shot, not for the entire session)
perms.addFromPrincipal(
principal,
"MediaManagerVideo",
perms.ALLOW_ACTION,
perms.EXPIRE_SESSION
);
let { mediaSource, rawId } = videoInputDevices.find(
({ deviceIndex }) => deviceIndex == videoDeviceIndex
);
aActor.activateDevicePerm(aRequest.windowID, mediaSource, rawId);
if (remember) {
lazy.SitePermissions.setForPrincipal(
principal,
"camera",
lazy.SitePermissions.ALLOW
);
}
}
}
if (reqAudioInput === "Microphone") {
let audioDeviceIndex = doc.getElementById(
"webRTC-selectMicrophone-menulist"
).value;
let allowMic = audioDeviceIndex != "-1";
if (allowMic) {
allowedDevices.push(audioDeviceIndex);
let { mediaSource, rawId } = audioInputDevices.find(
({ deviceIndex }) => deviceIndex == audioDeviceIndex
);
aActor.activateDevicePerm(aRequest.windowID, mediaSource, rawId);
if (remember) {
lazy.SitePermissions.setForPrincipal(
principal,
"microphone",
lazy.SitePermissions.ALLOW
);
}
}
} else if (reqAudioInput === "AudioCapture") {
// Only one device possible for audio capture.
allowedDevices.push(0);
}
if (reqAudioOutput) {
let audioDeviceIndex = doc.getElementById(
"webRTC-selectSpeaker-menulist"
).value;
let allowSpeaker = audioDeviceIndex != "-1";
if (allowSpeaker) {
allowedDevices.push(audioDeviceIndex);
let { id } = audioOutputDevices.find(
({ deviceIndex }) => deviceIndex == audioDeviceIndex
);
lazy.SitePermissions.setForPrincipal(
principal,
["speaker", id].join("^"),
lazy.SitePermissions.ALLOW
);
}
}
if (!allowedDevices.length) {
aActor.denyRequest(aRequest);
return;
}
const camNeeded = reqVideoInput === "Camera";
const micNeeded = !!reqAudioInput;
const scrNeeded = reqVideoInput === "Screen";
const havePermission = await aActor.checkOSPermission(
camNeeded,
micNeeded,
scrNeeded
);
if (!havePermission) {
aActor.denyRequestNoPermission(aRequest);
return;
}
aActor.sendAsyncMessage("webrtc:Allow", {
callID: aRequest.callID,
windowID: aRequest.windowID,
devices: allowedDevices,
suppressNotifications: silenceNotifications,
});
};
// If we haven't handled the permission yet, we want to show the doorhanger.
return false;
},
};
function shouldShowAlwaysRemember() {
// Don't offer "always remember" action in PB mode
if (lazy.PrivateBrowsingUtils.isBrowserPrivate(aBrowser)) {
return false;
}
// Don't offer "always remember" action in third party with no permission
// delegation
if (aRequest.isThirdPartyOrigin && !aRequest.shouldDelegatePermission) {
return false;
}
// Don't offer "always remember" action in maybe unsafe permission
// delegation
if (aRequest.shouldDelegatePermission && aRequest.secondOrigin) {
return false;
}
// "Always allow this speaker" not yet supported for
// selectAudioOutput(). Bug 1712892
if (reqAudioOutput) {
return false;
}
return true;
}
if (shouldShowAlwaysRemember()) {
// Disable the permanent 'Allow' action if the connection isn't secure, or for
// screen/audio sharing (because we can't guess which window the user wants to
// share without prompting). Note that we never enter this block for private
// browsing windows.
let reason = "";
if (sharingScreen) {
reason = "webrtc-reason-for-no-permanent-allow-screen";
} else if (sharingAudio) {
reason = "webrtc-reason-for-no-permanent-allow-audio";
} else if (!aRequest.secure) {
reason = "webrtc-reason-for-no-permanent-allow-insecure";
}
options.checkbox = {
label: localization.formatValueSync("webrtc-remember-allow-checkbox"),
checked: principal.isAddonOrExpandedAddonPrincipal,
checkedState: reason
? {
disableMainAction: true,
warningLabel: localization.formatValueSync(reason),
}
: undefined,
};
}
// If the notification silencing feature is enabled and we're sharing a
// screen, then the checkbox for the permission panel is what controls
// notification silencing.
if (notificationSilencingEnabled && sharingScreen) {
options.checkbox = {
label: localization.formatValueSync("webrtc-mute-notifications-checkbox"),
checked: false,
checkedState: {
disableMainAction: false,
},
};
}
let anchorId = "webRTC-shareDevices-notification-icon";
if (reqVideoInput === "Screen") {
anchorId = "webRTC-shareScreen-notification-icon";
} else if (!reqVideoInput) {
if (reqAudioInput && !reqAudioOutput) {
anchorId = "webRTC-shareMicrophone-notification-icon";
} else if (!reqAudioInput && reqAudioOutput) {
anchorId = "webRTC-shareSpeaker-notification-icon";
}
}
if (aRequest.secondOrigin) {
options.secondName = lazy.webrtcUI.getHostOrExtensionName(
null,
aRequest.secondOrigin
);
}
notification = chromeDoc.defaultView.PopupNotifications.show(
aBrowser,
"webRTC-shareDevices",
message,
anchorId,
mainAction,
secondaryActions,
options
);
notification.callID = aRequest.callID;
let schemeHistogram = Services.telemetry.getKeyedHistogramById(
"PERMISSION_REQUEST_ORIGIN_SCHEME"
);
let userInputHistogram = Services.telemetry.getKeyedHistogramById(
"PERMISSION_REQUEST_HANDLING_USER_INPUT"
);
let docURI = aRequest.documentURI;
let scheme = 0;
if (docURI.startsWith("https")) {
scheme = 2;
} else if (docURI.startsWith("http")) {
scheme = 1;
}
for (let requestType of requestTypes) {
if (requestType == "AudioCapture") {
requestType = "Microphone";
}
requestType = requestType.toLowerCase();
schemeHistogram.add(requestType, scheme);
userInputHistogram.add(requestType, aRequest.isHandlingUserInput);
}
}
/**
* @param {"Screen" | "Camera" | null} reqVideoInput
* @param {"AudioCapture" | "Microphone" | null} reqAudioInput
* @param {boolean} reqAudioOutput
* @param {boolean} delegation - Is the access delegated to a third party?
* @returns {string} Localization message identifier
*/
function getPromptMessageId(
reqVideoInput,
reqAudioInput,
reqAudioOutput,
delegation
) {
switch (reqVideoInput) {
case "Camera":
switch (reqAudioInput) {
case "Microphone":
return delegation
? "webrtc-allow-share-camera-and-microphone-unsafe-delegation"
: "webrtc-allow-share-camera-and-microphone";
case "AudioCapture":
return delegation
? "webrtc-allow-share-camera-and-audio-capture-unsafe-delegation"
: "webrtc-allow-share-camera-and-audio-capture";
default:
return delegation
? "webrtc-allow-share-camera-unsafe-delegation"
: "webrtc-allow-share-camera";
}
case "Screen":
switch (reqAudioInput) {
case "Microphone":
return delegation
? "webrtc-allow-share-screen-and-microphone-unsafe-delegation"
: "webrtc-allow-share-screen-and-microphone";
case "AudioCapture":
return delegation
? "webrtc-allow-share-screen-and-audio-capture-unsafe-delegation"
: "webrtc-allow-share-screen-and-audio-capture";
default:
return delegation
? "webrtc-allow-share-screen-unsafe-delegation"
: "webrtc-allow-share-screen";
}
default:
switch (reqAudioInput) {
case "Microphone":
return delegation
? "webrtc-allow-share-microphone-unsafe-delegation"
: "webrtc-allow-share-microphone";
case "AudioCapture":
return delegation
? "webrtc-allow-share-audio-capture-unsafe-delegation"
: "webrtc-allow-share-audio-capture";
default:
// This should be always true, if we've reached this far.
if (reqAudioOutput) {
return delegation
? "webrtc-allow-share-speaker-unsafe-delegation"
: "webrtc-allow-share-speaker";
}
return undefined;
}
}
}
/**
* Checks whether we have a microphone/camera in use by checking the activePerms map
* or if we have an allow permission for a microphone/camera in sitePermissions
* @param {Browser} browser - Browser to find all active and allowed microphone and camera devices for
* @return true if one of the above conditions is met
*/
function allowedOrActiveCameraOrMicrophone(browser) {
// Do we have an allow permission for cam/mic in the permissions manager?
if (
lazy.SitePermissions.getAllForBrowser(browser).some(perm => {
return (
perm.state == lazy.SitePermissions.ALLOW &&
(perm.id.startsWith("camera") || perm.id.startsWith("microphone"))
);
})
) {
// Return early, no need to check for active devices
return true;
}
// Do we have an active device?
return (
// Find all windowIDs that belong to our browsing contexts
browser.browsingContext
.getAllBrowsingContextsInSubtree()
// Only keep the outerWindowIds
.map(bc => bc.currentWindowGlobal?.outerWindowId)
.filter(id => id != null)
// We have an active device if one of our windowIds has a non empty map in the activePerms map
// that includes one device of type "camera" or "microphone"
.some(id => {
let map = lazy.webrtcUI.activePerms.get(id);
if (!map) {
// This windowId has no active device
return false;
}
// Let's see if one of the devices is a camera or a microphone
let types = [...map.values()];
return types.includes("microphone") || types.includes("camera");
})
);
}
function removePrompt(aBrowser, aCallId) {
let chromeWin = aBrowser.ownerGlobal;
let notification = chromeWin.PopupNotifications.getNotification(
"webRTC-shareDevices",
aBrowser
);
if (notification && notification.callID == aCallId) {
notification.remove();
}
}
/**
* Clears temporary permission grants used for WebRTC device grace periods.
* @param browser - Browser element to clear permissions for.
* @param {boolean} clearCamera - Clear camera grants.
* @param {boolean} clearMicrophone - Clear microphone grants.
*/
function clearTemporaryGrants(browser, clearCamera, clearMicrophone) {
if (!clearCamera && !clearMicrophone) {
// Nothing to clear.
return;
}
let perms = lazy.SitePermissions.getAllForBrowser(browser);
perms
.filter(perm => {
let [id, key] = perm.id.split(lazy.SitePermissions.PERM_KEY_DELIMITER);
// We only want to clear WebRTC grace periods. These are temporary, device
// specifc (double-keyed) microphone or camera permissions.
return (
key &&
perm.state == lazy.SitePermissions.ALLOW &&
perm.scope == lazy.SitePermissions.SCOPE_TEMPORARY &&
((clearCamera && id == "camera") ||
(clearMicrophone && id == "microphone"))
);
})
.forEach(perm =>
lazy.SitePermissions.removeFromPrincipal(null, perm.id, browser)
);
}
|