PresentationConsoleMessageHelper.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @param {WebInspector.UISourceCodeProvider} uiSourceCodeProvider
  33. */
  34. WebInspector.PresentationConsoleMessageHelper = function(uiSourceCodeProvider)
  35. {
  36. /**
  37. * @type {Object.<string, Array.<WebInspector.ConsoleMessage>>}
  38. */
  39. this._pendingConsoleMessages = {};
  40. this._presentationConsoleMessages = [];
  41. this._uiSourceCodeProvider = uiSourceCodeProvider;
  42. WebInspector.console.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, this._consoleMessageAdded, this);
  43. WebInspector.console.addEventListener(WebInspector.ConsoleModel.Events.RepeatCountUpdated, this._consoleMessageAdded, this);
  44. WebInspector.console.addEventListener(WebInspector.ConsoleModel.Events.ConsoleCleared, this._consoleCleared, this);
  45. WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSource, this);
  46. WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.FailedToParseScriptSource, this._parsedScriptSource, this);
  47. WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
  48. }
  49. WebInspector.PresentationConsoleMessageHelper.prototype = {
  50. /**
  51. * @param {WebInspector.Event} event
  52. */
  53. _consoleMessageAdded: function(event)
  54. {
  55. var message = /** @type {WebInspector.ConsoleMessage} */ (event.data);
  56. if (!message.url || !message.isErrorOrWarning())
  57. return;
  58. var rawLocation = message.location();
  59. if (rawLocation)
  60. this._addConsoleMessageToScript(message, rawLocation);
  61. else
  62. this._addPendingConsoleMessage(message);
  63. },
  64. /**
  65. * @param {WebInspector.ConsoleMessage} message
  66. * @param {WebInspector.DebuggerModel.Location} rawLocation
  67. */
  68. _addConsoleMessageToScript: function(message, rawLocation)
  69. {
  70. this._presentationConsoleMessages.push(new WebInspector.PresentationConsoleMessage(message, rawLocation));
  71. },
  72. /**
  73. * @param {WebInspector.ConsoleMessage} message
  74. */
  75. _addPendingConsoleMessage: function(message)
  76. {
  77. if (!message.url)
  78. return;
  79. if (!this._pendingConsoleMessages[message.url])
  80. this._pendingConsoleMessages[message.url] = [];
  81. this._pendingConsoleMessages[message.url].push(message);
  82. },
  83. /**
  84. * @param {WebInspector.Event} event
  85. */
  86. _parsedScriptSource: function(event)
  87. {
  88. var script = /** @type {WebInspector.Script} */ (event.data);
  89. var messages = this._pendingConsoleMessages[script.sourceURL];
  90. if (!messages)
  91. return;
  92. var pendingMessages = [];
  93. for (var i = 0; i < messages.length; i++) {
  94. var message = messages[i];
  95. var rawLocation = /** @type {WebInspector.DebuggerModel.Location} */ (message.location());
  96. if (script.scriptId === rawLocation.scriptId)
  97. this._addConsoleMessageToScript(message, rawLocation);
  98. else
  99. pendingMessages.push(message);
  100. }
  101. if (pendingMessages.length)
  102. this._pendingConsoleMessages[script.sourceURL] = pendingMessages;
  103. else
  104. delete this._pendingConsoleMessages[script.sourceURL];
  105. },
  106. _consoleCleared: function()
  107. {
  108. this._pendingConsoleMessages = {};
  109. for (var i = 0; i < this._presentationConsoleMessages.length; ++i)
  110. this._presentationConsoleMessages[i].dispose();
  111. this._presentationConsoleMessages = [];
  112. var uiSourceCodes = this._uiSourceCodeProvider.uiSourceCodes();
  113. for (var i = 0; i < uiSourceCodes.length; ++i)
  114. uiSourceCodes[i].consoleMessagesCleared();
  115. },
  116. _debuggerReset: function()
  117. {
  118. this._pendingConsoleMessages = {};
  119. this._presentationConsoleMessages = [];
  120. }
  121. }
  122. /**
  123. * @constructor
  124. * @param {WebInspector.ConsoleMessage} message
  125. * @param {WebInspector.DebuggerModel.Location} rawLocation
  126. */
  127. WebInspector.PresentationConsoleMessage = function(message, rawLocation)
  128. {
  129. this.originalMessage = message;
  130. this._liveLocation = WebInspector.debuggerModel.createLiveLocation(rawLocation, this._updateLocation.bind(this));
  131. }
  132. WebInspector.PresentationConsoleMessage.prototype = {
  133. /**
  134. * @param {WebInspector.UILocation} uiLocation
  135. */
  136. _updateLocation: function(uiLocation)
  137. {
  138. if (this._uiLocation)
  139. this._uiLocation.uiSourceCode.consoleMessageRemoved(this);
  140. this._uiLocation = uiLocation;
  141. this._uiLocation.uiSourceCode.consoleMessageAdded(this);
  142. },
  143. get lineNumber()
  144. {
  145. return this._uiLocation.lineNumber;
  146. },
  147. dispose: function()
  148. {
  149. this._liveLocation.dispose();
  150. }
  151. }