diff --git a/build/types/polyfill b/build/types/polyfill index 13eb749ead..f54e383ae6 100644 --- a/build/types/polyfill +++ b/build/types/polyfill @@ -13,6 +13,7 @@ +../../lib/polyfill/patchedmediakeys_nop.js +../../lib/polyfill/patchedmediakeys_webkit.js +../../lib/polyfill/pip_webkit.js ++../../lib/polyfill/random_uuid.js +../../lib/polyfill/storage_estimate.js +../../lib/polyfill/video_play_promise.js +../../lib/polyfill/videoplaybackquality.js diff --git a/externs/webcrypto.js b/externs/webcrypto.js new file mode 100644 index 0000000000..c4fce4c114 --- /dev/null +++ b/externs/webcrypto.js @@ -0,0 +1,18 @@ +/*! @license + * Shaka Player + * Copyright 2016 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + + +/** + * @fileoverview Externs for crypto properties not in the Closure compiler. + * + * @externs + */ + + +/** + * @return {string} + */ +webCrypto.Crypto.prototype.randomUUID = function() {}; diff --git a/lib/polyfill/random_uuid.js b/lib/polyfill/random_uuid.js new file mode 100644 index 0000000000..2d26b70821 --- /dev/null +++ b/lib/polyfill/random_uuid.js @@ -0,0 +1,52 @@ +/*! @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);