Source: lib/polyfill/patchedmediakeys_nop.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.polyfill.PatchedMediaKeysNop');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.log');
  9. goog.require('shaka.polyfill');
  10. /**
  11. * @summary A polyfill to stub out
  12. * {@link https://bit.ly/EmeMar15 EME draft 12 March 2015} on browsers without
  13. * EME.
  14. * All methods will fail.
  15. * @export
  16. */
  17. shaka.polyfill.PatchedMediaKeysNop = class {
  18. /**
  19. * Installs the polyfill if needed.
  20. * @export
  21. */
  22. static install() {
  23. if (!window.HTMLVideoElement ||
  24. (navigator.requestMediaKeySystemAccess &&
  25. // eslint-disable-next-line no-restricted-syntax
  26. MediaKeySystemAccess.prototype.getConfiguration)) {
  27. return;
  28. }
  29. shaka.log.info('EME not available.');
  30. // Alias.
  31. const PatchedMediaKeysNop = shaka.polyfill.PatchedMediaKeysNop;
  32. // Install patches.
  33. navigator.requestMediaKeySystemAccess =
  34. PatchedMediaKeysNop.requestMediaKeySystemAccess;
  35. // Delete mediaKeys to work around strict mode compatibility issues.
  36. // eslint-disable-next-line no-restricted-syntax
  37. delete HTMLMediaElement.prototype['mediaKeys'];
  38. // Work around read-only declaration for mediaKeys by using a string.
  39. // eslint-disable-next-line no-restricted-syntax
  40. HTMLMediaElement.prototype['mediaKeys'] = null;
  41. // eslint-disable-next-line no-restricted-syntax
  42. HTMLMediaElement.prototype.setMediaKeys = PatchedMediaKeysNop.setMediaKeys;
  43. // These are not usable, but allow Player.isBrowserSupported to pass.
  44. window.MediaKeys = PatchedMediaKeysNop.MediaKeys;
  45. window.MediaKeySystemAccess = PatchedMediaKeysNop.MediaKeySystemAccess;
  46. window.shakaMediaKeysPolyfill = PatchedMediaKeysNop.apiName_;
  47. }
  48. /**
  49. * An implementation of navigator.requestMediaKeySystemAccess.
  50. * Retrieves a MediaKeySystemAccess object.
  51. *
  52. * @this {!Navigator}
  53. * @param {string} keySystem
  54. * @param {!Array.<!MediaKeySystemConfiguration>} supportedConfigurations
  55. * @return {!Promise.<!MediaKeySystemAccess>}
  56. */
  57. static requestMediaKeySystemAccess(keySystem, supportedConfigurations) {
  58. shaka.log.debug('PatchedMediaKeysNop.requestMediaKeySystemAccess');
  59. goog.asserts.assert(this == navigator,
  60. 'bad "this" for requestMediaKeySystemAccess');
  61. return Promise.reject(new Error(
  62. 'The key system specified is not supported.'));
  63. }
  64. /**
  65. * An implementation of HTMLMediaElement.prototype.setMediaKeys.
  66. * Attaches a MediaKeys object to the media element.
  67. *
  68. * @this {!HTMLMediaElement}
  69. * @param {MediaKeys} mediaKeys
  70. * @return {!Promise}
  71. */
  72. static setMediaKeys(mediaKeys) {
  73. shaka.log.debug('PatchedMediaKeysNop.setMediaKeys');
  74. goog.asserts.assert(this instanceof HTMLMediaElement,
  75. 'bad "this" for setMediaKeys');
  76. if (mediaKeys == null) {
  77. return Promise.resolve();
  78. }
  79. return Promise.reject(new Error('MediaKeys not supported.'));
  80. }
  81. };
  82. /**
  83. * An unusable constructor for MediaKeys.
  84. * @implements {MediaKeys}
  85. */
  86. shaka.polyfill.PatchedMediaKeysNop.MediaKeys = class {
  87. /** */
  88. constructor() {
  89. throw new TypeError('Illegal constructor.');
  90. }
  91. /** @override */
  92. createSession() {}
  93. /** @override */
  94. setServerCertificate() {}
  95. /** @override */
  96. getStatusForPolicy(policy) {
  97. return Promise.resolve('usable');
  98. }
  99. };
  100. /**
  101. * An unusable constructor for MediaKeySystemAccess.
  102. * @implements {MediaKeySystemAccess}
  103. */
  104. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess = class {
  105. /** */
  106. constructor() {
  107. /** @override */
  108. this.keySystem = ''; // For the compiler.
  109. throw new TypeError('Illegal constructor.');
  110. }
  111. /** @override */
  112. getConfiguration() {}
  113. /** @override */
  114. createMediaKeys() {}
  115. };
  116. /**
  117. * API name.
  118. *
  119. * @private {string}
  120. */
  121. shaka.polyfill.PatchedMediaKeysNop.apiName_ = 'nop';
  122. // A low priority ensures this is the last and acts as a fallback.
  123. shaka.polyfill.register(shaka.polyfill.PatchedMediaKeysNop.install, -10);