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)
38 * @type {!Object.<string, !Array.<!WebInspector.ConsoleMessage>>}
40 this._pendingConsoleMessages = {};
41 this._presentationConsoleMessages = [];
42 this._workspace = workspace;
44 WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.ConsoleCleared, this._consoleCleared, this);
45 WebInspector.multitargetConsoleModel.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, this._onConsoleMessageAdded, this);
46 WebInspector.multitargetConsoleModel.messages().forEach(this._consoleMessageAdded, this);
47 WebInspector.targetManager.addModelListener(WebInspector.DebuggerModel, WebInspector.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSource, this);
48 WebInspector.targetManager.addModelListener(WebInspector.DebuggerModel, WebInspector.DebuggerModel.Events.FailedToParseScriptSource, this._parsedScriptSource, this);
49 WebInspector.targetManager.addModelListener(WebInspector.DebuggerModel, WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
52 WebInspector.PresentationConsoleMessageHelper.prototype = {
54 * @param {!WebInspector.Event} event
56 _onConsoleMessageAdded: function(event)
58 var message = /** @type {!WebInspector.ConsoleMessage} */ (event.data);
59 this._consoleMessageAdded(message)
63 * @param {!WebInspector.ConsoleMessage} message
65 _consoleMessageAdded: function(message)
67 if (!message.url || !message.isErrorOrWarning())
70 var rawLocation = this._rawLocation(message);
72 this._addConsoleMessageToScript(message, rawLocation);
74 this._addPendingConsoleMessage(message);
78 * @param {!WebInspector.ConsoleMessage} message
79 * @return {?WebInspector.DebuggerModel.Location}
81 _rawLocation: function(message)
83 // FIXME(62725): stack trace line/column numbers are one-based.
84 var lineNumber = message.stackTrace ? message.stackTrace[0].lineNumber - 1 : message.line - 1;
85 var columnNumber = message.stackTrace && message.stackTrace[0].columnNumber ? message.stackTrace[0].columnNumber - 1 : 0;
86 return message.target().debuggerModel.createRawLocationByURL(message.url || "", lineNumber, columnNumber);
90 * @param {!WebInspector.ConsoleMessage} message
91 * @param {!WebInspector.DebuggerModel.Location} rawLocation
93 _addConsoleMessageToScript: function(message, rawLocation)
95 this._presentationConsoleMessages.push(new WebInspector.PresentationConsoleMessage(message, rawLocation));
99 * @param {!WebInspector.ConsoleMessage} message
101 _addPendingConsoleMessage: function(message)
105 if (!this._pendingConsoleMessages[message.url])
106 this._pendingConsoleMessages[message.url] = [];
107 this._pendingConsoleMessages[message.url].push(message);
111 * @param {!WebInspector.Event} event
113 _parsedScriptSource: function(event)
115 var script = /** @type {!WebInspector.Script} */ (event.data);
117 var messages = this._pendingConsoleMessages[script.sourceURL];
121 var pendingMessages = [];
122 for (var i = 0; i < messages.length; i++) {
123 var message = messages[i];
124 var rawLocation = this._rawLocation(message);
125 if (script.target() === message.target() && script.scriptId === rawLocation.scriptId)
126 this._addConsoleMessageToScript(message, rawLocation);
128 pendingMessages.push(message);
131 if (pendingMessages.length)
132 this._pendingConsoleMessages[script.sourceURL] = pendingMessages;
134 delete this._pendingConsoleMessages[script.sourceURL];
137 _consoleCleared: function()
139 this._pendingConsoleMessages = {};
140 for (var i = 0; i < this._presentationConsoleMessages.length; ++i)
141 this._presentationConsoleMessages[i].dispose();
142 this._presentationConsoleMessages = [];
143 var uiSourceCodes = this._workspace.uiSourceCodes();
144 for (var i = 0; i < uiSourceCodes.length; ++i)
145 uiSourceCodes[i].consoleMessagesCleared();
148 _debuggerReset: function()
150 this._pendingConsoleMessages = {};
151 this._presentationConsoleMessages = [];
157 * @implements {WebInspector.PresentationMessage}
158 * @param {!WebInspector.ConsoleMessage} message
159 * @param {!WebInspector.DebuggerModel.Location} rawLocation
161 WebInspector.PresentationConsoleMessage = function(message, rawLocation)
163 this.originalMessage = message;
164 this._liveLocation = WebInspector.debuggerWorkspaceBinding.createLiveLocation(rawLocation, this._updateLocation.bind(this));
167 WebInspector.PresentationConsoleMessage.prototype = {
169 * @param {!WebInspector.UILocation} uiLocation
171 _updateLocation: function(uiLocation)
173 if (this._uiLocation)
174 this._uiLocation.uiSourceCode.consoleMessageRemoved(this);
175 this._uiLocation = uiLocation;
176 this._uiLocation.uiSourceCode.consoleMessageAdded(this);
181 return this._uiLocation.lineNumber;
186 this._liveLocation.dispose();