1/* 2 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * Copyright (C) 2011 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 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 15 * its contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#ifndef InspectorDOMAgent_h 31#define InspectorDOMAgent_h 32 33#include "EventTarget.h" 34#include "InjectedScript.h" 35#include "InjectedScriptManager.h" 36#include "InspectorBaseAgent.h" 37#include "InspectorFrontend.h" 38#include "InspectorOverlay.h" 39#include "InspectorValues.h" 40#include "RenderLayer.h" 41#include "Timer.h" 42 43#include <wtf/Deque.h> 44#include <wtf/ListHashSet.h> 45#include <wtf/HashMap.h> 46#include <wtf/HashSet.h> 47#include <wtf/OwnPtr.h> 48#include <wtf/PassOwnPtr.h> 49#include <wtf/RefPtr.h> 50#include <wtf/Vector.h> 51#include <wtf/text/AtomicString.h> 52 53namespace WebCore { 54class ContainerNode; 55class CharacterData; 56class DOMEditor; 57class Document; 58class Element; 59class Event; 60class InspectorClient; 61class InspectorFrontend; 62class InspectorHistory; 63class InspectorOverlay; 64class InspectorPageAgent; 65class HitTestResult; 66class HTMLElement; 67class InspectorState; 68class InstrumentingAgents; 69class NameNodeMap; 70class Node; 71class RevalidateStyleAttributeTask; 72class ScriptValue; 73class ShadowRoot; 74 75struct HighlightConfig; 76 77typedef String ErrorString; 78typedef int BackendNodeId; 79 80#if ENABLE(INSPECTOR) 81 82struct EventListenerInfo { 83 EventListenerInfo(Node* node, const AtomicString& eventType, const EventListenerVector& eventListenerVector) 84 : node(node) 85 , eventType(eventType) 86 , eventListenerVector(eventListenerVector) 87 { 88 } 89 90 Node* node; 91 const AtomicString eventType; 92 const EventListenerVector eventListenerVector; 93}; 94 95class InspectorDOMAgent : public InspectorBaseAgent<InspectorDOMAgent>, public InspectorBackendDispatcher::DOMCommandHandler { 96 WTF_MAKE_NONCOPYABLE(InspectorDOMAgent); 97public: 98 struct DOMListener { 99 virtual ~DOMListener() 100 { 101 } 102 virtual void didRemoveDocument(Document*) = 0; 103 virtual void didRemoveDOMNode(Node*) = 0; 104 virtual void didModifyDOMAttr(Element*) = 0; 105 }; 106 107 static PassOwnPtr<InspectorDOMAgent> create(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay, InspectorClient* client) 108 { 109 return adoptPtr(new InspectorDOMAgent(instrumentingAgents, pageAgent, inspectorState, injectedScriptManager, overlay, client)); 110 } 111 112 static String toErrorString(const ExceptionCode&); 113 114 ~InspectorDOMAgent(); 115 116 virtual void setFrontend(InspectorFrontend*); 117 virtual void clearFrontend(); 118 virtual void restore(); 119 120 Vector<Document*> documents(); 121 void reset(); 122 123 // Methods called from the frontend for DOM nodes inspection. 124 virtual void querySelector(ErrorString*, int nodeId, const String& selectors, int* elementId); 125 virtual void querySelectorAll(ErrorString*, int nodeId, const String& selectors, RefPtr<TypeBuilder::Array<int> >& result); 126 virtual void getDocument(ErrorString*, RefPtr<TypeBuilder::DOM::Node>& root); 127 virtual void requestChildNodes(ErrorString*, int nodeId, const int* depth); 128 virtual void setAttributeValue(ErrorString*, int elementId, const String& name, const String& value); 129 virtual void setAttributesAsText(ErrorString*, int elementId, const String& text, const String* name); 130 virtual void removeAttribute(ErrorString*, int elementId, const String& name); 131 virtual void removeNode(ErrorString*, int nodeId); 132 virtual void setNodeName(ErrorString*, int nodeId, const String& name, int* newId); 133 virtual void getOuterHTML(ErrorString*, int nodeId, WTF::String* outerHTML); 134 virtual void setOuterHTML(ErrorString*, int nodeId, const String& outerHTML); 135 virtual void setNodeValue(ErrorString*, int nodeId, const String& value); 136 virtual void getEventListenersForNode(ErrorString*, int nodeId, const WTF::String* objectGroup, RefPtr<TypeBuilder::Array<TypeBuilder::DOM::EventListener> >& listenersArray); 137 virtual void performSearch(ErrorString*, const String& whitespaceTrimmedQuery, String* searchId, int* resultCount); 138 virtual void getSearchResults(ErrorString*, const String& searchId, int fromIndex, int toIndex, RefPtr<TypeBuilder::Array<int> >&); 139 virtual void discardSearchResults(ErrorString*, const String& searchId); 140 virtual void resolveNode(ErrorString*, int nodeId, const String* objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result); 141 virtual void getAttributes(ErrorString*, int nodeId, RefPtr<TypeBuilder::Array<String> >& result); 142 virtual void setInspectModeEnabled(ErrorString*, bool enabled, const RefPtr<InspectorObject>* highlightConfig); 143 virtual void requestNode(ErrorString*, const String& objectId, int* nodeId); 144 virtual void pushNodeByPathToFrontend(ErrorString*, const String& path, int* nodeId); 145 virtual void pushNodeByBackendIdToFrontend(ErrorString*, BackendNodeId, int* nodeId); 146 virtual void releaseBackendNodeIds(ErrorString*, const String& nodeGroup); 147 virtual void hideHighlight(ErrorString*); 148 virtual void highlightRect(ErrorString*, int x, int y, int width, int height, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor, const bool* usePageCoordinates); 149 virtual void highlightQuad(ErrorString*, const RefPtr<InspectorArray>& quad, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor, const bool* usePageCoordinates); 150 virtual void highlightNode(ErrorString*, const RefPtr<InspectorObject>& highlightConfig, const int* nodeId, const String* objectId); 151 virtual void highlightFrame(ErrorString*, const String& frameId, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor); 152 153 virtual void moveTo(ErrorString*, int nodeId, int targetNodeId, const int* anchorNodeId, int* newNodeId); 154 virtual void undo(ErrorString*); 155 virtual void redo(ErrorString*); 156 virtual void markUndoableState(ErrorString*); 157 virtual void focus(ErrorString*, int nodeId); 158 virtual void setFileInputFiles(ErrorString*, int nodeId, const RefPtr<InspectorArray>& files); 159 160 void getEventListeners(Node*, Vector<EventListenerInfo>& listenersArray, bool includeAncestors); 161 162 // Methods called from the InspectorInstrumentation. 163 void setDocument(Document*); 164 void releaseDanglingNodes(); 165 166 void mainFrameDOMContentLoaded(); 167 void loadEventFired(Document*); 168 169 void didInsertDOMNode(Node*); 170 void didRemoveDOMNode(Node*); 171 void willModifyDOMAttr(Element*, const AtomicString& oldValue, const AtomicString& newValue); 172 void didModifyDOMAttr(Element*, const AtomicString& name, const AtomicString& value); 173 void didRemoveDOMAttr(Element*, const AtomicString& name); 174 void styleAttributeInvalidated(const Vector<Element*>& elements); 175 void characterDataModified(CharacterData*); 176 void didInvalidateStyleAttr(Node*); 177 void didPushShadowRoot(Element* host, ShadowRoot*); 178 void willPopShadowRoot(Element* host, ShadowRoot*); 179 void frameDocumentUpdated(Frame*); 180 181 int pushNodeToFrontend(ErrorString*, int documentNodeId, Node*); 182 Node* nodeForId(int nodeId); 183 int boundNodeId(Node*); 184 void setDOMListener(DOMListener*); 185 BackendNodeId backendNodeIdForNode(Node*, const String& nodeGroup); 186 187 static String documentURLString(Document*); 188 189 PassRefPtr<TypeBuilder::Runtime::RemoteObject> resolveNode(Node*, const String& objectGroup); 190 bool handleMousePress(); 191 bool handleTouchEvent(Node*); 192 void mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags); 193 void inspect(Node*); 194 void focusNode(); 195 196 InspectorHistory* history() { return m_history.get(); } 197 198 // We represent embedded doms as a part of the same hierarchy. Hence we treat children of frame owners differently. 199 // We also skip whitespace text nodes conditionally. Following methods encapsulate these specifics. 200 static Node* innerFirstChild(Node*); 201 static Node* innerNextSibling(Node*); 202 static Node* innerPreviousSibling(Node*); 203 static unsigned innerChildNodeCount(Node*); 204 static Node* innerParentNode(Node*); 205 static bool isWhitespace(Node*); 206 207 Node* assertNode(ErrorString*, int nodeId); 208 Element* assertElement(ErrorString*, int nodeId); 209 Document* assertDocument(ErrorString*, int nodeId); 210 211 // Methods called from other agents. 212 InspectorPageAgent* pageAgent() { return m_pageAgent; } 213 214private: 215 InspectorDOMAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorCompositeState*, InjectedScriptManager*, InspectorOverlay*, InspectorClient*); 216 217 void setSearchingForNode(ErrorString*, bool enabled, InspectorObject* highlightConfig); 218 PassOwnPtr<HighlightConfig> highlightConfigFromInspectorObject(ErrorString*, InspectorObject* highlightInspectorObject); 219 220 // Node-related methods. 221 typedef HashMap<RefPtr<Node>, int> NodeToIdMap; 222 int bind(Node*, NodeToIdMap*); 223 void unbind(Node*, NodeToIdMap*); 224 225 Node* assertEditableNode(ErrorString*, int nodeId); 226 Element* assertEditableElement(ErrorString*, int nodeId); 227 228 int pushNodePathToFrontend(Node*); 229 void pushChildNodesToFrontend(int nodeId, int depth = 1); 230 231 bool hasBreakpoint(Node*, int type); 232 void updateSubtreeBreakpoints(Node* root, uint32_t rootMask, bool value); 233 void descriptionForDOMEvent(Node* target, int breakpointType, bool insertion, PassRefPtr<InspectorObject> description); 234 235 PassRefPtr<TypeBuilder::DOM::Node> buildObjectForNode(Node*, int depth, NodeToIdMap*); 236 PassRefPtr<TypeBuilder::Array<String> > buildArrayForElementAttributes(Element*); 237 PassRefPtr<TypeBuilder::Array<TypeBuilder::DOM::Node> > buildArrayForContainerChildren(Node* container, int depth, NodeToIdMap* nodesMap); 238 PassRefPtr<TypeBuilder::DOM::EventListener> buildObjectForEventListener(const RegisteredEventListener&, const AtomicString& eventType, Node*, const String* objectGroupId); 239 240 Node* nodeForPath(const String& path); 241 242 void discardBindings(); 243 244 void innerHighlightQuad(PassOwnPtr<FloatQuad>, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor, const bool* usePageCoordinates); 245 246 InspectorPageAgent* m_pageAgent; 247 InjectedScriptManager* m_injectedScriptManager; 248 InspectorOverlay* m_overlay; 249 InspectorClient* m_client; 250 InspectorFrontend::DOM* m_frontend; 251 DOMListener* m_domListener; 252 NodeToIdMap m_documentNodeToIdMap; 253 typedef HashMap<RefPtr<Node>, BackendNodeId> NodeToBackendIdMap; 254 HashMap<String, NodeToBackendIdMap> m_nodeGroupToBackendIdMap; 255 // Owns node mappings for dangling nodes. 256 Vector<OwnPtr<NodeToIdMap> > m_danglingNodeToIdMaps; 257 HashMap<int, Node*> m_idToNode; 258 HashMap<int, NodeToIdMap*> m_idToNodesMap; 259 HashSet<int> m_childrenRequested; 260 HashMap<BackendNodeId, std::pair<Node*, String> > m_backendIdToNode; 261 int m_lastNodeId; 262 BackendNodeId m_lastBackendNodeId; 263 RefPtr<Document> m_document; 264 typedef HashMap<String, Vector<RefPtr<Node> > > SearchResults; 265 SearchResults m_searchResults; 266 OwnPtr<RevalidateStyleAttributeTask> m_revalidateStyleAttrTask; 267 RefPtr<Node> m_nodeToFocus; 268 bool m_searchingForNode; 269 OwnPtr<HighlightConfig> m_inspectModeHighlightConfig; 270 OwnPtr<InspectorHistory> m_history; 271 OwnPtr<DOMEditor> m_domEditor; 272 bool m_suppressAttributeModifiedEvent; 273}; 274 275#endif // ENABLE(INSPECTOR) 276 277} // namespace WebCore 278 279#endif // !defined(InspectorDOMAgent_h) 280