Web Inspector: make ScriptsPanel compile
[WebKit-https.git] / Source / WebCore / inspector / front-end / DebuggerModel.js
1 /*
2  * Copyright (C) 2010 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.DebuggerModel = function()
36 {
37     this._debuggerPausedDetails = {};
38     this._scripts = {};
39
40     InspectorBackend.registerDebuggerDispatcher(new WebInspector.DebuggerDispatcher(this));
41 }
42
43 /**
44  * @constructor
45  */
46 WebInspector.DebuggerModel.Location = function()
47 {
48     this.scriptId = null;
49 }
50
51 WebInspector.DebuggerModel.Events = {
52     DebuggerWasEnabled: "debugger-was-enabled",
53     DebuggerWasDisabled: "debugger-was-disabled",
54     DebuggerPaused: "debugger-paused",
55     DebuggerResumed: "debugger-resumed",
56     ParsedScriptSource: "parsed-script-source",
57     FailedToParseScriptSource: "failed-to-parse-script-source",
58     BreakpointResolved: "breakpoint-resolved",
59     Reset: "reset"
60 }
61
62 WebInspector.DebuggerModel.BreakReason = {
63     DOM: "DOM",
64     EventListener: "EventListener",
65     XHR: "XHR",
66     Exception: "exception"
67 }
68
69 WebInspector.DebuggerModel.prototype = {
70     enableDebugger: function()
71     {
72         DebuggerAgent.enable();
73     },
74
75     disableDebugger: function()
76     {
77         DebuggerAgent.disable();
78     },
79
80     _debuggerWasEnabled: function()
81     {
82         this.dispatchEventToListeners(WebInspector.DebuggerModel.Events.DebuggerWasEnabled);
83     },
84
85     _debuggerWasDisabled: function()
86     {
87         this.dispatchEventToListeners(WebInspector.DebuggerModel.Events.DebuggerWasDisabled);
88     },
89
90     continueToLocation: function(location)
91     {
92         DebuggerAgent.continueToLocation(location);
93     },
94
95     setBreakpointByScriptLocation: function(location, condition, callback)
96     {
97         var script = this.scriptForSourceID(location.scriptId);
98         if (script.sourceURL)
99             this.setBreakpoint(script.sourceURL, location.lineNumber, location.columnNumber, condition, callback);
100         else
101             this.setBreakpointBySourceId(location, condition, callback);
102     },
103
104     setBreakpoint: function(url, lineNumber, columnNumber, condition, callback)
105     {
106         // Adjust column if needed.
107         var minColumnNumber = 0;
108         for (var id in this._scripts) {
109             var script = this._scripts[id];
110             if (url === script.sourceURL && lineNumber === script.lineOffset)
111                 minColumnNumber = minColumnNumber ? Math.min(minColumnNumber, script.columnOffset) : script.columnOffset;
112         }
113         columnNumber = Math.max(columnNumber, minColumnNumber);
114
115         function didSetBreakpoint(error, breakpointId, locations)
116         {
117             if (callback)
118                 callback(error ? null : breakpointId, locations);
119         }
120         DebuggerAgent.setBreakpointByUrl(lineNumber, url, undefined, columnNumber, condition, didSetBreakpoint.bind(this));
121         WebInspector.userMetrics.ScriptsBreakpointSet.record();
122     },
123
124     setBreakpointBySourceId: function(location, condition, callback)
125     {
126         function didSetBreakpoint(error, breakpointId, actualLocation)
127         {
128             if (callback)
129                 callback(error ? null : breakpointId, [actualLocation]);
130         }
131         DebuggerAgent.setBreakpoint(location, condition, didSetBreakpoint.bind(this));
132         WebInspector.userMetrics.ScriptsBreakpointSet.record();
133     },
134
135     removeBreakpoint: function(breakpointId, callback)
136     {
137         DebuggerAgent.removeBreakpoint(breakpointId, callback);
138     },
139
140     _breakpointResolved: function(breakpointId, location)
141     {
142         this.dispatchEventToListeners(WebInspector.DebuggerModel.Events.BreakpointResolved, {breakpointId: breakpointId, location: location});
143     },
144
145     reset: function()
146     {
147         this._debuggerPausedDetails = {};
148         this._scripts = {};
149         this.dispatchEventToListeners(WebInspector.DebuggerModel.Events.Reset);
150     },
151
152     get scripts()
153     {
154         return this._scripts;
155     },
156
157     scriptForSourceID: function(scriptId)
158     {
159         return this._scripts[scriptId];
160     },
161
162     scriptsForURL: function(url)
163     {
164         return this.queryScripts(function(s) { return s.sourceURL === url; });
165     },
166
167     queryScripts: function(filter)
168     {
169         var scripts = [];
170         for (var scriptId in this._scripts) {
171             var script = this._scripts[scriptId];
172             if (filter(script))
173                 scripts.push(script);
174         }
175         return scripts;
176     },
177
178     setScriptSource: function(scriptId, newSource, callback)
179     {
180         this._scripts[scriptId].editSource(newSource, this._didEditScriptSource.bind(this, scriptId, newSource, callback));
181     },
182
183     _didEditScriptSource: function(scriptId, newSource, callback, error, callFrames)
184     {
185         if (!error && callFrames && callFrames.length)
186             this._debuggerPausedDetails.callFrames = callFrames;
187         callback(error);
188     },
189
190     get callFrames()
191     {
192         return this._debuggerPausedDetails.callFrames;
193     },
194
195     get debuggerPausedDetails()
196     {
197         return this._debuggerPausedDetails;
198     },
199
200     _pausedScript: function(callFrames, reason, auxData)
201     {
202         var details = { callFrames: callFrames, reason: reason, auxData: auxData };
203         this._debuggerPausedDetails = details;
204         this.dispatchEventToListeners(WebInspector.DebuggerModel.Events.DebuggerPaused, details);
205     },
206
207     _resumedScript: function()
208     {
209         this._debuggerPausedDetails = {};
210         this.dispatchEventToListeners(WebInspector.DebuggerModel.Events.DebuggerResumed);
211     },
212
213     _parsedScriptSource: function(scriptId, sourceURL, startLine, startColumn, endLine, endColumn, isContentScript)
214     {
215         var script = new WebInspector.Script(scriptId, sourceURL, startLine, startColumn, endLine, endColumn, undefined, undefined, isContentScript);
216         this._scripts[scriptId] = script;
217         this.dispatchEventToListeners(WebInspector.DebuggerModel.Events.ParsedScriptSource, script);
218     },
219
220     _failedToParseScriptSource: function(sourceURL, source, startingLine, errorLine, errorMessage)
221     {
222         var script = new WebInspector.Script(null, sourceURL, startingLine, 0, 0, 0, errorLine, errorMessage, undefined);
223         this.dispatchEventToListeners(WebInspector.DebuggerModel.Events.FailedToParseScriptSource, script);
224     }
225 }
226
227 WebInspector.DebuggerModel.prototype.__proto__ = WebInspector.Object.prototype;
228
229 WebInspector.DebuggerEventTypes = {
230     JavaScriptPause: 0,
231     JavaScriptBreakpoint: 1,
232     NativeBreakpoint: 2
233 };
234
235 /**
236  * @constructor
237  * @implements {DebuggerAgent.Dispatcher}
238  */
239 WebInspector.DebuggerDispatcher = function(debuggerModel)
240 {
241     this._debuggerModel = debuggerModel;
242 }
243
244 WebInspector.DebuggerDispatcher.prototype = {
245     paused: function(callFrames, reason, auxData)
246     {
247         this._debuggerModel._pausedScript(callFrames, reason, auxData);
248     },
249
250     resumed: function()
251     {
252         this._debuggerModel._resumedScript();
253     },
254
255     debuggerWasEnabled: function()
256     {
257         this._debuggerModel._debuggerWasEnabled();
258     },
259
260     debuggerWasDisabled: function()
261     {
262         this._debuggerModel._debuggerWasDisabled();
263     },
264
265     scriptParsed: function(scriptId, sourceURL, startLine, startColumn, endLine, endColumn, isContentScript)
266     {
267         this._debuggerModel._parsedScriptSource(scriptId, sourceURL, startLine, startColumn, endLine, endColumn, isContentScript);
268     },
269
270     scriptFailedToParse: function(sourceURL, source, startingLine, errorLine, errorMessage)
271     {
272         this._debuggerModel._failedToParseScriptSource(sourceURL, source, startingLine, errorLine, errorMessage);
273     },
274
275     breakpointResolved: function(breakpointId, scriptId, lineNumber, columnNumber)
276     {
277         this._debuggerModel._breakpointResolved(breakpointId, scriptId, lineNumber, columnNumber);
278     }
279 }
280
281 /**
282  * @type {?WebInspector.DebuggerModel}
283  */
284 WebInspector.debuggerModel = null;