Skip to content

Commit caad5b2

Browse files
aveladjoeyparrish
authored andcommitted
perf: Use navigator.userAgentData.platform to detect Android and Fuchsia (#8102)
More info: https://wicg.github.io/ua-client-hints/#sec-ch-ua-platform
1 parent a4e0f1e commit caad5b2

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/util/platform.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ shaka.util.Platform = class {
543543
* @return {boolean}
544544
*/
545545
static isAndroid() {
546+
if (navigator.userAgentData && navigator.userAgentData.platform) {
547+
return navigator.userAgentData.platform.toLowerCase() == 'android';
548+
}
546549
return shaka.util.Platform.userAgentContains_('Android');
547550
}
548551

@@ -552,6 +555,9 @@ shaka.util.Platform = class {
552555
* @return {boolean}
553556
*/
554557
static isFuchsia() {
558+
if (navigator.userAgentData && navigator.userAgentData.platform) {
559+
return navigator.userAgentData.platform.toLowerCase() == 'fuchsia';
560+
}
555561
return shaka.util.Platform.userAgentContains_('Fuchsia');
556562
}
557563

test/polyfill/media_capabilities_unit.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ describe('MediaCapabilities', () => {
193193
pending('Unable to delete window.cast');
194194
}
195195

196+
spyOn(shaka['util']['Platform'], 'isAndroid').and.returnValue(false);
197+
196198
const isChromecastSpy =
197199
spyOn(shaka['util']['Platform'],
198200
'isChromecast').and.returnValue(true);
@@ -218,6 +220,7 @@ describe('MediaCapabilities', () => {
218220
async () => {
219221
// We only set the cast namespace, but not the canDisplayType() API.
220222
window['cast'] = {};
223+
spyOn(shaka['util']['Platform'], 'isAndroid').and.returnValue(false);
221224
const isChromecastSpy =
222225
spyOn(shaka['util']['Platform'],
223226
'isChromecast').and.returnValue(true);
@@ -247,6 +250,7 @@ describe('MediaCapabilities', () => {
247250
window['cast'] = {
248251
__platform__: {canDisplayType: mockCanDisplayType},
249252
};
253+
spyOn(shaka['util']['Platform'], 'isAndroid').and.returnValue(false);
250254
const isChromecastSpy =
251255
spyOn(shaka['util']['Platform'],
252256
'isChromecast').and.returnValue(true);

0 commit comments

Comments
 (0)