Skip to content

Commit 01c39f6

Browse files
Add typing
1 parent c092391 commit 01c39f6

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

injected/src/features/web-compat.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class WebCompat extends ContentFeature {
132132
}
133133
// if (this.getFeatureSettingEnabled('deviceEnumeration')) {
134134
this.deviceEnumerationFix();
135-
//}
135+
// }
136136
}
137137

138138
/** Shim Web Share API in Android WebView */
@@ -761,6 +761,9 @@ export class WebCompat extends ContentFeature {
761761
}
762762
}
763763

764+
/**
765+
* Prevents device enumeration by returning an empty array when enabled
766+
*/
764767
preventDeviceEnumeration() {
765768
if (!window.MediaDevices) {
766769
return;
@@ -774,6 +777,9 @@ export class WebCompat extends ContentFeature {
774777
}
775778
if (disableDeviceEnumeration) {
776779
const enumerateDevicesProxy = new DDGProxy(this, MediaDevices.prototype, 'enumerateDevices', {
780+
/**
781+
* @returns {Promise<MediaDeviceInfo[]>}
782+
*/
777783
apply() {
778784
return Promise.resolve([]);
779785
},
@@ -782,49 +788,66 @@ export class WebCompat extends ContentFeature {
782788
}
783789
}
784790

791+
/**
792+
* Creates a valid MediaDeviceInfo object with required toJSON method
793+
* @param {'videoinput' | 'audioinput' | 'audiooutput'} kind - The device kind
794+
* @returns {MediaDeviceInfo}
795+
*/
796+
createMediaDeviceInfo(kind) {
797+
return {
798+
deviceId: 'default',
799+
kind: kind,
800+
label: '',
801+
groupId: 'default-group',
802+
toJSON() {
803+
return {
804+
deviceId: this.deviceId,
805+
kind: this.kind,
806+
label: this.label,
807+
groupId: this.groupId
808+
};
809+
}
810+
};
811+
}
812+
813+
/**
814+
* Fixes device enumeration to handle permission prompts gracefully
815+
*/
785816
deviceEnumerationFix() {
786817
if (!window.MediaDevices) {
787818
return;
788819
}
789820

790821
const enumerateDevicesProxy = new DDGProxy(this, MediaDevices.prototype, 'enumerateDevices', {
822+
/**
823+
* @param {MediaDevices['enumerateDevices']} target
824+
* @param {MediaDevices} thisArg
825+
* @param {Parameters<MediaDevices['enumerateDevices']>} args
826+
* @returns {Promise<MediaDeviceInfo[]>}
827+
*/
791828
apply: async (target, thisArg, args) => {
792829
try {
793-
debugger;
794830
// Request device enumeration information from native
831+
/** @type {{willPrompt: boolean, videoInput: boolean, audioInput: boolean, audioOutput: boolean}} */
795832
const response = await this.messaging.request(MSG_DEVICE_ENUMERATION, {});
796833

797834
// Check if native indicates that prompts would be required
798835
if (response.willPrompt) {
799836
// If prompts would be required, return a manipulated response
800837
// that includes the device types that are available
838+
/** @type {MediaDeviceInfo[]} */
801839
const devices = [];
802840

803841
if (response.videoInput) {
804-
devices.push({
805-
deviceId: 'default',
806-
kind: 'videoinput',
807-
label: '',
808-
groupId: 'default-group',
809-
});
842+
devices.push(this.createMediaDeviceInfo('videoinput'));
810843
}
811844

812845
if (response.audioInput) {
813-
devices.push({
814-
deviceId: 'default',
815-
kind: 'audioinput',
816-
label: '',
817-
groupId: 'default-group',
818-
});
846+
devices.push(this.createMediaDeviceInfo('audioinput'));
819847
}
820848

821849
if (response.audioOutput) {
822-
devices.push({
823-
deviceId: 'default',
824-
kind: 'audiooutput',
825-
label: '',
826-
groupId: 'default-group',
827-
});
850+
devices.push(this.createMediaDeviceInfo('audiooutput'));
828851
}
829852

830853
return Promise.resolve(devices);

0 commit comments

Comments
 (0)