mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-14 15:56:38 +03:00
a72adca9fd
Related to: #3662
53 lines
1.2 KiB
JavaScript
53 lines
1.2 KiB
JavaScript
/*! @license
|
|
* Shaka Player
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
goog.provide('shaka.polyfill.RandomUUID');
|
|
|
|
goog.require('shaka.log');
|
|
goog.require('shaka.polyfill');
|
|
|
|
/**
|
|
* @summary A polyfill to provide window.crypto.randomUUID in all browsers.
|
|
* @export
|
|
*/
|
|
shaka.polyfill.RandomUUID = class {
|
|
/**
|
|
* Install the polyfill if needed.
|
|
* @export
|
|
*/
|
|
static install() {
|
|
shaka.log.debug('randomUUID.install');
|
|
|
|
if (!window.crypto) {
|
|
// See: https://caniuse.com/cryptography
|
|
shaka.log.debug(
|
|
'window.crypto must be available to install randomUUID polyfill.');
|
|
return;
|
|
}
|
|
|
|
if ('randomUUID' in window.crypto) {
|
|
shaka.log.debug(
|
|
'RandomUUID: Native window.crypto.randomUUID() support found.');
|
|
return;
|
|
}
|
|
|
|
window.crypto.randomUUID = shaka.polyfill.RandomUUID.randomUUID_;
|
|
}
|
|
|
|
/**
|
|
* @return {string}
|
|
* @private
|
|
*/
|
|
static randomUUID_() {
|
|
const url = URL.createObjectURL(new Blob());
|
|
const uuid = url.toString();
|
|
URL.revokeObjectURL(url);
|
|
return uuid.substr(uuid.lastIndexOf('/') + 1);
|
|
}
|
|
};
|
|
|
|
|
|
shaka.polyfill.register(shaka.polyfill.RandomUUID.install);
|