123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- /*
- * Copyright (C) 2011 Google Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "config.h"
- #if ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)
- #include "InspectorDOMDebuggerAgent.h"
- #include "HTMLElement.h"
- #include "InspectorAgent.h"
- #include "InspectorDOMAgent.h"
- #include "InspectorDebuggerAgent.h"
- #include "InspectorFrontend.h"
- #include "InspectorInstrumentation.h"
- #include "InspectorState.h"
- #include "InspectorValues.h"
- #include "InstrumentingAgents.h"
- #include <wtf/text/WTFString.h>
- namespace {
- enum DOMBreakpointType {
- SubtreeModified = 0,
- AttributeModified,
- NodeRemoved,
- DOMBreakpointTypesCount
- };
- static const char* const listenerEventCategoryType = "listener:";
- static const char* const instrumentationEventCategoryType = "instrumentation:";
- const uint32_t inheritableDOMBreakpointTypesMask = (1 << SubtreeModified);
- const int domBreakpointDerivedTypeShift = 16;
- }
- namespace WebCore {
- namespace DOMDebuggerAgentState {
- static const char eventListenerBreakpoints[] = "eventListenerBreakpoints";
- static const char pauseOnAllXHRs[] = "pauseOnAllXHRs";
- static const char xhrBreakpoints[] = "xhrBreakpoints";
- }
- PassOwnPtr<InspectorDOMDebuggerAgent> InspectorDOMDebuggerAgent::create(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState, InspectorDOMAgent* domAgent, InspectorDebuggerAgent* debuggerAgent, InspectorAgent* inspectorAgent)
- {
- return adoptPtr(new InspectorDOMDebuggerAgent(instrumentingAgents, inspectorState, domAgent, debuggerAgent, inspectorAgent));
- }
- InspectorDOMDebuggerAgent::InspectorDOMDebuggerAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* inspectorState, InspectorDOMAgent* domAgent, InspectorDebuggerAgent* debuggerAgent, InspectorAgent*)
- : InspectorBaseAgent<InspectorDOMDebuggerAgent>("DOMDebugger", instrumentingAgents, inspectorState)
- , m_domAgent(domAgent)
- , m_debuggerAgent(debuggerAgent)
- , m_pauseInNextEventListener(false)
- {
- m_debuggerAgent->setListener(this);
- }
- InspectorDOMDebuggerAgent::~InspectorDOMDebuggerAgent()
- {
- ASSERT(!m_debuggerAgent);
- ASSERT(!m_instrumentingAgents->inspectorDOMDebuggerAgent());
- }
- // Browser debugger agent enabled only when JS debugger is enabled.
- void InspectorDOMDebuggerAgent::debuggerWasEnabled()
- {
- m_instrumentingAgents->setInspectorDOMDebuggerAgent(this);
- }
- void InspectorDOMDebuggerAgent::debuggerWasDisabled()
- {
- disable();
- }
- void InspectorDOMDebuggerAgent::stepInto()
- {
- m_pauseInNextEventListener = true;
- }
- void InspectorDOMDebuggerAgent::didPause()
- {
- m_pauseInNextEventListener = false;
- }
- void InspectorDOMDebuggerAgent::didProcessTask()
- {
- if (!m_pauseInNextEventListener)
- return;
- if (m_debuggerAgent && m_debuggerAgent->runningNestedMessageLoop())
- return;
- m_pauseInNextEventListener = false;
- }
- void InspectorDOMDebuggerAgent::disable()
- {
- m_instrumentingAgents->setInspectorDOMDebuggerAgent(0);
- clear();
- }
- void InspectorDOMDebuggerAgent::clearFrontend()
- {
- disable();
- }
- void InspectorDOMDebuggerAgent::discardAgent()
- {
- m_debuggerAgent->setListener(0);
- m_debuggerAgent = 0;
- }
- void InspectorDOMDebuggerAgent::discardBindings()
- {
- m_domBreakpoints.clear();
- }
- void InspectorDOMDebuggerAgent::setEventListenerBreakpoint(ErrorString* error, const String& eventName)
- {
- setBreakpoint(error, String(listenerEventCategoryType) + eventName);
- }
- void InspectorDOMDebuggerAgent::setInstrumentationBreakpoint(ErrorString* error, const String& eventName)
- {
- setBreakpoint(error, String(instrumentationEventCategoryType) + eventName);
- }
- void InspectorDOMDebuggerAgent::setBreakpoint(ErrorString* error, const String& eventName)
- {
- if (eventName.isEmpty()) {
- *error = "Event name is empty";
- return;
- }
- RefPtr<InspectorObject> eventListenerBreakpoints = m_state->getObject(DOMDebuggerAgentState::eventListenerBreakpoints);
- eventListenerBreakpoints->setBoolean(eventName, true);
- m_state->setObject(DOMDebuggerAgentState::eventListenerBreakpoints, eventListenerBreakpoints);
- }
- void InspectorDOMDebuggerAgent::removeEventListenerBreakpoint(ErrorString* error, const String& eventName)
- {
- removeBreakpoint(error, String(listenerEventCategoryType) + eventName);
- }
- void InspectorDOMDebuggerAgent::removeInstrumentationBreakpoint(ErrorString* error, const String& eventName)
- {
- removeBreakpoint(error, String(instrumentationEventCategoryType) + eventName);
- }
- void InspectorDOMDebuggerAgent::removeBreakpoint(ErrorString* error, const String& eventName)
- {
- if (eventName.isEmpty()) {
- *error = "Event name is empty";
- return;
- }
- RefPtr<InspectorObject> eventListenerBreakpoints = m_state->getObject(DOMDebuggerAgentState::eventListenerBreakpoints);
- eventListenerBreakpoints->remove(eventName);
- m_state->setObject(DOMDebuggerAgentState::eventListenerBreakpoints, eventListenerBreakpoints);
- }
- void InspectorDOMDebuggerAgent::didInvalidateStyleAttr(Node* node)
- {
- if (hasBreakpoint(node, AttributeModified)) {
- RefPtr<InspectorObject> eventData = InspectorObject::create();
- descriptionForDOMEvent(node, AttributeModified, false, eventData.get());
- m_debuggerAgent->breakProgram(InspectorFrontend::Debugger::Reason::DOM, eventData.release());
- }
- }
- void InspectorDOMDebuggerAgent::didInsertDOMNode(Node* node)
- {
- if (m_domBreakpoints.size()) {
- uint32_t mask = m_domBreakpoints.get(InspectorDOMAgent::innerParentNode(node));
- uint32_t inheritableTypesMask = (mask | (mask >> domBreakpointDerivedTypeShift)) & inheritableDOMBreakpointTypesMask;
- if (inheritableTypesMask)
- updateSubtreeBreakpoints(node, inheritableTypesMask, true);
- }
- }
- void InspectorDOMDebuggerAgent::didRemoveDOMNode(Node* node)
- {
- if (m_domBreakpoints.size()) {
- // Remove subtree breakpoints.
- m_domBreakpoints.remove(node);
- Vector<Node*> stack(1, InspectorDOMAgent::innerFirstChild(node));
- do {
- Node* node = stack.last();
- stack.removeLast();
- if (!node)
- continue;
- m_domBreakpoints.remove(node);
- stack.append(InspectorDOMAgent::innerFirstChild(node));
- stack.append(InspectorDOMAgent::innerNextSibling(node));
- } while (!stack.isEmpty());
- }
- }
- static int domTypeForName(ErrorString* errorString, const String& typeString)
- {
- if (typeString == "subtree-modified")
- return SubtreeModified;
- if (typeString == "attribute-modified")
- return AttributeModified;
- if (typeString == "node-removed")
- return NodeRemoved;
- *errorString = makeString("Unknown DOM breakpoint type: ", typeString);
- return -1;
- }
- static String domTypeName(int type)
- {
- switch (type) {
- case SubtreeModified: return "subtree-modified";
- case AttributeModified: return "attribute-modified";
- case NodeRemoved: return "node-removed";
- default: break;
- }
- return "";
- }
- void InspectorDOMDebuggerAgent::setDOMBreakpoint(ErrorString* errorString, int nodeId, const String& typeString)
- {
- Node* node = m_domAgent->assertNode(errorString, nodeId);
- if (!node)
- return;
- int type = domTypeForName(errorString, typeString);
- if (type == -1)
- return;
- uint32_t rootBit = 1 << type;
- m_domBreakpoints.set(node, m_domBreakpoints.get(node) | rootBit);
- if (rootBit & inheritableDOMBreakpointTypesMask) {
- for (Node* child = InspectorDOMAgent::innerFirstChild(node); child; child = InspectorDOMAgent::innerNextSibling(child))
- updateSubtreeBreakpoints(child, rootBit, true);
- }
- }
- void InspectorDOMDebuggerAgent::removeDOMBreakpoint(ErrorString* errorString, int nodeId, const String& typeString)
- {
- Node* node = m_domAgent->assertNode(errorString, nodeId);
- if (!node)
- return;
- int type = domTypeForName(errorString, typeString);
- if (type == -1)
- return;
- uint32_t rootBit = 1 << type;
- uint32_t mask = m_domBreakpoints.get(node) & ~rootBit;
- if (mask)
- m_domBreakpoints.set(node, mask);
- else
- m_domBreakpoints.remove(node);
- if ((rootBit & inheritableDOMBreakpointTypesMask) && !(mask & (rootBit << domBreakpointDerivedTypeShift))) {
- for (Node* child = InspectorDOMAgent::innerFirstChild(node); child; child = InspectorDOMAgent::innerNextSibling(child))
- updateSubtreeBreakpoints(child, rootBit, false);
- }
- }
- void InspectorDOMDebuggerAgent::willInsertDOMNode(Node* parent)
- {
- if (hasBreakpoint(parent, SubtreeModified)) {
- RefPtr<InspectorObject> eventData = InspectorObject::create();
- descriptionForDOMEvent(parent, SubtreeModified, true, eventData.get());
- m_debuggerAgent->breakProgram(InspectorFrontend::Debugger::Reason::DOM, eventData.release());
- }
- }
- void InspectorDOMDebuggerAgent::willRemoveDOMNode(Node* node)
- {
- Node* parentNode = InspectorDOMAgent::innerParentNode(node);
- if (hasBreakpoint(node, NodeRemoved)) {
- RefPtr<InspectorObject> eventData = InspectorObject::create();
- descriptionForDOMEvent(node, NodeRemoved, false, eventData.get());
- m_debuggerAgent->breakProgram(InspectorFrontend::Debugger::Reason::DOM, eventData.release());
- } else if (parentNode && hasBreakpoint(parentNode, SubtreeModified)) {
- RefPtr<InspectorObject> eventData = InspectorObject::create();
- descriptionForDOMEvent(node, SubtreeModified, false, eventData.get());
- m_debuggerAgent->breakProgram(InspectorFrontend::Debugger::Reason::DOM, eventData.release());
- }
- }
- void InspectorDOMDebuggerAgent::willModifyDOMAttr(Element* element)
- {
- if (hasBreakpoint(element, AttributeModified)) {
- RefPtr<InspectorObject> eventData = InspectorObject::create();
- descriptionForDOMEvent(element, AttributeModified, false, eventData.get());
- m_debuggerAgent->breakProgram(InspectorFrontend::Debugger::Reason::DOM, eventData.release());
- }
- }
- void InspectorDOMDebuggerAgent::descriptionForDOMEvent(Node* target, int breakpointType, bool insertion, InspectorObject* description)
- {
- ASSERT(hasBreakpoint(target, breakpointType));
- Node* breakpointOwner = target;
- if ((1 << breakpointType) & inheritableDOMBreakpointTypesMask) {
- // For inheritable breakpoint types, target node isn't always the same as the node that owns a breakpoint.
- // Target node may be unknown to frontend, so we need to push it first.
- RefPtr<TypeBuilder::Runtime::RemoteObject> targetNodeObject = m_domAgent->resolveNode(target, InspectorDebuggerAgent::backtraceObjectGroup);
- description->setValue("targetNode", targetNodeObject);
- // Find breakpoint owner node.
- if (!insertion)
- breakpointOwner = InspectorDOMAgent::innerParentNode(target);
- ASSERT(breakpointOwner);
- while (!(m_domBreakpoints.get(breakpointOwner) & (1 << breakpointType))) {
- Node* parentNode = InspectorDOMAgent::innerParentNode(breakpointOwner);
- if (!parentNode)
- break;
- breakpointOwner = parentNode;
- }
- if (breakpointType == SubtreeModified)
- description->setBoolean("insertion", insertion);
- }
- int breakpointOwnerNodeId = m_domAgent->boundNodeId(breakpointOwner);
- ASSERT(breakpointOwnerNodeId);
- description->setNumber("nodeId", breakpointOwnerNodeId);
- description->setString("type", domTypeName(breakpointType));
- }
- bool InspectorDOMDebuggerAgent::hasBreakpoint(Node* node, int type)
- {
- uint32_t rootBit = 1 << type;
- uint32_t derivedBit = rootBit << domBreakpointDerivedTypeShift;
- return m_domBreakpoints.get(node) & (rootBit | derivedBit);
- }
- void InspectorDOMDebuggerAgent::updateSubtreeBreakpoints(Node* node, uint32_t rootMask, bool set)
- {
- uint32_t oldMask = m_domBreakpoints.get(node);
- uint32_t derivedMask = rootMask << domBreakpointDerivedTypeShift;
- uint32_t newMask = set ? oldMask | derivedMask : oldMask & ~derivedMask;
- if (newMask)
- m_domBreakpoints.set(node, newMask);
- else
- m_domBreakpoints.remove(node);
- uint32_t newRootMask = rootMask & ~newMask;
- if (!newRootMask)
- return;
- for (Node* child = InspectorDOMAgent::innerFirstChild(node); child; child = InspectorDOMAgent::innerNextSibling(child))
- updateSubtreeBreakpoints(child, newRootMask, set);
- }
- void InspectorDOMDebuggerAgent::pauseOnNativeEventIfNeeded(bool isDOMEvent, const String& eventName, bool synchronous)
- {
- String fullEventName = (isDOMEvent ? listenerEventCategoryType : instrumentationEventCategoryType) + eventName;
- if (m_pauseInNextEventListener)
- m_pauseInNextEventListener = false;
- else {
- RefPtr<InspectorObject> eventListenerBreakpoints = m_state->getObject(DOMDebuggerAgentState::eventListenerBreakpoints);
- if (eventListenerBreakpoints->find(fullEventName) == eventListenerBreakpoints->end())
- return;
- }
- RefPtr<InspectorObject> eventData = InspectorObject::create();
- eventData->setString("eventName", fullEventName);
- if (synchronous)
- m_debuggerAgent->breakProgram(InspectorFrontend::Debugger::Reason::EventListener, eventData.release());
- else
- m_debuggerAgent->schedulePauseOnNextStatement(InspectorFrontend::Debugger::Reason::EventListener, eventData.release());
- }
- void InspectorDOMDebuggerAgent::setXHRBreakpoint(ErrorString*, const String& url)
- {
- if (url.isEmpty()) {
- m_state->setBoolean(DOMDebuggerAgentState::pauseOnAllXHRs, true);
- return;
- }
- RefPtr<InspectorObject> xhrBreakpoints = m_state->getObject(DOMDebuggerAgentState::xhrBreakpoints);
- xhrBreakpoints->setBoolean(url, true);
- m_state->setObject(DOMDebuggerAgentState::xhrBreakpoints, xhrBreakpoints);
- }
- void InspectorDOMDebuggerAgent::removeXHRBreakpoint(ErrorString*, const String& url)
- {
- if (url.isEmpty()) {
- m_state->setBoolean(DOMDebuggerAgentState::pauseOnAllXHRs, false);
- return;
- }
- RefPtr<InspectorObject> xhrBreakpoints = m_state->getObject(DOMDebuggerAgentState::xhrBreakpoints);
- xhrBreakpoints->remove(url);
- m_state->setObject(DOMDebuggerAgentState::xhrBreakpoints, xhrBreakpoints);
- }
- void InspectorDOMDebuggerAgent::willSendXMLHttpRequest(const String& url)
- {
- String breakpointURL;
- if (m_state->getBoolean(DOMDebuggerAgentState::pauseOnAllXHRs))
- breakpointURL = "";
- else {
- RefPtr<InspectorObject> xhrBreakpoints = m_state->getObject(DOMDebuggerAgentState::xhrBreakpoints);
- for (InspectorObject::iterator it = xhrBreakpoints->begin(); it != xhrBreakpoints->end(); ++it) {
- if (url.contains(it->key)) {
- breakpointURL = it->key;
- break;
- }
- }
- }
- if (breakpointURL.isNull())
- return;
- RefPtr<InspectorObject> eventData = InspectorObject::create();
- eventData->setString("breakpointURL", breakpointURL);
- eventData->setString("url", url);
- m_debuggerAgent->breakProgram(InspectorFrontend::Debugger::Reason::XHR, eventData.release());
- }
- void InspectorDOMDebuggerAgent::clear()
- {
- m_domBreakpoints.clear();
- m_pauseInNextEventListener = false;
- }
- } // namespace WebCore
- #endif // ENABLE(INSPECTOR) && ENABLE(JAVASCRIPT_DEBUGGER)
|