Web Inspector: document Debugger.paused, introduce types for ids in Debugger domain.
[WebKit.git] / Source / WebCore / inspector / InspectorDOMDebuggerAgent.cpp
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 #include "config.h"
32
33 #include "InspectorDOMDebuggerAgent.h"
34
35 #if ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)
36
37 #include "HTMLElement.h"
38 #include "InspectorAgent.h"
39 #include "InspectorDOMAgent.h"
40 #include "InspectorDebuggerAgent.h"
41 #include "InspectorState.h"
42 #include "InspectorValues.h"
43 #include "InstrumentingAgents.h"
44 #include <wtf/text/WTFString.h>
45
46 namespace {
47
48 enum DOMBreakpointType {
49     SubtreeModified = 0,
50     AttributeModified,
51     NodeRemoved,
52     DOMBreakpointTypesCount
53 };
54
55 static const char* const domNativeBreakpointType = "DOM";
56 static const char* const eventListenerNativeBreakpointType = "EventListener";
57 static const char* const xhrNativeBreakpointType = "XHR";
58
59 const uint32_t inheritableDOMBreakpointTypesMask = (1 << SubtreeModified);
60 const int domBreakpointDerivedTypeShift = 16;
61
62 }
63
64 namespace WebCore {
65
66 namespace DOMDebuggerAgentState {
67 static const char eventListenerBreakpoints[] = "eventListenerBreakpoints";
68 static const char pauseOnAllXHRs[] = "pauseOnAllXHRs";
69 static const char xhrBreakpoints[] = "xhrBreakpoints";
70 }
71
72 PassOwnPtr<InspectorDOMDebuggerAgent> InspectorDOMDebuggerAgent::create(InstrumentingAgents* instrumentingAgents, InspectorState* inspectorState, InspectorDOMAgent* domAgent, InspectorDebuggerAgent* debuggerAgent, InspectorAgent* inspectorAgent)
73 {
74     return adoptPtr(new InspectorDOMDebuggerAgent(instrumentingAgents, inspectorState, domAgent, debuggerAgent, inspectorAgent));
75 }
76
77 InspectorDOMDebuggerAgent::InspectorDOMDebuggerAgent(InstrumentingAgents* instrumentingAgents, InspectorState* inspectorState, InspectorDOMAgent* domAgent, InspectorDebuggerAgent* debuggerAgent, InspectorAgent* inspectorAgent)
78     : m_instrumentingAgents(instrumentingAgents)
79     , m_inspectorState(inspectorState)
80     , m_domAgent(domAgent)
81     , m_debuggerAgent(debuggerAgent)
82     , m_inspectorAgent(inspectorAgent)
83 {
84     m_debuggerAgent->setListener(this);
85 }
86
87 InspectorDOMDebuggerAgent::~InspectorDOMDebuggerAgent()
88 {
89     m_debuggerAgent->setListener(0);
90     ASSERT(!m_instrumentingAgents->inspectorDOMDebuggerAgent());
91 }
92
93 // Browser debugger agent enabled only when JS debugger is enabled.
94 void InspectorDOMDebuggerAgent::debuggerWasEnabled()
95 {
96     m_instrumentingAgents->setInspectorDOMDebuggerAgent(this);
97 }
98
99 void InspectorDOMDebuggerAgent::debuggerWasDisabled()
100 {
101     disable();
102 }
103
104 void InspectorDOMDebuggerAgent::disable()
105 {
106     m_instrumentingAgents->setInspectorDOMDebuggerAgent(0);
107     clear();
108 }
109
110 void InspectorDOMDebuggerAgent::clearFrontend()
111 {
112     disable();
113 }
114
115 void InspectorDOMDebuggerAgent::discardBindings()
116 {
117     m_domBreakpoints.clear();
118 }
119
120 void InspectorDOMDebuggerAgent::setEventListenerBreakpoint(ErrorString* error, const String& eventName)
121 {
122     if (eventName.isEmpty()) {
123         *error = "Event name is empty";
124         return;
125     }
126
127     RefPtr<InspectorObject> eventListenerBreakpoints = m_inspectorState->getObject(DOMDebuggerAgentState::eventListenerBreakpoints);
128     eventListenerBreakpoints->setBoolean(eventName, true);
129     m_inspectorState->setObject(DOMDebuggerAgentState::eventListenerBreakpoints, eventListenerBreakpoints);
130 }
131
132 void InspectorDOMDebuggerAgent::removeEventListenerBreakpoint(ErrorString* error, const String& eventName)
133 {
134     if (eventName.isEmpty()) {
135         *error = "Event name is empty";
136         return;
137     }
138
139     RefPtr<InspectorObject> eventListenerBreakpoints = m_inspectorState->getObject(DOMDebuggerAgentState::eventListenerBreakpoints);
140     eventListenerBreakpoints->remove(eventName);
141     m_inspectorState->setObject(DOMDebuggerAgentState::eventListenerBreakpoints, eventListenerBreakpoints);
142 }
143
144 void InspectorDOMDebuggerAgent::didInvalidateStyleAttr(Node* node)
145 {
146     if (hasBreakpoint(node, AttributeModified)) {
147         RefPtr<InspectorObject> eventData = InspectorObject::create();
148         descriptionForDOMEvent(node, AttributeModified, false, eventData.get());
149         m_debuggerAgent->breakProgram(domNativeBreakpointType, eventData.release());
150     }
151 }
152
153 void InspectorDOMDebuggerAgent::didInsertDOMNode(Node* node)
154 {
155     if (m_domBreakpoints.size()) {
156         uint32_t mask = m_domBreakpoints.get(InspectorDOMAgent::innerParentNode(node));
157         uint32_t inheritableTypesMask = (mask | (mask >> domBreakpointDerivedTypeShift)) & inheritableDOMBreakpointTypesMask;
158         if (inheritableTypesMask)
159             updateSubtreeBreakpoints(node, inheritableTypesMask, true);
160     }
161 }
162
163 void InspectorDOMDebuggerAgent::didRemoveDOMNode(Node* node)
164 {
165     if (m_domBreakpoints.size()) {
166         // Remove subtree breakpoints.
167         m_domBreakpoints.remove(node);
168         Vector<Node*> stack(1, InspectorDOMAgent::innerFirstChild(node));
169         do {
170             Node* node = stack.last();
171             stack.removeLast();
172             if (!node)
173                 continue;
174             m_domBreakpoints.remove(node);
175             stack.append(InspectorDOMAgent::innerFirstChild(node));
176             stack.append(InspectorDOMAgent::innerNextSibling(node));
177         } while (!stack.isEmpty());
178     }
179 }
180
181 static int domTypeForName(const String& typeString)
182 {
183     if (typeString == "subtree-modified")
184         return SubtreeModified;
185     if (typeString == "attribute-modified")
186         return AttributeModified;
187     if (typeString == "node-removed")
188         return NodeRemoved;
189     return SubtreeModified;
190 }
191
192 static String domTypeName(int type)
193 {
194     switch (type) {
195     case SubtreeModified: return "subtree-modified";
196     case AttributeModified: return "attribute-modified";
197     case NodeRemoved: return "node-removed";
198     default: break;
199     }
200     return "";
201 }
202
203 void InspectorDOMDebuggerAgent::setDOMBreakpoint(ErrorString*, int nodeId, const String& typeString)
204 {
205     Node* node = m_domAgent->nodeForId(nodeId);
206     if (!node)
207         return;
208     int type = domTypeForName(typeString);
209
210     uint32_t rootBit = 1 << type;
211     m_domBreakpoints.set(node, m_domBreakpoints.get(node) | rootBit);
212     if (rootBit & inheritableDOMBreakpointTypesMask) {
213         for (Node* child = InspectorDOMAgent::innerFirstChild(node); child; child = InspectorDOMAgent::innerNextSibling(child))
214             updateSubtreeBreakpoints(child, rootBit, true);
215     }
216 }
217
218 void InspectorDOMDebuggerAgent::removeDOMBreakpoint(ErrorString*, int nodeId, const String& typeString)
219 {
220     Node* node = m_domAgent->nodeForId(nodeId);
221     if (!node)
222         return;
223     int type = domTypeForName(typeString);
224
225     uint32_t rootBit = 1 << type;
226     uint32_t mask = m_domBreakpoints.get(node) & ~rootBit;
227     if (mask)
228         m_domBreakpoints.set(node, mask);
229     else
230         m_domBreakpoints.remove(node);
231
232     if ((rootBit & inheritableDOMBreakpointTypesMask) && !(mask & (rootBit << domBreakpointDerivedTypeShift))) {
233         for (Node* child = InspectorDOMAgent::innerFirstChild(node); child; child = InspectorDOMAgent::innerNextSibling(child))
234             updateSubtreeBreakpoints(child, rootBit, false);
235     }
236 }
237
238 void InspectorDOMDebuggerAgent::willInsertDOMNode(Node*, Node* parent)
239 {
240     if (hasBreakpoint(parent, SubtreeModified)) {
241         RefPtr<InspectorObject> eventData = InspectorObject::create();
242         descriptionForDOMEvent(parent, SubtreeModified, true, eventData.get());
243         m_debuggerAgent->breakProgram(domNativeBreakpointType, eventData.release());
244     }
245 }
246
247 void InspectorDOMDebuggerAgent::willRemoveDOMNode(Node* node)
248 {
249     Node* parentNode = InspectorDOMAgent::innerParentNode(node);
250     if (hasBreakpoint(node, NodeRemoved)) {
251         RefPtr<InspectorObject> eventData = InspectorObject::create();
252         descriptionForDOMEvent(node, NodeRemoved, false, eventData.get());
253         m_debuggerAgent->breakProgram(domNativeBreakpointType, eventData.release());
254     } else if (parentNode && hasBreakpoint(parentNode, SubtreeModified)) {
255         RefPtr<InspectorObject> eventData = InspectorObject::create();
256         descriptionForDOMEvent(node, SubtreeModified, false, eventData.get());
257         m_debuggerAgent->breakProgram(domNativeBreakpointType, eventData.release());
258     }
259 }
260
261 void InspectorDOMDebuggerAgent::willModifyDOMAttr(Element* element)
262 {
263     if (hasBreakpoint(element, AttributeModified)) {
264         RefPtr<InspectorObject> eventData = InspectorObject::create();
265         descriptionForDOMEvent(element, AttributeModified, false, eventData.get());
266         m_debuggerAgent->breakProgram(domNativeBreakpointType, eventData.release());
267     }
268 }
269
270 void InspectorDOMDebuggerAgent::descriptionForDOMEvent(Node* target, int breakpointType, bool insertion, InspectorObject* description)
271 {
272     ASSERT(hasBreakpoint(target, breakpointType));
273
274     Node* breakpointOwner = target;
275     if ((1 << breakpointType) & inheritableDOMBreakpointTypesMask) {
276         // For inheritable breakpoint types, target node isn't always the same as the node that owns a breakpoint.
277         // Target node may be unknown to frontend, so we need to push it first.
278         RefPtr<InspectorObject> targetNodeObject = m_domAgent->resolveNode(target, InspectorDebuggerAgent::backtraceObjectGroup);
279         description->setObject("targetNode", targetNodeObject);
280
281         // Find breakpoint owner node.
282         if (!insertion)
283             breakpointOwner = InspectorDOMAgent::innerParentNode(target);
284         ASSERT(breakpointOwner);
285         while (!(m_domBreakpoints.get(breakpointOwner) & (1 << breakpointType))) {
286             breakpointOwner = InspectorDOMAgent::innerParentNode(breakpointOwner);
287             ASSERT(breakpointOwner);
288         }
289
290         if (breakpointType == SubtreeModified)
291             description->setBoolean("insertion", insertion);
292     }
293
294     int breakpointOwnerNodeId = m_domAgent->boundNodeId(breakpointOwner);
295     ASSERT(breakpointOwnerNodeId);
296     description->setNumber("nodeId", breakpointOwnerNodeId);
297     description->setString("type", domTypeName(breakpointType));
298 }
299
300 bool InspectorDOMDebuggerAgent::hasBreakpoint(Node* node, int type)
301 {
302     uint32_t rootBit = 1 << type;
303     uint32_t derivedBit = rootBit << domBreakpointDerivedTypeShift;
304     return m_domBreakpoints.get(node) & (rootBit | derivedBit);
305 }
306
307 void InspectorDOMDebuggerAgent::updateSubtreeBreakpoints(Node* node, uint32_t rootMask, bool set)
308 {
309     uint32_t oldMask = m_domBreakpoints.get(node);
310     uint32_t derivedMask = rootMask << domBreakpointDerivedTypeShift;
311     uint32_t newMask = set ? oldMask | derivedMask : oldMask & ~derivedMask;
312     if (newMask)
313         m_domBreakpoints.set(node, newMask);
314     else
315         m_domBreakpoints.remove(node);
316
317     uint32_t newRootMask = rootMask & ~newMask;
318     if (!newRootMask)
319         return;
320
321     for (Node* child = InspectorDOMAgent::innerFirstChild(node); child; child = InspectorDOMAgent::innerNextSibling(child))
322         updateSubtreeBreakpoints(child, newRootMask, set);
323 }
324
325 void InspectorDOMDebuggerAgent::pauseOnNativeEventIfNeeded(const String& categoryType, const String& eventName, bool synchronous)
326 {
327     String fullEventName = categoryType + ':' + eventName;
328     RefPtr<InspectorObject> eventListenerBreakpoints = m_inspectorState->getObject(DOMDebuggerAgentState::eventListenerBreakpoints);
329     if (eventListenerBreakpoints->find(fullEventName) == eventListenerBreakpoints->end())
330         return;
331
332     RefPtr<InspectorObject> eventData = InspectorObject::create();
333     eventData->setString("eventName", fullEventName);
334     if (synchronous)
335         m_debuggerAgent->breakProgram(eventListenerNativeBreakpointType, eventData.release());
336     else
337         m_debuggerAgent->schedulePauseOnNextStatement(eventListenerNativeBreakpointType, eventData.release());
338 }
339
340 void InspectorDOMDebuggerAgent::setXHRBreakpoint(ErrorString*, const String& url)
341 {
342     if (url.isEmpty()) {
343         m_inspectorState->setBoolean(DOMDebuggerAgentState::pauseOnAllXHRs, true);
344         return;
345     }
346
347     RefPtr<InspectorObject> xhrBreakpoints = m_inspectorState->getObject(DOMDebuggerAgentState::xhrBreakpoints);
348     xhrBreakpoints->setBoolean(url, true);
349     m_inspectorState->setObject(DOMDebuggerAgentState::xhrBreakpoints, xhrBreakpoints);
350 }
351
352 void InspectorDOMDebuggerAgent::removeXHRBreakpoint(ErrorString*, const String& url)
353 {
354     if (url.isEmpty()) {
355         m_inspectorState->setBoolean(DOMDebuggerAgentState::pauseOnAllXHRs, false);
356         return;
357     }
358
359     RefPtr<InspectorObject> xhrBreakpoints = m_inspectorState->getObject(DOMDebuggerAgentState::xhrBreakpoints);
360     xhrBreakpoints->remove(url);
361     m_inspectorState->setObject(DOMDebuggerAgentState::xhrBreakpoints, xhrBreakpoints);
362 }
363
364 void InspectorDOMDebuggerAgent::willSendXMLHttpRequest(const String& url)
365 {
366     String breakpointURL;
367     if (m_inspectorState->getBoolean(DOMDebuggerAgentState::pauseOnAllXHRs))
368         breakpointURL = "";
369     else {
370         RefPtr<InspectorObject> xhrBreakpoints = m_inspectorState->getObject(DOMDebuggerAgentState::xhrBreakpoints);
371         for (InspectorObject::iterator it = xhrBreakpoints->begin(); it != xhrBreakpoints->end(); ++it) {
372             if (url.contains(it->first)) {
373                 breakpointURL = it->first;
374                 break;
375             }
376         }
377     }
378
379     if (breakpointURL.isNull())
380         return;
381
382     RefPtr<InspectorObject> eventData = InspectorObject::create();
383     eventData->setString("breakpointURL", breakpointURL);
384     eventData->setString("url", url);
385     m_debuggerAgent->breakProgram(xhrNativeBreakpointType, eventData.release());
386 }
387
388 void InspectorDOMDebuggerAgent::clear()
389 {
390     m_domBreakpoints.clear();
391 }
392
393 } // namespace WebCore
394
395 #endif // ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)