OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @constructor | |
7 * @extends {WebInspector.Object} | |
8 * @implements {WebInspector.TargetManager.Observer} | |
9 */ | |
10 WebInspector.DOMBreakpointManager = function() | |
11 { | |
12 /** @type {!Map<!WebInspector.Target, !Protocol.DOMDebuggerAgent>} */ | |
13 this._agents = new Map(); | |
14 this._domBreakpointsSetting = WebInspector.settings.createLocalSetting("domB reakpoints", []); | |
15 /** @type {!Map<string, !WebInspector.DOMBreakpointManager.Breakpoint>} */ | |
16 this._breakpoints = new Map(); | |
17 WebInspector.targetManager.addModelListener(WebInspector.DOMModel, WebInspec tor.DOMModel.Events.DocumentUpdated, this._restoreBreakpoints, this); | |
18 WebInspector.targetManager.addModelListener(WebInspector.DOMModel, WebInspec tor.DOMModel.Events.NodeRemoved, this._nodeRemoved, this); | |
19 WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Event s.InspectedURLChanged, this._inspectedURLChanged, this); | |
20 WebInspector.targetManager.observeTargets(this); | |
21 } | |
22 | |
23 WebInspector.DOMBreakpointManager.Marker = "breakpoint-marker"; | |
24 | |
25 WebInspector.DOMBreakpointManager.Events = { | |
26 BreakpointsChanged: "BreakpointsChanged" | |
27 }; | |
28 | |
29 WebInspector.DOMBreakpointManager.prototype = { | |
30 /** | |
31 * @override | |
32 * @param {!WebInspector.Target} target | |
33 */ | |
34 targetAdded: function(target) | |
35 { | |
36 if (target.hasDOMCapability()) | |
37 this._agents.set(target, target.domdebuggerAgent()); | |
38 }, | |
39 | |
40 /** | |
41 * @override | |
42 * @param {!WebInspector.Target} target | |
43 */ | |
44 targetRemoved: function(target) | |
45 { | |
46 if (target.hasDOMCapability()) | |
47 this._agents.delete(target); | |
48 }, | |
49 | |
50 /** | |
51 * @return {!Map<string, !WebInspector.DOMBreakpointManager.Breakpoint>} | |
lushnikov
2016/08/11 18:40:18
why do we return map here and not an array?
chenwilliam
2016/08/17 01:20:11
I was returning a map since that's how the class w
| |
52 */ | |
53 domBreakpoints: function() | |
54 { | |
55 return this._breakpoints; | |
56 }, | |
57 | |
58 /** | |
59 * @param {!WebInspector.DOMNode} node | |
60 * @param {!DOMDebuggerAgent.DOMBreakpointType} type | |
61 * @param {boolean} enabled | |
62 */ | |
63 setBreakpoint: function(node, type, enabled) | |
64 { | |
65 this._innerSetBreakpoint(node, type, enabled); | |
66 this._saveBreakpoints(); | |
67 }, | |
68 | |
69 /** | |
70 * @param {!WebInspector.DOMNode} node | |
71 * @param {!DOMDebuggerAgent.DOMBreakpointType} type | |
72 * @param {boolean} enabled | |
73 */ | |
74 _innerSetBreakpoint: function(node, type, enabled) | |
75 { | |
76 var breakpoint = new WebInspector.DOMBreakpointManager.Breakpoint(node, type, this._inspectedURL, enabled); | |
lushnikov
2016/08/11 18:40:18
instead of using inspectedURL, we probably should
chenwilliam
2016/08/17 01:20:11
Done.
| |
77 this._breakpoints.set(this._breakpointId(node, type), breakpoint); | |
78 var agent = this._agents.get(node.target()); | |
79 if (enabled) { | |
80 agent.setDOMBreakpoint(node.id, type); | |
81 node.setMarker(WebInspector.DOMBreakpointManager.Marker, true); | |
82 } else { | |
83 agent.removeDOMBreakpoint(node.id, type); | |
84 node.setMarker(WebInspector.DOMBreakpointManager.Marker, this._hasOt herBreakpoints(node, type) ? true : null); | |
85 } | |
86 }, | |
87 | |
88 /** | |
89 * @param {!WebInspector.DOMNode} node | |
90 * @param {!DOMDebuggerAgent.DOMBreakpointType} type | |
91 */ | |
92 removeBreakpoint: function(node, type) | |
93 { | |
94 this._innerRemoveBreakpoint(node, type); | |
95 this._saveBreakpoints(); | |
96 }, | |
97 | |
98 /** | |
99 * @param {!WebInspector.DOMNode} node | |
100 * @param {!DOMDebuggerAgent.DOMBreakpointType} type | |
101 */ | |
102 _innerRemoveBreakpoint: function(node, type) | |
103 { | |
104 var breakpointId = this._breakpointId(node, type); | |
105 var breakpoint = this._breakpoints.get(breakpointId); | |
106 if (breakpoint.enabled) { | |
107 var agent = this._agents.get(node.target()); | |
108 agent.removeDOMBreakpoint(node.id, type); | |
109 node.setMarker(WebInspector.DOMBreakpointManager.Marker, this._hasOt herBreakpoints(node, type) ? true : null); | |
110 } | |
111 this._breakpoints.remove(breakpointId); | |
112 }, | |
113 | |
114 /** | |
115 * @param {!WebInspector.DOMNode} node | |
116 * @param {!DOMDebuggerAgent.DOMBreakpointType} type | |
117 */ | |
118 toggleBreakpoint: function(node, type) | |
119 { | |
120 var nodeBreakpoints = this.nodeBreakpoints(node); | |
121 if (!nodeBreakpoints[type]) | |
122 this.setBreakpoint(node, type, true); | |
123 else | |
124 this.removeBreakpoint(node, type); | |
125 }, | |
126 | |
127 /** | |
128 * @param {!WebInspector.DOMNode} node | |
129 */ | |
130 removeBreakpointsForNode: function(node) | |
lushnikov
2016/08/11 18:40:19
why is this part of public API? AFAIU It's not use
chenwilliam
2016/08/17 01:20:11
Done.
| |
131 { | |
132 for (var breakpoint of this._breakpoints.values()) { | |
133 if (breakpoint.node === node) | |
134 this._innerRemoveBreakpoint(breakpoint.node, breakpoint.type); | |
135 } | |
136 this._saveBreakpoints(); | |
137 }, | |
138 | |
139 removeAllBreakpoints: function() | |
140 { | |
141 for (var breakpoint of this._breakpoints.values()) | |
142 this._innerRemoveBreakpoint(breakpoint.node, breakpoint.type); | |
143 this._saveBreakpoints(); | |
144 }, | |
145 | |
146 /** | |
147 * @param {!WebInspector.DOMNode} node | |
148 * @return {!Object<string, boolean>} | |
lushnikov
2016/08/11 18:40:19
return Map<string, boolean>
chenwilliam
2016/08/17 01:20:11
Done.
| |
149 */ | |
150 nodeBreakpoints: function(node) | |
151 { | |
152 var nodeBreakpoints = {}; | |
153 for (var breakpoint of this._breakpoints.values()) { | |
154 if (breakpoint.node === node && breakpoint.enabled) | |
155 nodeBreakpoints[breakpoint.type] = true; | |
156 } | |
157 return nodeBreakpoints; | |
158 }, | |
159 | |
160 /** | |
161 * @param {!WebInspector.Event} event | |
162 */ | |
163 _restoreBreakpoints: function(event) | |
164 { | |
165 var domModel = /** @type {!WebInspector.DOMModel} */ (event.target); | |
166 var pathToBreakpoints = {}; | |
167 this._breakpoints = new Map(); | |
lushnikov
2016/08/11 18:40:19
this._breakpoints.clear(); to play nice with closu
chenwilliam
2016/08/17 01:20:11
Done.
| |
168 | |
169 /** | |
170 * @param {string} path | |
171 * @param {?DOMAgent.NodeId} nodeId | |
172 * @this {WebInspector.DOMBreakpointManager} | |
173 */ | |
174 function didPushNodeByPathToFrontend(path, nodeId) | |
175 { | |
176 var node = nodeId ? domModel.nodeForId(nodeId) : null; | |
177 if (!node) | |
178 return; | |
179 | |
180 for (var breakpoint of pathToBreakpoints[path]) | |
181 this._innerSetBreakpoint(node, breakpoint.type, breakpoint.enabl ed); | |
182 this.dispatchEventToListeners(WebInspector.DOMBreakpointManager.Even ts.BreakpointsChanged); | |
183 } | |
184 | |
185 var breakpoints = this._domBreakpointsSetting.get(); | |
186 for (var breakpoint of breakpoints) { | |
187 if (breakpoint.url !== this._inspectedURL) | |
188 continue; | |
189 var path = breakpoint.path; | |
190 if (!pathToBreakpoints[path]) { | |
191 pathToBreakpoints[path] = []; | |
192 domModel.pushNodeByPathToFrontend(path, didPushNodeByPathToFront end.bind(this, path)); | |
193 } | |
194 pathToBreakpoints[path].push(breakpoint); | |
195 } | |
196 }, | |
197 | |
198 _saveBreakpoints: function() | |
199 { | |
200 var breakpoints = []; | |
201 var storedBreakpoints = this._domBreakpointsSetting.get(); | |
202 for (var breakpoint of storedBreakpoints) { | |
203 if (breakpoint.url !== this._inspectedURL) | |
204 breakpoints.push(breakpoint); | |
205 } | |
206 for (var breakpoint of this._breakpoints.values()) { | |
207 breakpoints.push({ | |
208 url: this._inspectedURL, | |
209 path: breakpoint.node.path(), | |
210 type: breakpoint.type, | |
211 enabled: breakpoint.enabled | |
212 }); | |
213 } | |
214 this._domBreakpointsSetting.set(breakpoints); | |
215 this.dispatchEventToListeners(WebInspector.DOMBreakpointManager.Events.B reakpointsChanged); | |
216 }, | |
217 | |
218 /** | |
219 * @param {!WebInspector.DOMNode} node | |
220 * @param {!DOMDebuggerAgent.DOMBreakpointType} type | |
221 * @return {boolean} | |
222 */ | |
223 _hasOtherBreakpoints: function(node, type) | |
224 { | |
225 for (var breakpoint of this._breakpoints.values()) { | |
226 if (breakpoint.node === node && breakpoint.type !== type && breakpoi nt.enabled) | |
227 return true; | |
228 } | |
229 return false; | |
230 }, | |
231 | |
232 /** | |
233 * @param {!WebInspector.Event} event | |
234 */ | |
235 _nodeRemoved: function(event) | |
236 { | |
237 var node = event.data.node; | |
238 this.removeBreakpointsForNode(node); | |
239 var children = node.children(); | |
240 if (!children) | |
241 return; | |
242 for (var child of children) | |
243 this.removeBreakpointsForNode(child); | |
244 this._saveBreakpoints(); | |
245 }, | |
246 | |
247 /** | |
248 * @param {!WebInspector.Event} event | |
249 */ | |
250 _inspectedURLChanged: function(event) | |
251 { | |
252 var target = /** @type {!WebInspector.Target} */ (event.data); | |
lushnikov
2016/08/11 18:40:18
why listen to this event altogether? it appears it
chenwilliam
2016/08/17 01:20:11
Done.
| |
253 if (target !== WebInspector.targetManager.mainTarget()) | |
254 return; | |
255 this._inspectedURL = target.inspectedURL().removeURLFragment(); | |
256 this._breakpoints = new Map(); | |
257 this.dispatchEventToListeners(WebInspector.DOMBreakpointManager.Events.B reakpointsChanged); | |
258 }, | |
259 | |
260 /** | |
261 * @param {!WebInspector.DOMNode} node | |
262 * @param {!DOMDebuggerAgent.DOMBreakpointType} type | |
263 * @return {string} | |
264 */ | |
265 _breakpointId: function(node, type) | |
266 { | |
267 return `${node.id}__${type}`; | |
lushnikov
2016/08/11 18:40:19
let's be consistent and separate with colon
chenwilliam
2016/08/17 01:20:11
Done.
| |
268 }, | |
269 | |
270 __proto__: WebInspector.Object.prototype | |
271 }; | |
272 | |
273 /** | |
274 * @constructor | |
275 * @param {!WebInspector.DOMNode} node | |
276 * @param {!DOMDebuggerAgent.DOMBreakpointType} type | |
277 * @param {string} url | |
278 * @param {boolean} enabled | |
279 */ | |
280 WebInspector.DOMBreakpointManager.Breakpoint = function(node, type, url, enabled ) | |
281 { | |
282 this.node = node; | |
283 this.path = node.path(); | |
284 this.type = type; | |
285 this.url = url; | |
286 this.enabled = enabled; | |
287 } | |
288 | |
289 /** | |
290 * @type {!WebInspector.DOMBreakpointManager} | |
291 */ | |
292 WebInspector.domBreakpointManager; | |
OLD | NEW |