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