Source: lib/deprecate/deprecate.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.Deprecate');
  18. goog.require('goog.asserts');
  19. goog.require('shaka.deprecate.Enforcer');
  20. goog.require('shaka.deprecate.Version');
  21. /**
  22. * |shaka.Deprecate| is the front-end of the deprecation system, allowing for
  23. * any part of the code to say that "this block of code should be removed by
  24. * version X".
  25. *
  26. * @final
  27. */
  28. shaka.Deprecate = class {
  29. /**
  30. * Initialize the system. This must happen before any calls to |enforce|. In
  31. * our code base, |shaka.Player| will be the only one to call this (it has the
  32. * version string).
  33. *
  34. * If the |Deprecate| called |Player.version| to initialize itself, it would
  35. * mean that |Player| could not use |Deprecate| because it would create a
  36. * circular dependency. To work around this, we provide this method so that
  37. * |Player| can give us the version without us needing to know about |Player|.
  38. *
  39. * This will initialize the system to:
  40. * - print warning messages when the feature is scheduled to be removed in a
  41. * later version
  42. * - print errors and fail assertions when the feature should be removed now
  43. *
  44. * @param {string} versionString
  45. */
  46. static init(versionString) {
  47. goog.asserts.assert(
  48. shaka.Deprecate.enforcer_ == null,
  49. 'Deprecate.init should only be called once.');
  50. shaka.Deprecate.enforcer_ = new shaka.deprecate.Enforcer(
  51. shaka.deprecate.Version.parse(versionString),
  52. shaka.Deprecate.onPending_,
  53. shaka.Deprecate.onExpired_);
  54. }
  55. /**
  56. * Ask the deprecation system to require this feature to be removed by the
  57. * given version.
  58. *
  59. * @param {number} major
  60. * @param {number} minor
  61. * @param {string} name
  62. * @param {string} description
  63. */
  64. static deprecateFeature(major, minor, name, description) {
  65. const enforcer = shaka.Deprecate.enforcer_;
  66. goog.asserts.assert(
  67. enforcer,
  68. 'Missing deprecation enforcer. Was |init| called?');
  69. const expiresAt = new shaka.deprecate.Version(major, minor);
  70. enforcer.enforce(expiresAt, name, description);
  71. }
  72. /**
  73. * @param {!shaka.deprecate.Version} libraryVersion
  74. * @param {!shaka.deprecate.Version} featureVersion
  75. * @param {string} name
  76. * @param {string} description
  77. * @private
  78. */
  79. static onPending_(libraryVersion, featureVersion, name, description) {
  80. // If we were to pass each value to the log call, it would be printed as
  81. // a comma-separated list. To make the print state appear more natural to
  82. // the reader, create one string for the message.
  83. shaka.log.alwaysWarn([
  84. name,
  85. 'has been deprecated and will be removed in',
  86. featureVersion,
  87. '. We are currently at version',
  88. libraryVersion,
  89. '. Additional information:',
  90. description,
  91. ].join(' '));
  92. }
  93. /**
  94. * @param {!shaka.deprecate.Version} libraryVersion
  95. * @param {!shaka.deprecate.Version} featureVersion
  96. * @param {string} name
  97. * @param {string} description
  98. * @private
  99. */
  100. static onExpired_(libraryVersion, featureVersion, name, description) {
  101. // If we were to pass each value to the log call, it would be printed as
  102. // a comma-separated list. To make the print state appear more natural to
  103. // the reader, create one string for the message.
  104. const errorMessage = [
  105. name,
  106. 'has been deprecated and has been removed in',
  107. featureVersion,
  108. '. We are now at version',
  109. libraryVersion,
  110. '. Additional information:',
  111. description,
  112. ].join('');
  113. shaka.log.alwaysError(errorMessage);
  114. goog.asserts.assert(false, errorMessage);
  115. }
  116. };
  117. /**
  118. * The global deprecation enforcer that will be set by the player (because the
  119. * player knows the version) when it calls |init|. This may appear a little
  120. * round-about to you, because it is. Since player uses |Deprecate|, it means
  121. * that |Deprecate| can't depend on Player directly.
  122. *
  123. * @private {shaka.deprecate.Enforcer}
  124. */
  125. shaka.Deprecate.enforcer_ = null;