tizen beta release
[profile/ivi/webkit-efl.git] / debian / libwebkit-engine / usr / share / ewebkit-0 / webinspector / 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         ConsoleAgent.enable();
57     },
58
59     /**
60      * @param {WebInspector.ConsoleMessage} msg
61      */
62     addMessage: function(msg)
63     {
64         this.messages.push(msg);
65         this._previousMessage = msg;
66         this._incrementErrorWarningCount(msg);
67         this.dispatchEventToListeners(WebInspector.ConsoleModel.Events.MessageAdded, msg);
68         this._interruptRepeatCount = false;
69     },
70
71     /**
72      * @param {WebInspector.ConsoleMessage} msg
73      */
74     _incrementErrorWarningCount: function(msg)
75     {
76         switch (msg.level) {
77             case WebInspector.ConsoleMessage.MessageLevel.Warning:
78                 this.warnings += msg.repeatDelta;
79                 break;
80             case WebInspector.ConsoleMessage.MessageLevel.Error:
81                 this.errors += msg.repeatDelta;
82                 break;
83         }
84     },
85
86     requestClearMessages: function()
87     {
88         ConsoleAgent.clearMessages();
89         this.clearMessages();
90     },
91
92     clearMessages: function()
93     {
94         this.messages = [];
95
96         this.errors = 0;
97         this.warnings = 0;
98
99         this.dispatchEventToListeners(WebInspector.ConsoleModel.Events.ConsoleCleared);
100     },
101
102     interruptRepeatCount: function()
103     {
104         this._interruptRepeatCount = true;
105     },
106
107     _messageRepeatCountUpdated: function(count)
108     {
109         var msg = this._previousMessage;
110         if (!msg)
111             return;
112
113         var prevRepeatCount = msg.totalRepeatCount;
114
115         if (!this._interruptRepeatCount) {
116             msg.repeatDelta = count - prevRepeatCount;
117             msg.repeatCount = msg.repeatCount + msg.repeatDelta;
118             msg.totalRepeatCount = count;
119             msg.updateRepeatCount();
120
121             this._incrementErrorWarningCount(msg);
122             this.dispatchEventToListeners(WebInspector.ConsoleModel.Events.RepeatCountUpdated, msg);
123         } else {
124             var msgCopy = WebInspector.ConsoleMessage.create(msg.source, msg.level, msg._messageText, msg.type, msg.url, msg.line, count - prevRepeatCount, msg._parameters, msg._stackTrace, msg._request);
125             msgCopy.totalRepeatCount = count;
126             this.addMessage(msgCopy);
127         }
128     }
129 }
130
131 WebInspector.ConsoleModel.prototype.__proto__ = WebInspector.Object.prototype;
132
133 /**
134  * @constructor
135  */
136 WebInspector.ConsoleMessage = function()
137 {
138     this.repeatDelta = 0;
139     this.repeatCount = 0;
140     this._totalRepeatCount = 0;
141 }
142
143 WebInspector.ConsoleMessage.prototype = {
144     /**
145      * @return {number}
146      */
147     get totalRepeatCount()
148     {
149         return this._totalRepeatCount;
150     },
151
152     set totalRepeatCount(totalRepeatCount)
153     {
154         this._totalRepeatCount = totalRepeatCount;
155     },
156
157     updateRepeatCount: function()
158     {
159         // Implemented by concrete instances
160     }
161 }
162
163 /**
164  * @param {string} source
165  * @param {string} level
166  * @param {string} message
167  * @param {string=} type
168  * @param {string=} url
169  * @param {number=} line
170  * @param {number=} repeatCount
171  * @param {Array.<RuntimeAgent.RemoteObject>=} parameters
172  * @param {ConsoleAgent.StackTrace=} stackTrace
173  * @param {WebInspector.Resource=} request
174  *
175  * @return {WebInspector.ConsoleMessage}
176  */
177 WebInspector.ConsoleMessage.create = function(source, level, message, type, url, line, repeatCount, parameters, stackTrace, request)
178 {
179 }
180
181 // Note: Keep these constants in sync with the ones in Console.h
182 WebInspector.ConsoleMessage.MessageSource = {
183     HTML: "html",
184     XML: "xml",
185     JS: "javascript",
186     Network: "network",
187     ConsoleAPI: "console-api",
188     Other: "other"
189 }
190
191 WebInspector.ConsoleMessage.MessageType = {
192     Log: "log",
193     Dir: "dir",
194     DirXML: "dirxml",
195     Trace: "trace",
196     StartGroup: "startGroup",
197     StartGroupCollapsed: "startGroupCollapsed",
198     EndGroup: "endGroup",
199     Assert: "assert",
200     Result: "result"
201 }
202
203 WebInspector.ConsoleMessage.MessageLevel = {
204     Tip: "tip",
205     Log: "log",
206     Warning: "warning",
207     Error: "error",
208     Debug: "debug"
209 }
210
211
212 /**
213  * @constructor
214  * @implements {ConsoleAgent.Dispatcher}
215  * @param {WebInspector.ConsoleModel} console
216  */
217 WebInspector.ConsoleDispatcher = function(console)
218 {
219     this._console = console;
220 }
221
222 WebInspector.ConsoleDispatcher.prototype = {
223     /**
224      * @param {ConsoleAgent.ConsoleMessage} payload
225      */
226     messageAdded: function(payload)
227     {
228         var consoleMessage = WebInspector.ConsoleMessage.create(
229             payload.source,
230             payload.level,
231             payload.text,
232             payload.type,
233             payload.url,
234             payload.line,
235             payload.repeatCount,
236             payload.parameters,
237             payload.stackTrace,
238             payload.networkRequestId ? WebInspector.networkResourceById(payload.networkRequestId) : undefined);
239         this._console.addMessage(consoleMessage);
240     },
241
242     /**
243      * @param {number} count
244      */
245     messageRepeatCountUpdated: function(count)
246     {
247         this._console._messageRepeatCountUpdated(count);
248     },
249
250     messagesCleared: function()
251     {
252         if (!WebInspector.settings.preserveConsoleLog.get())
253             this._console.clearMessages();
254     }
255 }
256
257 /**
258  * @type {?WebInspector.ConsoleModel}
259  */
260 WebInspector.console = null;