Source: lib/polyfill/patchedmediakeys_nop.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.polyfill.PatchedMediaKeysNop');
  18. goog.require('goog.asserts');
  19. goog.require('shaka.log');
  20. goog.require('shaka.polyfill.register');
  21. /**
  22. * @namespace shaka.polyfill.PatchedMediaKeysNop
  23. *
  24. * @summary A polyfill to stub out
  25. * {@link https://bit.ly/EmeMar15 EME draft 12 March 2015} on browsers without
  26. * EME.
  27. * All methods will fail.
  28. */
  29. /**
  30. * Installs the polyfill if needed.
  31. */
  32. shaka.polyfill.PatchedMediaKeysNop.install = function() {
  33. if (!window.HTMLVideoElement ||
  34. (navigator.requestMediaKeySystemAccess &&
  35. MediaKeySystemAccess.prototype.getConfiguration)) {
  36. return;
  37. }
  38. shaka.log.info('EME not available.');
  39. // Alias.
  40. const PatchedMediaKeysNop = shaka.polyfill.PatchedMediaKeysNop;
  41. // Install patches.
  42. navigator.requestMediaKeySystemAccess =
  43. PatchedMediaKeysNop.requestMediaKeySystemAccess;
  44. // Delete mediaKeys to work around strict mode compatibility issues.
  45. delete HTMLMediaElement.prototype['mediaKeys'];
  46. // Work around read-only declaration for mediaKeys by using a string.
  47. HTMLMediaElement.prototype['mediaKeys'] = null;
  48. HTMLMediaElement.prototype.setMediaKeys = PatchedMediaKeysNop.setMediaKeys;
  49. // These are not usable, but allow Player.isBrowserSupported to pass.
  50. window.MediaKeys = PatchedMediaKeysNop.MediaKeys;
  51. window.MediaKeySystemAccess = PatchedMediaKeysNop.MediaKeySystemAccess;
  52. };
  53. /**
  54. * An implementation of navigator.requestMediaKeySystemAccess.
  55. * Retrieves a MediaKeySystemAccess object.
  56. *
  57. * @this {!Navigator}
  58. * @param {string} keySystem
  59. * @param {!Array.<!MediaKeySystemConfiguration>} supportedConfigurations
  60. * @return {!Promise.<!MediaKeySystemAccess>}
  61. */
  62. shaka.polyfill.PatchedMediaKeysNop.requestMediaKeySystemAccess =
  63. function(keySystem, supportedConfigurations) {
  64. shaka.log.debug('PatchedMediaKeysNop.requestMediaKeySystemAccess');
  65. goog.asserts.assert(this == navigator,
  66. 'bad "this" for requestMediaKeySystemAccess');
  67. return Promise.reject(new Error(
  68. 'The key system specified is not supported.'));
  69. };
  70. /**
  71. * An implementation of HTMLMediaElement.prototype.setMediaKeys.
  72. * Attaches a MediaKeys object to the media element.
  73. *
  74. * @this {!HTMLMediaElement}
  75. * @param {MediaKeys} mediaKeys
  76. * @return {!Promise}
  77. */
  78. shaka.polyfill.PatchedMediaKeysNop.setMediaKeys = function(mediaKeys) {
  79. shaka.log.debug('PatchedMediaKeysNop.setMediaKeys');
  80. goog.asserts.assert(this instanceof HTMLMediaElement,
  81. 'bad "this" for setMediaKeys');
  82. if (mediaKeys == null) {
  83. return Promise.resolve();
  84. }
  85. return Promise.reject(new Error('MediaKeys not supported.'));
  86. };
  87. /**
  88. * An unusable constructor for MediaKeys.
  89. * @constructor
  90. * @struct
  91. * @implements {MediaKeys}
  92. */
  93. shaka.polyfill.PatchedMediaKeysNop.MediaKeys = function() {
  94. throw new TypeError('Illegal constructor.');
  95. };
  96. /** @override */
  97. shaka.polyfill.PatchedMediaKeysNop.MediaKeys.prototype.createSession =
  98. function() {};
  99. /** @override */
  100. shaka.polyfill.PatchedMediaKeysNop.MediaKeys.prototype.setServerCertificate =
  101. function() {};
  102. /**
  103. * An unusable constructor for MediaKeySystemAccess.
  104. * @constructor
  105. * @struct
  106. * @implements {MediaKeySystemAccess}
  107. */
  108. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess = function() {
  109. throw new TypeError('Illegal constructor.');
  110. };
  111. /** @override */
  112. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess.prototype.
  113. getConfiguration = function() {};
  114. /** @override */
  115. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess.prototype.
  116. createMediaKeys = function() {};
  117. /** @override */
  118. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess.prototype.
  119. keySystem;
  120. // A low priority ensures this is the last and acts as a fallback.
  121. shaka.polyfill.register(shaka.polyfill.PatchedMediaKeysNop.install, -10);