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 http://goo.gl/blgtZZ EME draft 12 March 2015} on browsers without EME.
  26. * All methods will fail.
  27. */
  28. /**
  29. * Installs the polyfill if needed.
  30. */
  31. shaka.polyfill.PatchedMediaKeysNop.install = function() {
  32. if (!window.HTMLVideoElement ||
  33. (navigator.requestMediaKeySystemAccess &&
  34. MediaKeySystemAccess.prototype.getConfiguration)) {
  35. return;
  36. }
  37. shaka.log.info('EME not available.');
  38. // Alias.
  39. const PatchedMediaKeysNop = shaka.polyfill.PatchedMediaKeysNop;
  40. // Install patches.
  41. navigator.requestMediaKeySystemAccess =
  42. PatchedMediaKeysNop.requestMediaKeySystemAccess;
  43. // Delete mediaKeys to work around strict mode compatibility issues.
  44. delete HTMLMediaElement.prototype['mediaKeys'];
  45. // Work around read-only declaration for mediaKeys by using a string.
  46. HTMLMediaElement.prototype['mediaKeys'] = null;
  47. HTMLMediaElement.prototype.setMediaKeys = PatchedMediaKeysNop.setMediaKeys;
  48. // These are not usable, but allow Player.isBrowserSupported to pass.
  49. window.MediaKeys = PatchedMediaKeysNop.MediaKeys;
  50. window.MediaKeySystemAccess = PatchedMediaKeysNop.MediaKeySystemAccess;
  51. };
  52. /**
  53. * An implementation of navigator.requestMediaKeySystemAccess.
  54. * Retrieves a MediaKeySystemAccess object.
  55. *
  56. * @this {!Navigator}
  57. * @param {string} keySystem
  58. * @param {!Array.<!MediaKeySystemConfiguration>} supportedConfigurations
  59. * @return {!Promise.<!MediaKeySystemAccess>}
  60. */
  61. shaka.polyfill.PatchedMediaKeysNop.requestMediaKeySystemAccess =
  62. function(keySystem, supportedConfigurations) {
  63. shaka.log.debug('PatchedMediaKeysNop.requestMediaKeySystemAccess');
  64. goog.asserts.assert(this == navigator,
  65. 'bad "this" for requestMediaKeySystemAccess');
  66. return Promise.reject(new Error(
  67. 'The key system specified is not supported.'));
  68. };
  69. /**
  70. * An implementation of HTMLMediaElement.prototype.setMediaKeys.
  71. * Attaches a MediaKeys object to the media element.
  72. *
  73. * @this {!HTMLMediaElement}
  74. * @param {MediaKeys} mediaKeys
  75. * @return {!Promise}
  76. */
  77. shaka.polyfill.PatchedMediaKeysNop.setMediaKeys = function(mediaKeys) {
  78. shaka.log.debug('PatchedMediaKeysNop.setMediaKeys');
  79. goog.asserts.assert(this instanceof HTMLMediaElement,
  80. 'bad "this" for setMediaKeys');
  81. if (mediaKeys == null) {
  82. return Promise.resolve();
  83. }
  84. return Promise.reject(new Error('MediaKeys not supported.'));
  85. };
  86. /**
  87. * An unusable constructor for MediaKeys.
  88. * @constructor
  89. * @struct
  90. * @implements {MediaKeys}
  91. */
  92. shaka.polyfill.PatchedMediaKeysNop.MediaKeys = function() {
  93. throw new TypeError('Illegal constructor.');
  94. };
  95. /** @override */
  96. shaka.polyfill.PatchedMediaKeysNop.MediaKeys.prototype.createSession =
  97. function() {};
  98. /** @override */
  99. shaka.polyfill.PatchedMediaKeysNop.MediaKeys.prototype.setServerCertificate =
  100. function() {};
  101. /**
  102. * An unusable constructor for MediaKeySystemAccess.
  103. * @constructor
  104. * @struct
  105. * @implements {MediaKeySystemAccess}
  106. */
  107. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess = function() {
  108. throw new TypeError('Illegal constructor.');
  109. };
  110. /** @override */
  111. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess.prototype.
  112. getConfiguration = function() {};
  113. /** @override */
  114. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess.prototype.
  115. createMediaKeys = function() {};
  116. /** @override */
  117. shaka.polyfill.PatchedMediaKeysNop.MediaKeySystemAccess.prototype.
  118. keySystem;
  119. // A low priority ensures this is the last and acts as a fallback.
  120. shaka.polyfill.register(shaka.polyfill.PatchedMediaKeysNop.install, -10);