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 * @implements {WebInspector.TargetManager.Observer}
34 * @param {!WebInspector.Workspace} workspace
36 WebInspector.PresentationConsoleMessageHelper = function(workspace)
39 * @type {!Object.<string, !Array.<!WebInspector.ConsoleMessage>>}
41 this._pendingConsoleMessages = {};
42 this._presentationConsoleMessages = [];
43 this._workspace = workspace;
45 WebInspector.targetManager.observeTargets(this);
48 WebInspector.PresentationConsoleMessageHelper.prototype = {
50 * @param {!WebInspector.Target} target
52 targetAdded: function(target)
54 target.consoleModel.addEventListener(WebInspector.ConsoleModel.Events.MessageAdded, this._consoleMessageAdded, this);
55 target.consoleModel.addEventListener(WebInspector.ConsoleModel.Events.ConsoleCleared, this._consoleCleared, this);
57 target.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSource, this);
58 target.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.FailedToParseScriptSource, this._parsedScriptSource, this);
59 target.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
63 * @param {!WebInspector.Target} target
65 targetRemoved: function(target)
67 target.consoleModel.removeEventListener(WebInspector.ConsoleModel.Events.MessageAdded, this._consoleMessageAdded, this);
68 target.consoleModel.removeEventListener(WebInspector.ConsoleModel.Events.ConsoleCleared, this._consoleCleared, this);
70 target.debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSource, this);
71 target.debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.FailedToParseScriptSource, this._parsedScriptSource, this);
72 target.debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
76 * @param {!WebInspector.Event} event
78 _consoleMessageAdded: function(event)
80 var message = /** @type {!WebInspector.ConsoleMessage} */ (event.data);
81 if (!message.url || !message.isErrorOrWarning())
84 var rawLocation = this._rawLocation(message);
86 this._addConsoleMessageToScript(message, rawLocation);
88 this._addPendingConsoleMessage(message);
92 * @param {!WebInspector.ConsoleMessage} message
93 * @return {?WebInspector.DebuggerModel.Location}
95 _rawLocation: function(message)
97 // FIXME(62725): stack trace line/column numbers are one-based.
98 var lineNumber = message.stackTrace ? message.stackTrace[0].lineNumber - 1 : message.line - 1;
99 var columnNumber = message.stackTrace && message.stackTrace[0].columnNumber ? message.stackTrace[0].columnNumber - 1 : 0;
100 return message.target().debuggerModel.createRawLocationByURL(message.url || "", lineNumber, columnNumber);
104 * @param {!WebInspector.ConsoleMessage} message
105 * @param {!WebInspector.DebuggerModel.Location} rawLocation
107 _addConsoleMessageToScript: function(message, rawLocation)
109 this._presentationConsoleMessages.push(new WebInspector.PresentationConsoleMessage(message, rawLocation));
113 * @param {!WebInspector.ConsoleMessage} message
115 _addPendingConsoleMessage: function(message)
119 if (!this._pendingConsoleMessages[message.url])
120 this._pendingConsoleMessages[message.url] = [];
121 this._pendingConsoleMessages[message.url].push(message);
125 * @param {!WebInspector.Event} event
127 _parsedScriptSource: function(event)
129 var script = /** @type {!WebInspector.Script} */ (event.data);
131 var messages = this._pendingConsoleMessages[script.sourceURL];
135 var pendingMessages = [];
136 for (var i = 0; i < messages.length; i++) {
137 var message = messages[i];
138 var rawLocation = this._rawLocation(message);
139 if (script.scriptId === rawLocation.scriptId)
140 this._addConsoleMessageToScript(message, rawLocation);
142 pendingMessages.push(message);
145 if (pendingMessages.length)
146 this._pendingConsoleMessages[script.sourceURL] = pendingMessages;
148 delete this._pendingConsoleMessages[script.sourceURL];
151 _consoleCleared: function()
153 this._pendingConsoleMessages = {};
154 for (var i = 0; i < this._presentationConsoleMessages.length; ++i)
155 this._presentationConsoleMessages[i].dispose();
156 this._presentationConsoleMessages = [];
157 var uiSourceCodes = this._workspace.uiSourceCodes();
158 for (var i = 0; i < uiSourceCodes.length; ++i)
159 uiSourceCodes[i].consoleMessagesCleared();
162 _debuggerReset: function()
164 this._pendingConsoleMessages = {};
165 this._presentationConsoleMessages = [];
171 * @param {!WebInspector.ConsoleMessage} message
172 * @param {!WebInspector.DebuggerModel.Location} rawLocation
174 WebInspector.PresentationConsoleMessage = function(message, rawLocation)
176 this.originalMessage = message;
177 this._liveLocation = rawLocation.createLiveLocation(this._updateLocation.bind(this));
180 WebInspector.PresentationConsoleMessage.prototype = {
182 * @param {!WebInspector.UILocation} uiLocation
184 _updateLocation: function(uiLocation)
186 if (this._uiLocation)
187 this._uiLocation.uiSourceCode.consoleMessageRemoved(this);
188 this._uiLocation = uiLocation;
189 this._uiLocation.uiSourceCode.consoleMessageAdded(this);
194 return this._uiLocation.lineNumber;
199 this._liveLocation.dispose();