Bug 1890689 Don't pretend to pre-buffer with DynamicResampler r=pehrsons
[gecko.git] / browser / modules / webrtcUI.sys.mjs
blobf270f89d77bf18c659f4ad353f3ac3311cc9a2d7
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 import { EventEmitter } from "resource:///modules/syncedtabs/EventEmitter.sys.mjs";
6 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
7 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
9 const lazy = {};
10 ChromeUtils.defineESModuleGetters(lazy, {
11   BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.sys.mjs",
12   SitePermissions: "resource:///modules/SitePermissions.sys.mjs",
13 });
14 ChromeUtils.defineLazyGetter(
15   lazy,
16   "syncL10n",
17   () => new Localization(["browser/webrtcIndicator.ftl"], true)
19 ChromeUtils.defineLazyGetter(
20   lazy,
21   "listFormat",
22   () => new Services.intl.ListFormat(undefined)
25 const SHARING_L10NID_BY_TYPE = new Map([
26   [
27     "Camera",
28     [
29       "webrtc-indicator-menuitem-sharing-camera-with",
30       "webrtc-indicator-menuitem-sharing-camera-with-n-tabs",
31     ],
32   ],
33   [
34     "Microphone",
35     [
36       "webrtc-indicator-menuitem-sharing-microphone-with",
37       "webrtc-indicator-menuitem-sharing-microphone-with-n-tabs",
38     ],
39   ],
40   [
41     "Application",
42     [
43       "webrtc-indicator-menuitem-sharing-application-with",
44       "webrtc-indicator-menuitem-sharing-application-with-n-tabs",
45     ],
46   ],
47   [
48     "Screen",
49     [
50       "webrtc-indicator-menuitem-sharing-screen-with",
51       "webrtc-indicator-menuitem-sharing-screen-with-n-tabs",
52     ],
53   ],
54   [
55     "Window",
56     [
57       "webrtc-indicator-menuitem-sharing-window-with",
58       "webrtc-indicator-menuitem-sharing-window-with-n-tabs",
59     ],
60   ],
61   [
62     "Browser",
63     [
64       "webrtc-indicator-menuitem-sharing-browser-with",
65       "webrtc-indicator-menuitem-sharing-browser-with-n-tabs",
66     ],
67   ],
68 ]);
70 // These identifiers are defined in MediaStreamTrack.webidl
71 const MEDIA_SOURCE_L10NID_BY_TYPE = new Map([
72   ["camera", "webrtc-item-camera"],
73   ["screen", "webrtc-item-screen"],
74   ["application", "webrtc-item-application"],
75   ["window", "webrtc-item-window"],
76   ["browser", "webrtc-item-browser"],
77   ["microphone", "webrtc-item-microphone"],
78   ["audioCapture", "webrtc-item-audio-capture"],
79 ]);
81 export var webrtcUI = {
82   initialized: false,
84   peerConnectionBlockers: new Set(),
85   emitter: new EventEmitter(),
87   init() {
88     if (!this.initialized) {
89       Services.obs.addObserver(this, "browser-delayed-startup-finished");
90       this.initialized = true;
92       XPCOMUtils.defineLazyPreferenceGetter(
93         this,
94         "deviceGracePeriodTimeoutMs",
95         "privacy.webrtc.deviceGracePeriodTimeoutMs"
96       );
97       XPCOMUtils.defineLazyPreferenceGetter(
98         this,
99         "showIndicatorsOnMacos14AndAbove",
100         "privacy.webrtc.showIndicatorsOnMacos14AndAbove",
101         true
102       );
104       Services.telemetry.setEventRecordingEnabled("webrtc.ui", true);
105     }
106   },
108   uninit() {
109     if (this.initialized) {
110       Services.obs.removeObserver(this, "browser-delayed-startup-finished");
111       this.initialized = false;
112     }
113   },
115   observe(subject, topic) {
116     if (topic == "browser-delayed-startup-finished") {
117       if (webrtcUI.showGlobalIndicator) {
118         showOrCreateMenuForWindow(subject);
119       }
120     }
121   },
123   SHARING_NONE: 0,
124   SHARING_WINDOW: 1,
125   SHARING_SCREEN: 2,
127   // Set of browser windows that are being shared over WebRTC.
128   sharedBrowserWindows: new WeakSet(),
130   // True if one or more screens is being shared.
131   sharingScreen: false,
133   allowedSharedBrowsers: new WeakSet(),
134   allowTabSwitchesForSession: false,
135   tabSwitchCountForSession: 0,
137   // True if a window or screen is being shared.
138   sharingDisplay: false,
140   // The session ID is used to try to differentiate between instances
141   // where the user is sharing their display somehow. If the user
142   // transitions from a state of not sharing their display, to sharing a
143   // display, we bump the ID.
144   sharingDisplaySessionId: 0,
146   // Map of browser elements to indicator data.
147   perTabIndicators: new Map(),
148   activePerms: new Map(),
150   get showGlobalIndicator() {
151     for (let [, indicators] of this.perTabIndicators) {
152       if (
153         indicators.showCameraIndicator ||
154         indicators.showMicrophoneIndicator ||
155         indicators.showScreenSharingIndicator
156       ) {
157         return true;
158       }
159     }
160     return false;
161   },
163   get showCameraIndicator() {
164     // Bug 1857254 - MacOS 14 displays two camera icons in menu bar
165     // temporarily disabled the firefox camera icon until a better fix comes around
166     if (
167       AppConstants.isPlatformAndVersionAtLeast("macosx", 14.0) &&
168       !this.showIndicatorsOnMacos14AndAbove
169     ) {
170       return false;
171     }
173     for (let [, indicators] of this.perTabIndicators) {
174       if (indicators.showCameraIndicator) {
175         return true;
176       }
177     }
178     return false;
179   },
181   get showMicrophoneIndicator() {
182     // Bug 1857254 - MacOS 14 displays two microphone icons in menu bar
183     // temporarily disabled the firefox camera icon until a better fix comes around
184     if (
185       AppConstants.isPlatformAndVersionAtLeast("macosx", 14.0) &&
186       !this.showIndicatorsOnMacos14AndAbove
187     ) {
188       return false;
189     }
191     for (let [, indicators] of this.perTabIndicators) {
192       if (indicators.showMicrophoneIndicator) {
193         return true;
194       }
195     }
196     return false;
197   },
199   get showScreenSharingIndicator() {
200     // Bug 1857254 - MacOS 14 displays two screen share icons in menu bar
201     // temporarily disabled the firefox camera icon until a better fix comes around
202     if (
203       AppConstants.isPlatformAndVersionAtLeast("macosx", 14.0) &&
204       !this.showIndicatorsOnMacos14AndAbove
205     ) {
206       return "";
207     }
209     let list = [""];
210     for (let [, indicators] of this.perTabIndicators) {
211       if (indicators.showScreenSharingIndicator) {
212         list.push(indicators.showScreenSharingIndicator);
213       }
214     }
216     let precedence = ["Screen", "Window", "Application", "Browser", ""];
218     list.sort((a, b) => {
219       return precedence.indexOf(a) - precedence.indexOf(b);
220     });
222     return list[0];
223   },
225   _streams: [],
226   // The boolean parameters indicate which streams should be included in the result.
227   getActiveStreams(aCamera, aMicrophone, aScreen, aWindow = false) {
228     return webrtcUI._streams
229       .filter(aStream => {
230         let state = aStream.state;
231         return (
232           (aCamera && state.camera) ||
233           (aMicrophone && state.microphone) ||
234           (aScreen && state.screen) ||
235           (aWindow && state.window)
236         );
237       })
238       .map(aStream => {
239         let state = aStream.state;
240         let types = {
241           camera: state.camera,
242           microphone: state.microphone,
243           screen: state.screen,
244           window: state.window,
245         };
246         let browser = aStream.topBrowsingContext.embedderElement;
247         // browser can be null when we are in the process of closing a tab
248         // and our stream list hasn't been updated yet.
249         // gBrowser will be null if a stream is used outside a tabbrowser window.
250         let tab = browser?.ownerGlobal.gBrowser?.getTabForBrowser(browser);
251         return {
252           uri: state.documentURI,
253           tab,
254           browser,
255           types,
256           devices: state.devices,
257         };
258       });
259   },
261   /**
262    * Returns true if aBrowser has an active WebRTC stream.
263    */
264   browserHasStreams(aBrowser) {
265     for (let stream of this._streams) {
266       if (stream.topBrowsingContext.embedderElement == aBrowser) {
267         return true;
268       }
269     }
271     return false;
272   },
274   /**
275    * Determine the combined state of all the active streams associated with
276    * the specified top-level browsing context.
277    */
278   getCombinedStateForBrowser(aTopBrowsingContext) {
279     function combine(x, y) {
280       if (
281         x == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED ||
282         y == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED
283       ) {
284         return Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED;
285       }
286       if (
287         x == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED ||
288         y == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED
289       ) {
290         return Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED;
291       }
292       return Ci.nsIMediaManagerService.STATE_NOCAPTURE;
293     }
295     let camera, microphone, screen, window, browser;
296     for (let stream of this._streams) {
297       if (stream.topBrowsingContext == aTopBrowsingContext) {
298         camera = combine(stream.state.camera, camera);
299         microphone = combine(stream.state.microphone, microphone);
300         screen = combine(stream.state.screen, screen);
301         window = combine(stream.state.window, window);
302         browser = combine(stream.state.browser, browser);
303       }
304     }
306     let tabState = { camera, microphone };
307     if (screen == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED) {
308       tabState.screen = "Screen";
309     } else if (window == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED) {
310       tabState.screen = "Window";
311     } else if (browser == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED) {
312       tabState.screen = "Browser";
313     } else if (screen == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED) {
314       tabState.screen = "ScreenPaused";
315     } else if (window == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED) {
316       tabState.screen = "WindowPaused";
317     } else if (browser == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED) {
318       tabState.screen = "BrowserPaused";
319     }
321     let screenEnabled = tabState.screen && !tabState.screen.includes("Paused");
322     let cameraEnabled =
323       tabState.camera == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED;
324     let microphoneEnabled =
325       tabState.microphone == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED;
327     // tabState.sharing controls which global indicator should be shown
328     // for the tab. It should always be set to the _enabled_ device which
329     // we consider most intrusive (screen > camera > microphone).
330     if (screenEnabled) {
331       tabState.sharing = "screen";
332     } else if (cameraEnabled) {
333       tabState.sharing = "camera";
334     } else if (microphoneEnabled) {
335       tabState.sharing = "microphone";
336     } else if (tabState.screen) {
337       tabState.sharing = "screen";
338     } else if (tabState.camera) {
339       tabState.sharing = "camera";
340     } else if (tabState.microphone) {
341       tabState.sharing = "microphone";
342     }
344     // The stream is considered paused when we're sharing something
345     // but all devices are off or set to disabled.
346     tabState.paused =
347       tabState.sharing &&
348       !screenEnabled &&
349       !cameraEnabled &&
350       !microphoneEnabled;
352     if (
353       tabState.camera == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED ||
354       tabState.camera == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED
355     ) {
356       tabState.showCameraIndicator = true;
357     }
358     if (
359       tabState.microphone == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED ||
360       tabState.microphone == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED
361     ) {
362       tabState.showMicrophoneIndicator = true;
363     }
365     tabState.showScreenSharingIndicator = "";
366     if (tabState.screen) {
367       if (tabState.screen.startsWith("Screen")) {
368         tabState.showScreenSharingIndicator = "Screen";
369       } else if (tabState.screen.startsWith("Window")) {
370         if (tabState.showScreenSharingIndicator != "Screen") {
371           tabState.showScreenSharingIndicator = "Window";
372         }
373       } else if (tabState.screen.startsWith("Browser")) {
374         if (!tabState.showScreenSharingIndicator) {
375           tabState.showScreenSharingIndicator = "Browser";
376         }
377       }
378     }
380     return tabState;
381   },
383   /*
384    * Indicate that a stream has been added or removed from the given
385    * browsing context. If it has been added, aData specifies the
386    * specific indicator types it uses. If aData is null or has no
387    * documentURI assigned, then the stream has been removed.
388    */
389   streamAddedOrRemoved(aBrowsingContext, aData) {
390     this.init();
392     let index;
393     for (index = 0; index < webrtcUI._streams.length; ++index) {
394       let stream = this._streams[index];
395       if (stream.browsingContext == aBrowsingContext) {
396         break;
397       }
398     }
399     // The update is a removal of the stream, triggered by the
400     // recording-window-ended notification.
401     if (aData.remove) {
402       if (index < this._streams.length) {
403         this._streams.splice(index, 1);
404       }
405     } else {
406       this._streams[index] = {
407         browsingContext: aBrowsingContext,
408         topBrowsingContext: aBrowsingContext.top,
409         state: aData,
410       };
411     }
413     let wasSharingDisplay = this.sharingDisplay;
415     // Reset our internal notion of whether or not we're sharing
416     // a screen or browser window. Now we'll go through the shared
417     // devices and re-determine what's being shared.
418     let sharingBrowserWindow = false;
419     let sharedWindowRawDeviceIds = new Set();
420     this.sharingDisplay = false;
421     this.sharingScreen = false;
422     let suppressNotifications = false;
424     // First, go through the streams and collect the counts on things
425     // like the total number of shared windows, and whether or not we're
426     // sharing screens.
427     for (let stream of this._streams) {
428       let { state } = stream;
429       suppressNotifications |= state.suppressNotifications;
431       for (let device of state.devices) {
432         let mediaSource = device.mediaSource;
434         if (mediaSource == "window" || mediaSource == "screen") {
435           this.sharingDisplay = true;
436         }
438         if (!device.scary) {
439           continue;
440         }
442         if (mediaSource == "window") {
443           sharedWindowRawDeviceIds.add(device.rawId);
444         } else if (mediaSource == "screen") {
445           this.sharingScreen = true;
446         }
448         // If the user has granted a particular site the ability
449         // to get a stream from a window or screen, we will
450         // presume that it's exempt from the tab switch warning.
451         //
452         // We use the permanentKey here so that the allowing of
453         // the tab survives tab tear-in and tear-out. We ignore
454         // browsers that don't have permanentKey, since those aren't
455         // tabbrowser browsers.
456         let browser = stream.topBrowsingContext.embedderElement;
457         if (browser.permanentKey) {
458           this.allowedSharedBrowsers.add(browser.permanentKey);
459         }
460       }
461     }
463     // Next, go through the list of shared windows, and map them
464     // to our browser windows so that we know which ones are shared.
465     this.sharedBrowserWindows = new WeakSet();
467     for (let win of lazy.BrowserWindowTracker.orderedWindows) {
468       let rawDeviceId;
469       try {
470         rawDeviceId = win.windowUtils.webrtcRawDeviceId;
471       } catch (e) {
472         // This can theoretically throw if some of the underlying
473         // window primitives don't exist. In that case, we can skip
474         // to the next window.
475         continue;
476       }
477       if (sharedWindowRawDeviceIds.has(rawDeviceId)) {
478         this.sharedBrowserWindows.add(win);
480         // If we've shared a window, then the initially selected tab
481         // in that window should be exempt from tab switch warnings,
482         // since it's already been shared.
483         let selectedBrowser = win.gBrowser.selectedBrowser;
484         this.allowedSharedBrowsers.add(selectedBrowser.permanentKey);
486         sharingBrowserWindow = true;
487       }
488     }
490     // If we weren't sharing a window or screen, and now are, bump
491     // the sharingDisplaySessionId. We use this ID for Event
492     // telemetry, and consider a transition from no shared displays
493     // to some shared displays as a new session.
494     if (!wasSharingDisplay && this.sharingDisplay) {
495       this.sharingDisplaySessionId++;
496     }
498     // If we were adding a new display stream, record some Telemetry for
499     // it with the most recent sharedDisplaySessionId. We do this separately
500     // from the loops above because those take into account the pre-existing
501     // streams that might already have been shared.
502     if (aData.devices) {
503       // The mixture of camelCase with under_score notation here is due to
504       // an unfortunate collision of conventions between this file and
505       // Event Telemetry.
506       let silence_notifs = suppressNotifications ? "true" : "false";
507       for (let device of aData.devices) {
508         if (device.mediaSource == "screen") {
509           this.recordEvent("share_display", "screen", {
510             silence_notifs,
511           });
512         } else if (device.mediaSource == "window") {
513           if (device.scary) {
514             this.recordEvent("share_display", "browser_window", {
515               silence_notifs,
516             });
517           } else {
518             this.recordEvent("share_display", "window", {
519               silence_notifs,
520             });
521           }
522         }
523       }
524     }
526     // Since we're not sharing a screen or browser window,
527     // we can clear these state variables, which are used
528     // to warn users on tab switching when sharing. These
529     // are safe to reset even if we hadn't been sharing
530     // the screen or browser window already.
531     if (!this.sharingScreen && !sharingBrowserWindow) {
532       this.allowedSharedBrowsers = new WeakSet();
533       this.allowTabSwitchesForSession = false;
534       this.tabSwitchCountForSession = 0;
535     }
537     this._setSharedData();
538     if (
539       Services.prefs.getBoolPref(
540         "privacy.webrtc.allowSilencingNotifications",
541         false
542       )
543     ) {
544       let alertsService = Cc["@mozilla.org/alerts-service;1"]
545         .getService(Ci.nsIAlertsService)
546         .QueryInterface(Ci.nsIAlertsDoNotDisturb);
547       alertsService.suppressForScreenSharing = suppressNotifications;
548     }
549   },
551   /**
552    * Remove all the streams associated with a given
553    * browsing context.
554    */
555   forgetStreamsFromBrowserContext(aBrowsingContext) {
556     for (let index = 0; index < webrtcUI._streams.length; ) {
557       let stream = this._streams[index];
558       if (stream.browsingContext == aBrowsingContext) {
559         this._streams.splice(index, 1);
560       } else {
561         index++;
562       }
563     }
565     // Remove the per-tab indicator if it no longer needs to be displayed.
566     let topBC = aBrowsingContext.top;
567     if (this.perTabIndicators.has(topBC)) {
568       let tabState = this.getCombinedStateForBrowser(topBC);
569       if (
570         !tabState.showCameraIndicator &&
571         !tabState.showMicrophoneIndicator &&
572         !tabState.showScreenSharingIndicator
573       ) {
574         this.perTabIndicators.delete(topBC);
575       }
576     }
578     this.updateGlobalIndicator();
579     this._setSharedData();
580   },
582   /**
583    * Given some set of streams, stops device access for those streams.
584    * Optionally, it's possible to stop a subset of the devices on those
585    * streams by passing in optional arguments.
586    *
587    * Once the streams have been stopped, this method will also find the
588    * newest stream's <xul:browser> and window, focus the window, and
589    * select the browser.
590    *
591    * For camera and microphone streams, this will also revoke any associated
592    * permissions from SitePermissions.
593    *
594    * @param {Array<Object>} activeStreams - An array of streams obtained via webrtcUI.getActiveStreams.
595    * @param {boolean} stopCameras - True to stop the camera streams (defaults to true)
596    * @param {boolean} stopMics - True to stop the microphone streams (defaults to true)
597    * @param {boolean} stopScreens - True to stop the screen streams (defaults to true)
598    * @param {boolean} stopWindows - True to stop the window streams (defaults to true)
599    */
600   stopSharingStreams(
601     activeStreams,
602     stopCameras = true,
603     stopMics = true,
604     stopScreens = true,
605     stopWindows = true
606   ) {
607     if (!activeStreams.length) {
608       return;
609     }
611     let ids = [];
612     if (stopCameras) {
613       ids.push("camera");
614     }
615     if (stopMics) {
616       ids.push("microphone");
617     }
618     if (stopScreens || stopWindows) {
619       ids.push("screen");
620     }
622     for (let stream of activeStreams) {
623       let { browser } = stream;
625       let gBrowser = browser.getTabBrowser();
626       if (!gBrowser) {
627         console.error("Can't stop sharing stream - cannot find gBrowser.");
628         continue;
629       }
631       let tab = gBrowser.getTabForBrowser(browser);
632       if (!tab) {
633         console.error("Can't stop sharing stream - cannot find tab.");
634         continue;
635       }
637       this.clearPermissionsAndStopSharing(ids, tab);
638     }
640     // Switch to the newest stream's browser.
641     let mostRecentStream = activeStreams[activeStreams.length - 1];
642     let { browser: browserToSelect } = mostRecentStream;
644     let window = browserToSelect.ownerGlobal;
645     let gBrowser = browserToSelect.getTabBrowser();
646     let tab = gBrowser.getTabForBrowser(browserToSelect);
647     window.focus();
648     gBrowser.selectedTab = tab;
649   },
651   /**
652    * Clears permissions and stops sharing (if active) for a list of device types
653    * and a specific tab.
654    * @param {("camera"|"microphone"|"screen")[]} types - Device types to stop
655    * and clear permissions for.
656    * @param tab - Tab of the devices to stop and clear permissions.
657    */
658   clearPermissionsAndStopSharing(types, tab) {
659     let invalidTypes = types.filter(
660       type => !["camera", "screen", "microphone", "speaker"].includes(type)
661     );
662     if (invalidTypes.length) {
663       throw new Error(`Invalid device types ${invalidTypes.join(",")}`);
664     }
665     let browser = tab.linkedBrowser;
666     let sharingState = tab._sharingState?.webRTC;
668     // If we clear a WebRTC permission we need to remove all permissions of
669     // the same type across device ids. We also need to stop active WebRTC
670     // devices related to the permission.
671     let perms = lazy.SitePermissions.getAllForBrowser(browser);
673     // If capturing, don't revoke one of camera/microphone without the other.
674     let sharingCameraOrMic =
675       (sharingState?.camera || sharingState?.microphone) &&
676       (types.includes("camera") || types.includes("microphone"));
678     perms
679       .filter(perm => {
680         let [id] = perm.id.split(lazy.SitePermissions.PERM_KEY_DELIMITER);
681         if (sharingCameraOrMic && (id == "camera" || id == "microphone")) {
682           return true;
683         }
684         return types.includes(id);
685       })
686       .forEach(perm => {
687         lazy.SitePermissions.removeFromPrincipal(
688           browser.contentPrincipal,
689           perm.id,
690           browser
691         );
692       });
694     if (!sharingState?.windowId) {
695       return;
696     }
698     // If the device of the permission we're clearing is currently active,
699     // tell the WebRTC implementation to stop sharing it.
700     let { windowId } = sharingState;
702     let windowIds = [];
703     if (types.includes("screen") && sharingState.screen) {
704       windowIds.push(`screen:${windowId}`);
705     }
706     if (sharingCameraOrMic) {
707       windowIds.push(windowId);
708     }
710     if (!windowIds.length) {
711       return;
712     }
714     let actor =
715       sharingState.browsingContext.currentWindowGlobal.getActor("WebRTC");
717     // Delete activePerms for all outerWindowIds under the current browser. We
718     // need to do this prior to sending the stopSharing message, so WebRTCParent
719     // can skip adding grace periods for these devices.
720     webrtcUI.forgetActivePermissionsFromBrowser(browser);
722     windowIds.forEach(id => actor.sendAsyncMessage("webrtc:StopSharing", id));
723   },
725   updateIndicators(aTopBrowsingContext) {
726     let tabState = this.getCombinedStateForBrowser(aTopBrowsingContext);
728     let indicators;
729     if (this.perTabIndicators.has(aTopBrowsingContext)) {
730       indicators = this.perTabIndicators.get(aTopBrowsingContext);
731     } else {
732       indicators = {};
733       this.perTabIndicators.set(aTopBrowsingContext, indicators);
734     }
736     indicators.showCameraIndicator = tabState.showCameraIndicator;
737     indicators.showMicrophoneIndicator = tabState.showMicrophoneIndicator;
738     indicators.showScreenSharingIndicator = tabState.showScreenSharingIndicator;
739     this.updateGlobalIndicator();
741     return tabState;
742   },
744   swapBrowserForNotification(aOldBrowser, aNewBrowser) {
745     for (let stream of this._streams) {
746       if (stream.browser == aOldBrowser) {
747         stream.browser = aNewBrowser;
748       }
749     }
750   },
752   /**
753    * Remove all entries from the activePerms map for a browser, including all
754    * child frames.
755    * Note: activePerms is an internal WebRTC UI permission map and does not
756    * reflect the PermissionManager or SitePermissions state.
757    * @param aBrowser - Browser to clear active permissions for.
758    */
759   forgetActivePermissionsFromBrowser(aBrowser) {
760     let browserWindowIds = aBrowser.browsingContext
761       .getAllBrowsingContextsInSubtree()
762       .map(bc => bc.currentWindowGlobal?.outerWindowId)
763       .filter(id => id != null);
764     browserWindowIds.push(aBrowser.outerWindowId);
765     browserWindowIds.forEach(id => this.activePerms.delete(id));
766   },
768   /**
769    * Shows the Permission Panel for the tab associated with the provided
770    * active stream.
771    * @param aActiveStream - The stream that the user wants to see permissions for.
772    * @param aEvent - The user input event that is invoking the panel. This can be
773    *        undefined / null if no such event exists.
774    */
775   showSharingDoorhanger(aActiveStream, aEvent) {
776     let browserWindow = aActiveStream.browser.ownerGlobal;
777     if (aActiveStream.tab) {
778       browserWindow.gBrowser.selectedTab = aActiveStream.tab;
779     } else {
780       aActiveStream.browser.focus();
781     }
782     browserWindow.focus();
784     if (AppConstants.platform == "macosx" && !Services.focus.activeWindow) {
785       browserWindow.addEventListener(
786         "activate",
787         function () {
788           Services.tm.dispatchToMainThread(function () {
789             browserWindow.gPermissionPanel.openPopup(aEvent);
790           });
791         },
792         { once: true }
793       );
794       Cc["@mozilla.org/widget/macdocksupport;1"]
795         .getService(Ci.nsIMacDockSupport)
796         .activateApplication(true);
797       return;
798     }
799     browserWindow.gPermissionPanel.openPopup(aEvent);
800   },
802   updateWarningLabel(aMenuList) {
803     let type = aMenuList.selectedItem.getAttribute("devicetype");
804     let document = aMenuList.ownerDocument;
805     document.getElementById("webRTC-all-windows-shared").hidden =
806       type != "screen";
807   },
809   // Add-ons can override stock permission behavior by doing:
810   //
811   //   webrtcUI.addPeerConnectionBlocker(function(aParams) {
812   //     // new permission checking logic
813   //   }));
814   //
815   // The blocking function receives an object with origin, callID, and windowID
816   // parameters.  If it returns the string "deny" or a Promise that resolves
817   // to "deny", the connection is immediately blocked.  With any other return
818   // value (though the string "allow" is suggested for consistency), control
819   // is passed to other registered blockers.  If no registered blockers block
820   // the connection (or of course if there are no registered blockers), then
821   // the connection is allowed.
822   //
823   // Add-ons may also use webrtcUI.on/off to listen to events without
824   // blocking anything:
825   //   peer-request-allowed is emitted when a new peer connection is
826   //                        established (and not blocked).
827   //   peer-request-blocked is emitted when a peer connection request is
828   //                        blocked by some blocking connection handler.
829   //   peer-request-cancel is emitted when a peer-request connection request
830   //                       is canceled.  (This would typically be used in
831   //                       conjunction with a blocking handler to cancel
832   //                       a user prompt or other work done by the handler)
833   addPeerConnectionBlocker(aCallback) {
834     this.peerConnectionBlockers.add(aCallback);
835   },
837   removePeerConnectionBlocker(aCallback) {
838     this.peerConnectionBlockers.delete(aCallback);
839   },
841   on(...args) {
842     return this.emitter.on(...args);
843   },
845   off(...args) {
846     return this.emitter.off(...args);
847   },
849   getHostOrExtensionName(uri, href) {
850     let host;
851     try {
852       if (!uri) {
853         uri = Services.io.newURI(href);
854       }
856       let addonPolicy = WebExtensionPolicy.getByURI(uri);
857       host = addonPolicy?.name ?? uri.hostPort;
858     } catch (ex) {}
860     if (!host) {
861       if (uri && uri.scheme.toLowerCase() == "about") {
862         // For about URIs, just use the full spec, without any #hash parts.
863         host = uri.specIgnoringRef;
864       } else {
865         // This is unfortunate, but we should display *something*...
866         host = lazy.syncL10n.formatValueSync(
867           "webrtc-sharing-menuitem-unknown-host"
868         );
869       }
870     }
871     return host;
872   },
874   updateGlobalIndicator() {
875     for (let chromeWin of Services.wm.getEnumerator("navigator:browser")) {
876       if (this.showGlobalIndicator) {
877         showOrCreateMenuForWindow(chromeWin);
878       } else {
879         let doc = chromeWin.document;
880         let existingMenu = doc.getElementById("tabSharingMenu");
881         if (existingMenu) {
882           existingMenu.hidden = true;
883         }
884         if (AppConstants.platform == "macosx") {
885           let separator = doc.getElementById("tabSharingSeparator");
886           if (separator) {
887             separator.hidden = true;
888           }
889         }
890       }
891     }
893     if (this.showGlobalIndicator) {
894       if (!gIndicatorWindow) {
895         gIndicatorWindow = getGlobalIndicator();
896       } else {
897         try {
898           gIndicatorWindow.updateIndicatorState();
899         } catch (err) {
900           console.error(
901             `error in gIndicatorWindow.updateIndicatorState(): ${err.message}`
902           );
903         }
904       }
905     } else if (gIndicatorWindow) {
906       if (gIndicatorWindow.closingInternally) {
907         // Before calling .close(), we call .closingInternally() to allow us to
908         // differentiate between situations where the indicator closes because
909         // we no longer want to show the indicator (this case), and cases where
910         // the user has found a way to close the indicator via OS window control
911         // mechanisms.
912         gIndicatorWindow.closingInternally();
913       }
914       gIndicatorWindow.close();
915       gIndicatorWindow = null;
916     }
917   },
919   getWindowShareState(window) {
920     if (this.sharingScreen) {
921       return this.SHARING_SCREEN;
922     } else if (this.sharedBrowserWindows.has(window)) {
923       return this.SHARING_WINDOW;
924     }
925     return this.SHARING_NONE;
926   },
928   tabAddedWhileSharing(tab) {
929     this.allowedSharedBrowsers.add(tab.linkedBrowser.permanentKey);
930   },
932   shouldShowSharedTabWarning(tab) {
933     if (!tab || !tab.linkedBrowser) {
934       return false;
935     }
937     let browser = tab.linkedBrowser;
938     // We want the user to be able to switch to one tab after starting
939     // to share their window or screen. The presumption here is that
940     // most users will have a single window with multiple tabs, where
941     // the selected tab will be the one with the screen or window
942     // sharing web application, and it's most likely that the contents
943     // that the user wants to share are in another tab that they'll
944     // switch to immediately upon sharing. These presumptions are based
945     // on research that our user research team did with users using
946     // video conferencing web applications.
947     if (!this.tabSwitchCountForSession) {
948       this.allowedSharedBrowsers.add(browser.permanentKey);
949     }
951     this.tabSwitchCountForSession++;
952     let shouldShow =
953       !this.allowTabSwitchesForSession &&
954       !this.allowedSharedBrowsers.has(browser.permanentKey);
956     return shouldShow;
957   },
959   allowSharedTabSwitch(tab, allowForSession) {
960     let browser = tab.linkedBrowser;
961     let gBrowser = browser.getTabBrowser();
962     this.allowedSharedBrowsers.add(browser.permanentKey);
963     gBrowser.selectedTab = tab;
964     this.allowTabSwitchesForSession = allowForSession;
965   },
967   recordEvent(type, object, args = {}) {
968     Services.telemetry.recordEvent(
969       "webrtc.ui",
970       type,
971       object,
972       this.sharingDisplaySessionId.toString(),
973       args
974     );
975   },
977   /**
978    * Updates the sharedData structure to reflect shared screen and window
979    * state. This sets the following key: data pairs on sharedData.
980    * - "webrtcUI:isSharingScreen": a boolean value reflecting
981    * this.sharingScreen.
982    * - "webrtcUI:sharedTopInnerWindowIds": a set containing the inner window
983    * ids of each top level browser window that is in sharedBrowserWindows.
984    */
985   _setSharedData() {
986     let sharedTopInnerWindowIds = new Set();
987     for (let win of lazy.BrowserWindowTracker.orderedWindows) {
988       if (this.sharedBrowserWindows.has(win)) {
989         sharedTopInnerWindowIds.add(
990           win.browsingContext.currentWindowGlobal.innerWindowId
991         );
992       }
993     }
994     Services.ppmm.sharedData.set(
995       "webrtcUI:isSharingScreen",
996       this.sharingScreen
997     );
998     Services.ppmm.sharedData.set(
999       "webrtcUI:sharedTopInnerWindowIds",
1000       sharedTopInnerWindowIds
1001     );
1002   },
1005 function getGlobalIndicator() {
1006   const INDICATOR_CHROME_URI = "chrome://browser/content/webrtcIndicator.xhtml";
1007   let features = "chrome,titlebar=no,alwaysontop,minimizable,dialog";
1009   return Services.ww.openWindow(
1010     null,
1011     INDICATOR_CHROME_URI,
1012     "_blank",
1013     features,
1014     null
1015   );
1019  * Add a localized stream sharing menu to the event target
1021  * @param {Window} win - The parent `window`
1022  * @param {Event} event - The popupshowing event for the <menu>.
1023  * @param {boolean} inclWindow - Should the window stream be included in the active streams.
1024  */
1025 export function showStreamSharingMenu(win, event, inclWindow = false) {
1026   win.MozXULElement.insertFTLIfNeeded("browser/webrtcIndicator.ftl");
1027   const doc = win.document;
1028   const menu = event.target;
1030   let type = menu.getAttribute("type");
1031   let activeStreams;
1032   if (type == "Camera") {
1033     activeStreams = webrtcUI.getActiveStreams(true, false, false);
1034   } else if (type == "Microphone") {
1035     activeStreams = webrtcUI.getActiveStreams(false, true, false);
1036   } else if (type == "Screen") {
1037     activeStreams = webrtcUI.getActiveStreams(false, false, true, inclWindow);
1038     type = webrtcUI.showScreenSharingIndicator;
1039   }
1041   if (!activeStreams.length) {
1042     event.preventDefault();
1043     return;
1044   }
1046   const l10nIds = SHARING_L10NID_BY_TYPE.get(type) ?? [];
1047   if (activeStreams.length == 1) {
1048     let stream = activeStreams[0];
1050     const sharingItem = doc.createXULElement("menuitem");
1051     const streamTitle = stream.browser.contentTitle || stream.uri;
1052     doc.l10n.setAttributes(sharingItem, l10nIds[0], { streamTitle });
1053     sharingItem.setAttribute("disabled", "true");
1054     menu.appendChild(sharingItem);
1056     const controlItem = doc.createXULElement("menuitem");
1057     doc.l10n.setAttributes(
1058       controlItem,
1059       "webrtc-indicator-menuitem-control-sharing"
1060     );
1061     controlItem.stream = stream;
1062     controlItem.addEventListener("command", this);
1064     menu.appendChild(controlItem);
1065   } else {
1066     // We show a different menu when there are several active streams.
1067     const sharingItem = doc.createXULElement("menuitem");
1068     doc.l10n.setAttributes(sharingItem, l10nIds[1], {
1069       tabCount: activeStreams.length,
1070     });
1071     sharingItem.setAttribute("disabled", "true");
1072     menu.appendChild(sharingItem);
1074     for (let stream of activeStreams) {
1075       const controlItem = doc.createXULElement("menuitem");
1076       const streamTitle = stream.browser.contentTitle || stream.uri;
1077       doc.l10n.setAttributes(
1078         controlItem,
1079         "webrtc-indicator-menuitem-control-sharing-on",
1080         { streamTitle }
1081       );
1082       controlItem.stream = stream;
1083       controlItem.addEventListener("command", this);
1084       menu.appendChild(controlItem);
1085     }
1086   }
1089 function onTabSharingMenuPopupShowing(e) {
1090   const streams = webrtcUI.getActiveStreams(true, true, true, true);
1091   for (let streamInfo of streams) {
1092     const names = streamInfo.devices.map(({ mediaSource }) => {
1093       const l10nId = MEDIA_SOURCE_L10NID_BY_TYPE.get(mediaSource);
1094       return l10nId ? lazy.syncL10n.formatValueSync(l10nId) : mediaSource;
1095     });
1097     const doc = e.target.ownerDocument;
1098     const menuitem = doc.createXULElement("menuitem");
1099     doc.l10n.setAttributes(menuitem, "webrtc-sharing-menuitem", {
1100       origin: webrtcUI.getHostOrExtensionName(null, streamInfo.uri),
1101       itemList: lazy.listFormat.format(names),
1102     });
1103     menuitem.stream = streamInfo;
1104     menuitem.addEventListener("command", onTabSharingMenuPopupCommand);
1105     e.target.appendChild(menuitem);
1106   }
1109 function onTabSharingMenuPopupHiding() {
1110   while (this.lastChild) {
1111     this.lastChild.remove();
1112   }
1115 function onTabSharingMenuPopupCommand(e) {
1116   webrtcUI.showSharingDoorhanger(e.target.stream, e);
1119 function showOrCreateMenuForWindow(aWindow) {
1120   let document = aWindow.document;
1121   let menu = document.getElementById("tabSharingMenu");
1122   if (!menu) {
1123     menu = document.createXULElement("menu");
1124     menu.id = "tabSharingMenu";
1125     document.l10n.setAttributes(menu, "webrtc-sharing-menu");
1127     let container, insertionPoint;
1128     if (AppConstants.platform == "macosx") {
1129       container = document.getElementById("menu_ToolsPopup");
1130       insertionPoint = document.getElementById("devToolsSeparator");
1131       let separator = document.createXULElement("menuseparator");
1132       separator.id = "tabSharingSeparator";
1133       container.insertBefore(separator, insertionPoint);
1134     } else {
1135       container = document.getElementById("main-menubar");
1136       insertionPoint = document.getElementById("helpMenu");
1137     }
1138     let popup = document.createXULElement("menupopup");
1139     popup.id = "tabSharingMenuPopup";
1140     popup.addEventListener("popupshowing", onTabSharingMenuPopupShowing);
1141     popup.addEventListener("popuphiding", onTabSharingMenuPopupHiding);
1142     menu.appendChild(popup);
1143     container.insertBefore(menu, insertionPoint);
1144   } else {
1145     menu.hidden = false;
1146     if (AppConstants.platform == "macosx") {
1147       document.getElementById("tabSharingSeparator").hidden = false;
1148     }
1149   }
1152 var gIndicatorWindow = null;