entrypointregistry.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2010 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A global registry for entry points into a program,
  16. * so that they can be instrumented. Each module should register their
  17. * entry points with this registry. Designed to be compiled out
  18. * if no instrumentation is requested.
  19. *
  20. * Entry points may be registered before or after a call to
  21. * goog.debug.entryPointRegistry.monitorAll. If an entry point is registered
  22. * later, the existing monitor will instrument the new entry point.
  23. *
  24. * @author nicksantos@google.com (Nick Santos)
  25. */
  26. goog.provide('goog.debug.EntryPointMonitor');
  27. goog.provide('goog.debug.entryPointRegistry');
  28. goog.require('goog.asserts');
  29. /**
  30. * @interface
  31. */
  32. goog.debug.EntryPointMonitor = function() {};
  33. /**
  34. * Instruments a function.
  35. *
  36. * @param {!Function} fn A function to instrument.
  37. * @return {!Function} The instrumented function.
  38. */
  39. goog.debug.EntryPointMonitor.prototype.wrap;
  40. /**
  41. * Try to remove an instrumentation wrapper created by this monitor.
  42. * If the function passed to unwrap is not a wrapper created by this
  43. * monitor, then we will do nothing.
  44. *
  45. * Notice that some wrappers may not be unwrappable. For example, if other
  46. * monitors have applied their own wrappers, then it will be impossible to
  47. * unwrap them because their wrappers will have captured our wrapper.
  48. *
  49. * So it is important that entry points are unwrapped in the reverse
  50. * order that they were wrapped.
  51. *
  52. * @param {!Function} fn A function to unwrap.
  53. * @return {!Function} The unwrapped function, or {@code fn} if it was not
  54. * a wrapped function created by this monitor.
  55. */
  56. goog.debug.EntryPointMonitor.prototype.unwrap;
  57. /**
  58. * An array of entry point callbacks.
  59. * @type {!Array<function(!Function)>}
  60. * @private
  61. */
  62. goog.debug.entryPointRegistry.refList_ = [];
  63. /**
  64. * Monitors that should wrap all the entry points.
  65. * @type {!Array<!goog.debug.EntryPointMonitor>}
  66. * @private
  67. */
  68. goog.debug.entryPointRegistry.monitors_ = [];
  69. /**
  70. * Whether goog.debug.entryPointRegistry.monitorAll has ever been called.
  71. * Checking this allows the compiler to optimize out the registrations.
  72. * @type {boolean}
  73. * @private
  74. */
  75. goog.debug.entryPointRegistry.monitorsMayExist_ = false;
  76. /**
  77. * Register an entry point with this module.
  78. *
  79. * The entry point will be instrumented when a monitor is passed to
  80. * goog.debug.entryPointRegistry.monitorAll. If this has already occurred, the
  81. * entry point is instrumented immediately.
  82. *
  83. * @param {function(!Function)} callback A callback function which is called
  84. * with a transforming function to instrument the entry point. The callback
  85. * is responsible for wrapping the relevant entry point with the
  86. * transforming function.
  87. */
  88. goog.debug.entryPointRegistry.register = function(callback) {
  89. // Don't use push(), so that this can be compiled out.
  90. goog.debug.entryPointRegistry
  91. .refList_[goog.debug.entryPointRegistry.refList_.length] = callback;
  92. // If no one calls monitorAll, this can be compiled out.
  93. if (goog.debug.entryPointRegistry.monitorsMayExist_) {
  94. var monitors = goog.debug.entryPointRegistry.monitors_;
  95. for (var i = 0; i < monitors.length; i++) {
  96. callback(goog.bind(monitors[i].wrap, monitors[i]));
  97. }
  98. }
  99. };
  100. /**
  101. * Configures a monitor to wrap all entry points.
  102. *
  103. * Entry points that have already been registered are immediately wrapped by
  104. * the monitor. When an entry point is registered in the future, it will also
  105. * be wrapped by the monitor when it is registered.
  106. *
  107. * @param {!goog.debug.EntryPointMonitor} monitor An entry point monitor.
  108. */
  109. goog.debug.entryPointRegistry.monitorAll = function(monitor) {
  110. goog.debug.entryPointRegistry.monitorsMayExist_ = true;
  111. var transformer = goog.bind(monitor.wrap, monitor);
  112. for (var i = 0; i < goog.debug.entryPointRegistry.refList_.length; i++) {
  113. goog.debug.entryPointRegistry.refList_[i](transformer);
  114. }
  115. goog.debug.entryPointRegistry.monitors_.push(monitor);
  116. };
  117. /**
  118. * Try to unmonitor all the entry points that have already been registered. If
  119. * an entry point is registered in the future, it will not be wrapped by the
  120. * monitor when it is registered. Note that this may fail if the entry points
  121. * have additional wrapping.
  122. *
  123. * @param {!goog.debug.EntryPointMonitor} monitor The last monitor to wrap
  124. * the entry points.
  125. * @throws {Error} If the monitor is not the most recently configured monitor.
  126. */
  127. goog.debug.entryPointRegistry.unmonitorAllIfPossible = function(monitor) {
  128. var monitors = goog.debug.entryPointRegistry.monitors_;
  129. goog.asserts.assert(
  130. monitor == monitors[monitors.length - 1],
  131. 'Only the most recent monitor can be unwrapped.');
  132. var transformer = goog.bind(monitor.unwrap, monitor);
  133. for (var i = 0; i < goog.debug.entryPointRegistry.refList_.length; i++) {
  134. goog.debug.entryPointRegistry.refList_[i](transformer);
  135. }
  136. monitors.length--;
  137. };