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";
10 ChromeUtils.defineESModuleGetters(lazy, {
11 BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.sys.mjs",
12 SitePermissions: "resource:///modules/SitePermissions.sys.mjs",
14 ChromeUtils.defineLazyGetter(
17 () => new Localization(["browser/webrtcIndicator.ftl"], true)
19 ChromeUtils.defineLazyGetter(
22 () => new Services.intl.ListFormat(undefined)
25 const SHARING_L10NID_BY_TYPE = new Map([
29 "webrtc-indicator-menuitem-sharing-camera-with",
30 "webrtc-indicator-menuitem-sharing-camera-with-n-tabs",
36 "webrtc-indicator-menuitem-sharing-microphone-with",
37 "webrtc-indicator-menuitem-sharing-microphone-with-n-tabs",
43 "webrtc-indicator-menuitem-sharing-application-with",
44 "webrtc-indicator-menuitem-sharing-application-with-n-tabs",
50 "webrtc-indicator-menuitem-sharing-screen-with",
51 "webrtc-indicator-menuitem-sharing-screen-with-n-tabs",
57 "webrtc-indicator-menuitem-sharing-window-with",
58 "webrtc-indicator-menuitem-sharing-window-with-n-tabs",
64 "webrtc-indicator-menuitem-sharing-browser-with",
65 "webrtc-indicator-menuitem-sharing-browser-with-n-tabs",
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"],
81 export var webrtcUI = {
84 peerConnectionBlockers: new Set(),
85 emitter: new EventEmitter(),
88 if (!this.initialized) {
89 Services.obs.addObserver(this, "browser-delayed-startup-finished");
90 this.initialized = true;
92 XPCOMUtils.defineLazyPreferenceGetter(
94 "deviceGracePeriodTimeoutMs",
95 "privacy.webrtc.deviceGracePeriodTimeoutMs"
97 XPCOMUtils.defineLazyPreferenceGetter(
99 "showIndicatorsOnMacos14AndAbove",
100 "privacy.webrtc.showIndicatorsOnMacos14AndAbove",
104 Services.telemetry.setEventRecordingEnabled("webrtc.ui", true);
109 if (this.initialized) {
110 Services.obs.removeObserver(this, "browser-delayed-startup-finished");
111 this.initialized = false;
115 observe(subject, topic) {
116 if (topic == "browser-delayed-startup-finished") {
117 if (webrtcUI.showGlobalIndicator) {
118 showOrCreateMenuForWindow(subject);
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) {
153 indicators.showCameraIndicator ||
154 indicators.showMicrophoneIndicator ||
155 indicators.showScreenSharingIndicator
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
167 AppConstants.isPlatformAndVersionAtLeast("macosx", 14.0) &&
168 !this.showIndicatorsOnMacos14AndAbove
173 for (let [, indicators] of this.perTabIndicators) {
174 if (indicators.showCameraIndicator) {
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
185 AppConstants.isPlatformAndVersionAtLeast("macosx", 14.0) &&
186 !this.showIndicatorsOnMacos14AndAbove
191 for (let [, indicators] of this.perTabIndicators) {
192 if (indicators.showMicrophoneIndicator) {
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
203 AppConstants.isPlatformAndVersionAtLeast("macosx", 14.0) &&
204 !this.showIndicatorsOnMacos14AndAbove
210 for (let [, indicators] of this.perTabIndicators) {
211 if (indicators.showScreenSharingIndicator) {
212 list.push(indicators.showScreenSharingIndicator);
216 let precedence = ["Screen", "Window", "Application", "Browser", ""];
218 list.sort((a, b) => {
219 return precedence.indexOf(a) - precedence.indexOf(b);
226 // The boolean parameters indicate which streams should be included in the result.
227 getActiveStreams(aCamera, aMicrophone, aScreen, aWindow = false) {
228 return webrtcUI._streams
230 let state = aStream.state;
232 (aCamera && state.camera) ||
233 (aMicrophone && state.microphone) ||
234 (aScreen && state.screen) ||
235 (aWindow && state.window)
239 let state = aStream.state;
241 camera: state.camera,
242 microphone: state.microphone,
243 screen: state.screen,
244 window: state.window,
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);
252 uri: state.documentURI,
256 devices: state.devices,
262 * Returns true if aBrowser has an active WebRTC stream.
264 browserHasStreams(aBrowser) {
265 for (let stream of this._streams) {
266 if (stream.topBrowsingContext.embedderElement == aBrowser) {
275 * Determine the combined state of all the active streams associated with
276 * the specified top-level browsing context.
278 getCombinedStateForBrowser(aTopBrowsingContext) {
279 function combine(x, y) {
281 x == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED ||
282 y == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED
284 return Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED;
287 x == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED ||
288 y == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED
290 return Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED;
292 return Ci.nsIMediaManagerService.STATE_NOCAPTURE;
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);
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";
321 let screenEnabled = tabState.screen && !tabState.screen.includes("Paused");
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).
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";
344 // The stream is considered paused when we're sharing something
345 // but all devices are off or set to disabled.
353 tabState.camera == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED ||
354 tabState.camera == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED
356 tabState.showCameraIndicator = true;
359 tabState.microphone == Ci.nsIMediaManagerService.STATE_CAPTURE_ENABLED ||
360 tabState.microphone == Ci.nsIMediaManagerService.STATE_CAPTURE_DISABLED
362 tabState.showMicrophoneIndicator = true;
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";
373 } else if (tabState.screen.startsWith("Browser")) {
374 if (!tabState.showScreenSharingIndicator) {
375 tabState.showScreenSharingIndicator = "Browser";
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.
389 streamAddedOrRemoved(aBrowsingContext, aData) {
393 for (index = 0; index < webrtcUI._streams.length; ++index) {
394 let stream = this._streams[index];
395 if (stream.browsingContext == aBrowsingContext) {
399 // The update is a removal of the stream, triggered by the
400 // recording-window-ended notification.
402 if (index < this._streams.length) {
403 this._streams.splice(index, 1);
406 this._streams[index] = {
407 browsingContext: aBrowsingContext,
408 topBrowsingContext: aBrowsingContext.top,
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
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;
442 if (mediaSource == "window") {
443 sharedWindowRawDeviceIds.add(device.rawId);
444 } else if (mediaSource == "screen") {
445 this.sharingScreen = true;
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.
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);
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) {
470 rawDeviceId = win.windowUtils.webrtcRawDeviceId;
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.
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;
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++;
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.
503 // The mixture of camelCase with under_score notation here is due to
504 // an unfortunate collision of conventions between this file and
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", {
512 } else if (device.mediaSource == "window") {
514 this.recordEvent("share_display", "browser_window", {
518 this.recordEvent("share_display", "window", {
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;
537 this._setSharedData();
539 Services.prefs.getBoolPref(
540 "privacy.webrtc.allowSilencingNotifications",
544 let alertsService = Cc["@mozilla.org/alerts-service;1"]
545 .getService(Ci.nsIAlertsService)
546 .QueryInterface(Ci.nsIAlertsDoNotDisturb);
547 alertsService.suppressForScreenSharing = suppressNotifications;
552 * Remove all the streams associated with a given
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);
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);
570 !tabState.showCameraIndicator &&
571 !tabState.showMicrophoneIndicator &&
572 !tabState.showScreenSharingIndicator
574 this.perTabIndicators.delete(topBC);
578 this.updateGlobalIndicator();
579 this._setSharedData();
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.
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.
591 * For camera and microphone streams, this will also revoke any associated
592 * permissions from SitePermissions.
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)
607 if (!activeStreams.length) {
616 ids.push("microphone");
618 if (stopScreens || stopWindows) {
622 for (let stream of activeStreams) {
623 let { browser } = stream;
625 let gBrowser = browser.getTabBrowser();
627 console.error("Can't stop sharing stream - cannot find gBrowser.");
631 let tab = gBrowser.getTabForBrowser(browser);
633 console.error("Can't stop sharing stream - cannot find tab.");
637 this.clearPermissionsAndStopSharing(ids, tab);
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);
648 gBrowser.selectedTab = tab;
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.
658 clearPermissionsAndStopSharing(types, tab) {
659 let invalidTypes = types.filter(
660 type => !["camera", "screen", "microphone", "speaker"].includes(type)
662 if (invalidTypes.length) {
663 throw new Error(`Invalid device types ${invalidTypes.join(",")}`);
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"));
680 let [id] = perm.id.split(lazy.SitePermissions.PERM_KEY_DELIMITER);
681 if (sharingCameraOrMic && (id == "camera" || id == "microphone")) {
684 return types.includes(id);
687 lazy.SitePermissions.removeFromPrincipal(
688 browser.contentPrincipal,
694 if (!sharingState?.windowId) {
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;
703 if (types.includes("screen") && sharingState.screen) {
704 windowIds.push(`screen:${windowId}`);
706 if (sharingCameraOrMic) {
707 windowIds.push(windowId);
710 if (!windowIds.length) {
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));
725 updateIndicators(aTopBrowsingContext) {
726 let tabState = this.getCombinedStateForBrowser(aTopBrowsingContext);
729 if (this.perTabIndicators.has(aTopBrowsingContext)) {
730 indicators = this.perTabIndicators.get(aTopBrowsingContext);
733 this.perTabIndicators.set(aTopBrowsingContext, indicators);
736 indicators.showCameraIndicator = tabState.showCameraIndicator;
737 indicators.showMicrophoneIndicator = tabState.showMicrophoneIndicator;
738 indicators.showScreenSharingIndicator = tabState.showScreenSharingIndicator;
739 this.updateGlobalIndicator();
744 swapBrowserForNotification(aOldBrowser, aNewBrowser) {
745 for (let stream of this._streams) {
746 if (stream.browser == aOldBrowser) {
747 stream.browser = aNewBrowser;
753 * Remove all entries from the activePerms map for a browser, including all
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.
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));
769 * Shows the Permission Panel for the tab associated with the provided
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.
775 showSharingDoorhanger(aActiveStream, aEvent) {
776 let browserWindow = aActiveStream.browser.ownerGlobal;
777 if (aActiveStream.tab) {
778 browserWindow.gBrowser.selectedTab = aActiveStream.tab;
780 aActiveStream.browser.focus();
782 browserWindow.focus();
784 if (AppConstants.platform == "macosx" && !Services.focus.activeWindow) {
785 browserWindow.addEventListener(
788 Services.tm.dispatchToMainThread(function () {
789 browserWindow.gPermissionPanel.openPopup(aEvent);
794 Cc["@mozilla.org/widget/macdocksupport;1"]
795 .getService(Ci.nsIMacDockSupport)
796 .activateApplication(true);
799 browserWindow.gPermissionPanel.openPopup(aEvent);
802 updateWarningLabel(aMenuList) {
803 let type = aMenuList.selectedItem.getAttribute("devicetype");
804 let document = aMenuList.ownerDocument;
805 document.getElementById("webRTC-all-windows-shared").hidden =
809 // Add-ons can override stock permission behavior by doing:
811 // webrtcUI.addPeerConnectionBlocker(function(aParams) {
812 // // new permission checking logic
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.
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);
837 removePeerConnectionBlocker(aCallback) {
838 this.peerConnectionBlockers.delete(aCallback);
842 return this.emitter.on(...args);
846 return this.emitter.off(...args);
849 getHostOrExtensionName(uri, href) {
853 uri = Services.io.newURI(href);
856 let addonPolicy = WebExtensionPolicy.getByURI(uri);
857 host = addonPolicy?.name ?? uri.hostPort;
861 if (uri && uri.scheme.toLowerCase() == "about") {
862 // For about URIs, just use the full spec, without any #hash parts.
863 host = uri.specIgnoringRef;
865 // This is unfortunate, but we should display *something*...
866 host = lazy.syncL10n.formatValueSync(
867 "webrtc-sharing-menuitem-unknown-host"
874 updateGlobalIndicator() {
875 for (let chromeWin of Services.wm.getEnumerator("navigator:browser")) {
876 if (this.showGlobalIndicator) {
877 showOrCreateMenuForWindow(chromeWin);
879 let doc = chromeWin.document;
880 let existingMenu = doc.getElementById("tabSharingMenu");
882 existingMenu.hidden = true;
884 if (AppConstants.platform == "macosx") {
885 let separator = doc.getElementById("tabSharingSeparator");
887 separator.hidden = true;
893 if (this.showGlobalIndicator) {
894 if (!gIndicatorWindow) {
895 gIndicatorWindow = getGlobalIndicator();
898 gIndicatorWindow.updateIndicatorState();
901 `error in gIndicatorWindow.updateIndicatorState(): ${err.message}`
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
912 gIndicatorWindow.closingInternally();
914 gIndicatorWindow.close();
915 gIndicatorWindow = null;
919 getWindowShareState(window) {
920 if (this.sharingScreen) {
921 return this.SHARING_SCREEN;
922 } else if (this.sharedBrowserWindows.has(window)) {
923 return this.SHARING_WINDOW;
925 return this.SHARING_NONE;
928 tabAddedWhileSharing(tab) {
929 this.allowedSharedBrowsers.add(tab.linkedBrowser.permanentKey);
932 shouldShowSharedTabWarning(tab) {
933 if (!tab || !tab.linkedBrowser) {
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);
951 this.tabSwitchCountForSession++;
953 !this.allowTabSwitchesForSession &&
954 !this.allowedSharedBrowsers.has(browser.permanentKey);
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;
967 recordEvent(type, object, args = {}) {
968 Services.telemetry.recordEvent(
972 this.sharingDisplaySessionId.toString(),
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.
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
994 Services.ppmm.sharedData.set(
995 "webrtcUI:isSharingScreen",
998 Services.ppmm.sharedData.set(
999 "webrtcUI:sharedTopInnerWindowIds",
1000 sharedTopInnerWindowIds
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(
1011 INDICATOR_CHROME_URI,
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.
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");
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;
1041 if (!activeStreams.length) {
1042 event.preventDefault();
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(
1059 "webrtc-indicator-menuitem-control-sharing"
1061 controlItem.stream = stream;
1062 controlItem.addEventListener("command", this);
1064 menu.appendChild(controlItem);
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,
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(
1079 "webrtc-indicator-menuitem-control-sharing-on",
1082 controlItem.stream = stream;
1083 controlItem.addEventListener("command", this);
1084 menu.appendChild(controlItem);
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;
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),
1103 menuitem.stream = streamInfo;
1104 menuitem.addEventListener("command", onTabSharingMenuPopupCommand);
1105 e.target.appendChild(menuitem);
1109 function onTabSharingMenuPopupHiding() {
1110 while (this.lastChild) {
1111 this.lastChild.remove();
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");
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);
1135 container = document.getElementById("main-menubar");
1136 insertionPoint = document.getElementById("helpMenu");
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);
1145 menu.hidden = false;
1146 if (AppConstants.platform == "macosx") {
1147 document.getElementById("tabSharingSeparator").hidden = false;
1152 var gIndicatorWindow = null;