2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @param {!WebInspector.Workspace} workspace
35 WebInspector.PresentationConsoleMessageHelper = function(workspace)
37 this._workspace = workspace;
39 /** @type {!Object.<string, !Array.<!WebInspector.ConsoleMessage>>} */
40 this._pendingConsoleMessages = {};
42 /** @type {!Array.<!WebInspector.PresentationConsoleMessage>} */
43 this._presentationConsoleMessages = [];
45 /** @type {!Map.<!WebInspector.UISourceCode, !Array.<!WebInspector.PresentationConsoleMessage>>} */
46 this._uiSourceCodeToMessages = new Map();
48 /** @type {!Map.<!WebInspector.UISourceCode, !WebInspector.Object>} */
49 this._uiSourceCodeToEventTarget = new Map();
51 workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeRemoved, this._uiSourceCodeRemoved, this);
52 workspace.addEventListener(WebInspector.Workspace.Events.ProjectRemoved, this._projectRemoved, this);
53 WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.ConsoleCleared, this._consoleCleared, this);
54 WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, this._onConsoleMessageAdded, this);
55 WebInspector.multitargetConsoleModel.messages().forEach(this._consoleMessageAdded, this);
56 WebInspector.targetManager.addModelListener(WebInspector.DebuggerModel, WebInspector.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSource, this);
57 WebInspector.targetManager.addModelListener(WebInspector.DebuggerModel, WebInspector.DebuggerModel.Events.FailedToParseScriptSource, this._parsedScriptSource, this);
58 WebInspector.targetManager.addModelListener(WebInspector.DebuggerModel, WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
64 WebInspector.PresentationConsoleMessageHelper.Events = {
65 ConsoleMessageAdded: "ConsoleMessageAdded",
66 ConsoleMessageRemoved: "ConsoleMessageRemoved",
67 ConsoleMessagesCleared: "ConsoleMessagesCleared",
70 WebInspector.PresentationConsoleMessageHelper.prototype = {
72 * @param {!WebInspector.PresentationConsoleMessageHelper.Events} eventType
73 * @param {!WebInspector.UISourceCode} uiSourceCode
74 * @param {function(!WebInspector.Event)} listener
75 * @param {!Object=} thisObject
77 addConsoleMessageEventListener: function(eventType, uiSourceCode, listener, thisObject)
79 var target = this._uiSourceCodeToEventTarget.get(uiSourceCode);
81 target = new WebInspector.Object();
82 this._uiSourceCodeToEventTarget.set(uiSourceCode, target);
84 target.addEventListener(eventType, listener, thisObject);
88 * @param {!WebInspector.PresentationConsoleMessageHelper.Events} eventType
89 * @param {!WebInspector.UISourceCode} uiSourceCode
90 * @param {function(!WebInspector.Event)} listener
91 * @param {!Object=} thisObject
93 removeConsoleMessageEventListener: function(eventType, uiSourceCode, listener, thisObject)
95 var target = this._uiSourceCodeToEventTarget.get(uiSourceCode);
98 target.removeEventListener(eventType, listener, thisObject);
102 * @param {!WebInspector.UISourceCode} uiSourceCode
103 * @return {!Array.<!WebInspector.PresentationConsoleMessage>}
105 consoleMessages: function(uiSourceCode)
107 return this._uiSourceCodeToMessages.get(uiSourceCode) || [];
111 * @param {!WebInspector.PresentationConsoleMessageHelper.Events} eventType
112 * @param {!WebInspector.UISourceCode} uiSourceCode
113 * @param {!WebInspector.PresentationConsoleMessage=} message
115 _dispatchConsoleEvent: function(eventType, uiSourceCode, message)
117 var target = this._uiSourceCodeToEventTarget.get(uiSourceCode);
120 target.dispatchEventToListeners(eventType, message);
124 * @param {!WebInspector.Event} event
126 _uiSourceCodeRemoved: function(event)
128 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
129 this._uiSourceCodeToEventTarget.remove(uiSourceCode);
130 this._uiSourceCodeToMessages.remove(uiSourceCode);
134 * @param {!WebInspector.Event} event
136 _projectRemoved: function(event)
138 var project = /** @type {!WebInspector.Project} */ (event.data);
139 var uiSourceCodes = project.uiSourceCodes();
140 for (var i = 0; i < uiSourceCodes.length; ++i) {
141 this._uiSourceCodeToEventTarget.remove(uiSourceCodes[i]);
142 this._uiSourceCodeToMessages.remove(uiSourceCodes[i]);
147 * @param {!WebInspector.Event} event
149 _onConsoleMessageAdded: function(event)
151 var message = /** @type {!WebInspector.ConsoleMessage} */ (event.data);
152 this._consoleMessageAdded(message)
156 * @param {!WebInspector.ConsoleMessage} message
158 _consoleMessageAdded: function(message)
160 if (!message.url || !message.isErrorOrWarning())
163 var rawLocation = this._rawLocation(message);
165 this._addConsoleMessageToScript(message, rawLocation);
167 this._addPendingConsoleMessage(message);
171 * @param {!WebInspector.ConsoleMessage} message
172 * @return {?WebInspector.DebuggerModel.Location}
174 _rawLocation: function(message)
176 // FIXME(62725): stack trace line/column numbers are one-based.
177 var lineNumber = message.stackTrace ? message.stackTrace[0].lineNumber - 1 : message.line - 1;
178 var columnNumber = message.stackTrace && message.stackTrace[0].columnNumber ? message.stackTrace[0].columnNumber - 1 : 0;
179 if (message.scriptId)
180 return message.target().debuggerModel.createRawLocationByScriptId(message.scriptId, message.url || "", lineNumber, columnNumber);
181 return message.target().debuggerModel.createRawLocationByURL(message.url || "", lineNumber, columnNumber);
185 * @param {!WebInspector.ConsoleMessage} message
186 * @param {!WebInspector.DebuggerModel.Location} rawLocation
188 _addConsoleMessageToScript: function(message, rawLocation)
190 this._presentationConsoleMessages.push(new WebInspector.PresentationConsoleMessage(message, rawLocation));
194 * @param {!WebInspector.ConsoleMessage} message
196 _addPendingConsoleMessage: function(message)
200 if (!this._pendingConsoleMessages[message.url])
201 this._pendingConsoleMessages[message.url] = [];
202 this._pendingConsoleMessages[message.url].push(message);
206 * @param {!WebInspector.Event} event
208 _parsedScriptSource: function(event)
210 var script = /** @type {!WebInspector.Script} */ (event.data);
212 var messages = this._pendingConsoleMessages[script.sourceURL];
216 var pendingMessages = [];
217 for (var i = 0; i < messages.length; i++) {
218 var message = messages[i];
219 var rawLocation = this._rawLocation(message);
220 if (script.target() === message.target() && script.scriptId === rawLocation.scriptId)
221 this._addConsoleMessageToScript(message, rawLocation);
223 pendingMessages.push(message);
226 if (pendingMessages.length)
227 this._pendingConsoleMessages[script.sourceURL] = pendingMessages;
229 delete this._pendingConsoleMessages[script.sourceURL];
233 * @param {!WebInspector.PresentationConsoleMessage} message
235 _presentationConsoleMessageAdded: function(message)
237 var uiSourceCode = message._uiLocation.uiSourceCode;
238 var messages = this._uiSourceCodeToMessages.get(uiSourceCode);
241 this._uiSourceCodeToMessages.set(uiSourceCode, messages);
243 messages.push(message);
244 this._dispatchConsoleEvent(WebInspector.PresentationConsoleMessageHelper.Events.ConsoleMessageAdded, uiSourceCode, message);
248 * @param {!WebInspector.PresentationConsoleMessage} message
250 _presentationConsoleMessageRemoved: function(message)
252 var uiSourceCode = message._uiLocation.uiSourceCode;
253 var messages = this._uiSourceCodeToMessages.get(uiSourceCode);
256 messages.remove(message);
257 this._dispatchConsoleEvent(WebInspector.PresentationConsoleMessageHelper.Events.ConsoleMessageRemoved, uiSourceCode, message);
260 _consoleCleared: function()
262 this._pendingConsoleMessages = {};
263 for (var i = 0; i < this._presentationConsoleMessages.length; ++i)
264 this._presentationConsoleMessages[i].dispose();
265 this._presentationConsoleMessages = [];
266 var targets = this._uiSourceCodeToEventTarget.valuesArray();
267 for (var i = 0; i < targets.length; ++i)
268 targets[i].dispatchEventToListeners(WebInspector.PresentationConsoleMessageHelper.Events.ConsoleMessagesCleared);
269 this._uiSourceCodeToMessages.clear();
272 _debuggerReset: function()
274 this._consoleCleared();
280 * @param {!WebInspector.ConsoleMessage} message
281 * @param {!WebInspector.DebuggerModel.Location} rawLocation
283 WebInspector.PresentationConsoleMessage = function(message, rawLocation)
285 this.originalMessage = message;
286 this._liveLocation = WebInspector.debuggerWorkspaceBinding.createLiveLocation(rawLocation, this._updateLocation.bind(this));
289 WebInspector.PresentationConsoleMessage.prototype = {
291 * @param {!WebInspector.UILocation} uiLocation
293 _updateLocation: function(uiLocation)
295 if (this._uiLocation)
296 WebInspector.presentationConsoleMessageHelper._presentationConsoleMessageRemoved(this);
297 this._uiLocation = uiLocation;
298 WebInspector.presentationConsoleMessageHelper._presentationConsoleMessageAdded(this);
303 return this._uiLocation.lineNumber;
308 this._liveLocation.dispose();
312 /** @type {!WebInspector.PresentationConsoleMessageHelper} */
313 WebInspector.presentationConsoleMessageHelper;