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