2010-08-09 Ilya Tikhonovsky <loislo@chromium.org>
[WebKit-https.git] / WebCore / inspector / front-end / BreakpointManager.js
1 /*
2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3  * Copyright (C) 2010 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 WebInspector.BreakpointManager = function()
28 {
29     this._breakpoints = {};
30 }
31
32 WebInspector.BreakpointManager.prototype = {
33     setOneTimeBreakpoint: function(sourceID, line)
34     {
35         var breakpoint = new WebInspector.Breakpoint(this, sourceID, undefined, line, true, undefined);
36         if (this._breakpoints[breakpoint.id])
37             return;
38         if (this._oneTimeBreakpoint)
39             InspectorBackend.removeBreakpoint(this._oneTimeBreakpoint.sourceID, this._oneTimeBreakpoint.line);
40         this._oneTimeBreakpoint = breakpoint;
41         // FIXME(40669): one time breakpoint will be persisted in inspector settings if not hit.
42         this._setBreakpointOnBackend(breakpoint, true);
43     },
44
45     removeOneTimeBreakpoint: function()
46     {
47         if (this._oneTimeBreakpoint) {
48             InspectorBackend.removeBreakpoint(this._oneTimeBreakpoint.sourceID, this._oneTimeBreakpoint.line);
49             delete this._oneTimeBreakpoint;
50         }
51     },
52
53     setBreakpoint: function(sourceID, sourceURL, line, enabled, condition)
54     {
55         var breakpoint = this._setBreakpoint(sourceID, sourceURL, line, enabled, condition);
56         if (breakpoint)
57             this._setBreakpointOnBackend(breakpoint);
58     },
59
60     restoredBreakpoint: function(sourceID, sourceURL, line, enabled, condition)
61     {
62         this._setBreakpoint(sourceID, sourceURL, line, enabled, condition);
63     },
64
65     removeBreakpoint: function(breakpoint)
66     {
67         if (this._removeBreakpoint(breakpoint))
68             InspectorBackend.removeBreakpoint(breakpoint.sourceID, breakpoint.line);
69     },
70
71     breakpointsForSourceID: function(sourceID)
72     {
73         var breakpoints = [];
74         for (var id in this._breakpoints) {
75             if (this._breakpoints[id].sourceID === sourceID)
76                 breakpoints.push(this._breakpoints[id]);
77         }
78         return breakpoints;
79     },
80
81     breakpointsForURL: function(url)
82     {
83         var breakpoints = [];
84         for (var id in this._breakpoints) {
85             if (this._breakpoints[id].url === url)
86                 breakpoints.push(this._breakpoints[id]);
87         }
88         return breakpoints;
89     },
90
91     reset: function()
92     {
93         this._breakpoints = {};
94         delete this._oneTimeBreakpoint;
95     },
96
97     _setBreakpoint: function(sourceID, sourceURL, line, enabled, condition)
98     {
99         var breakpoint = new WebInspector.Breakpoint(this, sourceID, sourceURL, line, enabled, condition);
100         if (this._breakpoints[breakpoint.id])
101             return;
102         if (this._oneTimeBreakpoint && (this._oneTimeBreakpoint.id == breakpoint.id))
103             delete this._oneTimeBreakpoint;
104         this._breakpoints[breakpoint.id] = breakpoint;
105         this.dispatchEventToListeners("breakpoint-added", breakpoint);
106         return breakpoint;
107     },
108
109     _removeBreakpoint: function(breakpoint)
110     {
111         if (!(breakpoint.id in this._breakpoints))
112             return false;
113         breakpoint.removeAllListeners();
114         delete breakpoint._breakpointManager;
115         delete this._breakpoints[breakpoint.id];
116         this.dispatchEventToListeners("breakpoint-removed", breakpoint);
117         return true;
118     },
119
120     _setBreakpointOnBackend: function(breakpoint, isOneTime)
121     {
122         function didSetBreakpoint(success, line)
123         {
124             if (success && line == breakpoint.line)
125                 return;
126             if (isOneTime) {
127                 if (success)
128                     this._oneTimeBreakpoint.line = line;
129                 else
130                     delete this._oneTimeBreakpoint;
131             } else {
132                 this._removeBreakpoint(breakpoint);
133                 if (success)
134                     this._setBreakpoint(breakpoint.sourceID, breakpoint.sourceURL, line, breakpoint.enabled, breakpoint.condition);
135             }
136         }
137         var callbackId = WebInspector.Callback.wrap(didSetBreakpoint.bind(this));
138         InspectorBackend.setBreakpoint(callbackId, breakpoint.sourceID, breakpoint.line, breakpoint.enabled, breakpoint.condition);
139     }
140 }
141
142 WebInspector.BreakpointManager.prototype.__proto__ = WebInspector.Object.prototype;
143
144 WebInspector.Breakpoint = function(breakpointManager, sourceID, sourceURL, line, enabled, condition)
145 {
146     this.url = sourceURL;
147     this.line = line;
148     this.sourceID = sourceID;
149     this._enabled = enabled;
150     this._condition = condition || "";
151     this._sourceText = "";
152     this._breakpointManager = breakpointManager;
153 }
154
155 WebInspector.Breakpoint.prototype = {
156     get enabled()
157     {
158         return this._enabled;
159     },
160
161     set enabled(x)
162     {
163         if (this._enabled === x)
164             return;
165
166         this._enabled = x;
167         this._breakpointManager._setBreakpointOnBackend(this);
168         if (this._enabled)
169             this.dispatchEventToListeners("enabled");
170         else
171             this.dispatchEventToListeners("disabled");
172     },
173
174     get sourceText()
175     {
176         return this._sourceText;
177     },
178
179     set sourceText(text)
180     {
181         this._sourceText = text;
182         this.dispatchEventToListeners("text-changed");
183     },
184
185     get label()
186     {
187         var displayName = (this.url ? WebInspector.displayNameForURL(this.url) : WebInspector.UIString("(program)"));
188         return displayName + ":" + this.line;
189     },
190
191     get id()
192     {
193         return this.sourceID + ":" + this.line;
194     },
195
196     get condition()
197     {
198         return this._condition;
199     },
200
201     set condition(c)
202     {
203         c = c || "";
204         if (this._condition === c)
205             return;
206
207         this._condition = c;
208         if (this.enabled)
209             this._breakpointManager._setBreakpointOnBackend(this);
210         this.dispatchEventToListeners("condition-changed");
211     }
212 }
213
214 WebInspector.Breakpoint.prototype.__proto__ = WebInspector.Object.prototype;
215