Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / bindings / PresentationConsoleMessageHelper.js
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 /**
32  * @constructor
33  * @param {!WebInspector.Workspace} workspace
34  */
35 WebInspector.PresentationConsoleMessageHelper = function(workspace)
36 {
37     /**
38      * @type {!Object.<string, !Array.<!WebInspector.ConsoleMessage>>}
39      */
40     this._pendingConsoleMessages = {};
41     this._presentationConsoleMessages = [];
42     this._workspace = workspace;
43
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);
50 }
51
52 WebInspector.PresentationConsoleMessageHelper.prototype = {
53     /**
54      * @param {!WebInspector.Event} event
55      */
56     _onConsoleMessageAdded: function(event)
57     {
58         var message = /** @type {!WebInspector.ConsoleMessage} */ (event.data);
59         this._consoleMessageAdded(message)
60     },
61
62     /**
63      * @param {!WebInspector.ConsoleMessage} message
64      */
65     _consoleMessageAdded: function(message)
66     {
67         if (!message.url || !message.isErrorOrWarning())
68             return;
69
70         var rawLocation = this._rawLocation(message);
71         if (rawLocation)
72             this._addConsoleMessageToScript(message, rawLocation);
73         else
74             this._addPendingConsoleMessage(message);
75     },
76
77     /**
78      * @param {!WebInspector.ConsoleMessage} message
79      * @return {?WebInspector.DebuggerModel.Location}
80      */
81     _rawLocation: function(message)
82     {
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);
87     },
88
89     /**
90      * @param {!WebInspector.ConsoleMessage} message
91      * @param {!WebInspector.DebuggerModel.Location} rawLocation
92      */
93     _addConsoleMessageToScript: function(message, rawLocation)
94     {
95         this._presentationConsoleMessages.push(new WebInspector.PresentationConsoleMessage(message, rawLocation));
96     },
97
98     /**
99      * @param {!WebInspector.ConsoleMessage} message
100      */
101     _addPendingConsoleMessage: function(message)
102     {
103         if (!message.url)
104             return;
105         if (!this._pendingConsoleMessages[message.url])
106             this._pendingConsoleMessages[message.url] = [];
107         this._pendingConsoleMessages[message.url].push(message);
108     },
109
110     /**
111      * @param {!WebInspector.Event} event
112      */
113     _parsedScriptSource: function(event)
114     {
115         var script = /** @type {!WebInspector.Script} */ (event.data);
116
117         var messages = this._pendingConsoleMessages[script.sourceURL];
118         if (!messages)
119             return;
120
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);
127             else
128                 pendingMessages.push(message);
129         }
130
131         if (pendingMessages.length)
132             this._pendingConsoleMessages[script.sourceURL] = pendingMessages;
133         else
134             delete this._pendingConsoleMessages[script.sourceURL];
135     },
136
137     _consoleCleared: function()
138     {
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();
146     },
147
148     _debuggerReset: function()
149     {
150         this._pendingConsoleMessages = {};
151         this._presentationConsoleMessages = [];
152     }
153 }
154
155 /**
156  * @constructor
157  * @implements {WebInspector.PresentationMessage}
158  * @param {!WebInspector.ConsoleMessage} message
159  * @param {!WebInspector.DebuggerModel.Location} rawLocation
160  */
161 WebInspector.PresentationConsoleMessage = function(message, rawLocation)
162 {
163     this.originalMessage = message;
164     this._liveLocation = WebInspector.debuggerWorkspaceBinding.createLiveLocation(rawLocation, this._updateLocation.bind(this));
165 }
166
167 WebInspector.PresentationConsoleMessage.prototype = {
168     /**
169      * @param {!WebInspector.UILocation} uiLocation
170      */
171     _updateLocation: function(uiLocation)
172     {
173         if (this._uiLocation)
174             this._uiLocation.uiSourceCode.consoleMessageRemoved(this);
175         this._uiLocation = uiLocation;
176         this._uiLocation.uiSourceCode.consoleMessageAdded(this);
177     },
178
179     get lineNumber()
180     {
181         return this._uiLocation.lineNumber;
182     },
183
184     dispose: function()
185     {
186         this._liveLocation.dispose();
187     }
188 }