Source: lib/polyfill/random_uuid.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.polyfill.RandomUUID');
  7. goog.require('shaka.log');
  8. goog.require('shaka.polyfill');
  9. /**
  10. * @summary A polyfill to provide window.crypto.randomUUID in all browsers.
  11. * @export
  12. */
  13. shaka.polyfill.RandomUUID = class {
  14. /**
  15. * Install the polyfill if needed.
  16. * @export
  17. */
  18. static install() {
  19. shaka.log.debug('randomUUID.install');
  20. if (!window.crypto) {
  21. // See: https://caniuse.com/cryptography
  22. shaka.log.debug(
  23. 'window.crypto must be available to install randomUUID polyfill.');
  24. return;
  25. }
  26. if ('randomUUID' in window.crypto) {
  27. shaka.log.debug(
  28. 'RandomUUID: Native window.crypto.randomUUID() support found.');
  29. return;
  30. }
  31. window.crypto.randomUUID = shaka.polyfill.RandomUUID.randomUUID_;
  32. }
  33. /**
  34. * @return {string}
  35. * @private
  36. */
  37. static randomUUID_() {
  38. const url = URL.createObjectURL(new Blob());
  39. const uuid = url.toString();
  40. URL.revokeObjectURL(url);
  41. return uuid.substr(uuid.lastIndexOf('/') + 1);
  42. }
  43. };
  44. shaka.polyfill.register(shaka.polyfill.RandomUUID.install);