ConsoleModel.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) 2011 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. * @extends {WebInspector.Object}
  33. */
  34. WebInspector.ConsoleModel = function()
  35. {
  36. this.messages = [];
  37. this.warnings = 0;
  38. this.errors = 0;
  39. this._interruptRepeatCount = false;
  40. InspectorBackend.registerConsoleDispatcher(new WebInspector.ConsoleDispatcher(this));
  41. }
  42. WebInspector.ConsoleModel.Events = {
  43. ConsoleCleared: "console-cleared",
  44. MessageAdded: "console-message-added",
  45. RepeatCountUpdated: "repeat-count-updated"
  46. }
  47. WebInspector.ConsoleModel.prototype = {
  48. enableAgent: function()
  49. {
  50. if (WebInspector.settings.monitoringXHREnabled.get())
  51. ConsoleAgent.setMonitoringXHREnabled(true);
  52. this._enablingConsole = true;
  53. function callback()
  54. {
  55. delete this._enablingConsole;
  56. }
  57. ConsoleAgent.enable(callback.bind(this));
  58. },
  59. /**
  60. * @return {boolean}
  61. */
  62. enablingConsole: function()
  63. {
  64. return !!this._enablingConsole;
  65. },
  66. /**
  67. * @param {WebInspector.ConsoleMessage} msg
  68. * @param {boolean=} isFromBackend
  69. */
  70. addMessage: function(msg, isFromBackend)
  71. {
  72. if (isFromBackend && WebInspector.SourceMap.hasSourceMapRequestHeader(msg.request()))
  73. return;
  74. msg.index = this.messages.length;
  75. this.messages.push(msg);
  76. this._incrementErrorWarningCount(msg);
  77. if (isFromBackend)
  78. this._previousMessage = msg;
  79. this._interruptRepeatCount = !isFromBackend;
  80. this.dispatchEventToListeners(WebInspector.ConsoleModel.Events.MessageAdded, msg);
  81. },
  82. /**
  83. * @param {WebInspector.ConsoleMessage} msg
  84. */
  85. _incrementErrorWarningCount: function(msg)
  86. {
  87. switch (msg.level) {
  88. case WebInspector.ConsoleMessage.MessageLevel.Warning:
  89. this.warnings += msg.repeatDelta;
  90. break;
  91. case WebInspector.ConsoleMessage.MessageLevel.Error:
  92. this.errors += msg.repeatDelta;
  93. break;
  94. }
  95. },
  96. requestClearMessages: function()
  97. {
  98. ConsoleAgent.clearMessages();
  99. this.clearMessages();
  100. },
  101. clearMessages: function()
  102. {
  103. this.dispatchEventToListeners(WebInspector.ConsoleModel.Events.ConsoleCleared);
  104. this.messages = [];
  105. delete this._previousMessage;
  106. this.errors = 0;
  107. this.warnings = 0;
  108. },
  109. /**
  110. * @param {number} count
  111. */
  112. _messageRepeatCountUpdated: function(count)
  113. {
  114. var msg = this._previousMessage;
  115. if (!msg)
  116. return;
  117. var prevRepeatCount = msg.totalRepeatCount;
  118. if (!this._interruptRepeatCount) {
  119. msg.repeatDelta = count - prevRepeatCount;
  120. msg.repeatCount = msg.repeatCount + msg.repeatDelta;
  121. msg.totalRepeatCount = count;
  122. msg.updateRepeatCount();
  123. this._incrementErrorWarningCount(msg);
  124. this.dispatchEventToListeners(WebInspector.ConsoleModel.Events.RepeatCountUpdated, msg);
  125. } else {
  126. var msgCopy = msg.clone();
  127. msgCopy.totalRepeatCount = count;
  128. msgCopy.repeatCount = (count - prevRepeatCount) || 1;
  129. msgCopy.repeatDelta = msgCopy.repeatCount;
  130. this.addMessage(msgCopy, true);
  131. }
  132. },
  133. __proto__: WebInspector.Object.prototype
  134. }
  135. /**
  136. * @constructor
  137. * @param {string} source
  138. * @param {string} level
  139. * @param {string=} url
  140. * @param {number=} line
  141. * @param {number=} column
  142. * @param {number=} repeatCount
  143. */
  144. WebInspector.ConsoleMessage = function(source, level, url, line, column, repeatCount)
  145. {
  146. this.source = source;
  147. this.level = level;
  148. this.url = url || null;
  149. this.line = line || 0;
  150. this.column = column || 0;
  151. this.message = "";
  152. repeatCount = repeatCount || 1;
  153. this.repeatCount = repeatCount;
  154. this.repeatDelta = repeatCount;
  155. this.totalRepeatCount = repeatCount;
  156. }
  157. WebInspector.ConsoleMessage.prototype = {
  158. /**
  159. * @return {boolean}
  160. */
  161. isErrorOrWarning: function()
  162. {
  163. return (this.level === WebInspector.ConsoleMessage.MessageLevel.Warning || this.level === WebInspector.ConsoleMessage.MessageLevel.Error);
  164. },
  165. updateRepeatCount: function()
  166. {
  167. // Implemented by concrete instances
  168. },
  169. /**
  170. * @return {WebInspector.ConsoleMessage}
  171. */
  172. clone: function()
  173. {
  174. // Implemented by concrete instances
  175. },
  176. /**
  177. * @return {WebInspector.DebuggerModel.Location}
  178. */
  179. location: function()
  180. {
  181. // Implemented by concrete instances
  182. }
  183. }
  184. /**
  185. * @param {string} source
  186. * @param {string} level
  187. * @param {string} message
  188. * @param {string=} type
  189. * @param {string=} url
  190. * @param {number=} line
  191. * @param {number=} column
  192. * @param {number=} repeatCount
  193. * @param {Array.<RuntimeAgent.RemoteObject>=} parameters
  194. * @param {ConsoleAgent.StackTrace=} stackTrace
  195. * @param {NetworkAgent.RequestId=} requestId
  196. * @param {boolean=} isOutdated
  197. * @return {WebInspector.ConsoleMessage}
  198. */
  199. WebInspector.ConsoleMessage.create = function(source, level, message, type, url, line, column, repeatCount, parameters, stackTrace, requestId, isOutdated)
  200. {
  201. }
  202. // Note: Keep these constants in sync with the ones in Console.h
  203. WebInspector.ConsoleMessage.MessageSource = {
  204. XML: "xml",
  205. JS: "javascript",
  206. Network: "network",
  207. ConsoleAPI: "console-api",
  208. Storage: "storage",
  209. AppCache: "appcache",
  210. Rendering: "rendering",
  211. CSS: "css",
  212. Security: "security",
  213. Other: "other",
  214. Deprecation: "deprecation"
  215. }
  216. WebInspector.ConsoleMessage.MessageType = {
  217. Log: "log",
  218. Dir: "dir",
  219. DirXML: "dirxml",
  220. Table: "table",
  221. Trace: "trace",
  222. Clear: "clear",
  223. StartGroup: "startGroup",
  224. StartGroupCollapsed: "startGroupCollapsed",
  225. EndGroup: "endGroup",
  226. Assert: "assert",
  227. Result: "result",
  228. Profile: "profile",
  229. ProfileEnd: "profileEnd"
  230. }
  231. WebInspector.ConsoleMessage.MessageLevel = {
  232. Log: "log",
  233. Warning: "warning",
  234. Error: "error",
  235. Debug: "debug"
  236. }
  237. /**
  238. * @constructor
  239. * @implements {ConsoleAgent.Dispatcher}
  240. * @param {WebInspector.ConsoleModel} console
  241. */
  242. WebInspector.ConsoleDispatcher = function(console)
  243. {
  244. this._console = console;
  245. }
  246. WebInspector.ConsoleDispatcher.prototype = {
  247. /**
  248. * @param {ConsoleAgent.ConsoleMessage} payload
  249. */
  250. messageAdded: function(payload)
  251. {
  252. var consoleMessage = WebInspector.ConsoleMessage.create(
  253. payload.source,
  254. payload.level,
  255. payload.text,
  256. payload.type,
  257. payload.url,
  258. payload.line,
  259. payload.column,
  260. payload.repeatCount,
  261. payload.parameters,
  262. payload.stackTrace,
  263. payload.networkRequestId,
  264. this._console._enablingConsole);
  265. this._console.addMessage(consoleMessage, true);
  266. },
  267. /**
  268. * @param {number} count
  269. */
  270. messageRepeatCountUpdated: function(count)
  271. {
  272. this._console._messageRepeatCountUpdated(count);
  273. },
  274. messagesCleared: function()
  275. {
  276. if (!WebInspector.settings.preserveConsoleLog.get())
  277. this._console.clearMessages();
  278. }
  279. }
  280. /**
  281. * @type {?WebInspector.ConsoleModel}
  282. */
  283. WebInspector.console = null;