InspectorDOMAgent.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #ifndef InspectorDOMAgent_h
  30. #define InspectorDOMAgent_h
  31. #include "EventTarget.h"
  32. #include "InjectedScript.h"
  33. #include "InjectedScriptManager.h"
  34. #include "InspectorBaseAgent.h"
  35. #include "InspectorFrontend.h"
  36. #include "InspectorOverlay.h"
  37. #include "InspectorValues.h"
  38. #include "RenderLayer.h"
  39. #include "Timer.h"
  40. #include <wtf/Deque.h>
  41. #include <wtf/ListHashSet.h>
  42. #include <wtf/HashMap.h>
  43. #include <wtf/HashSet.h>
  44. #include <wtf/OwnPtr.h>
  45. #include <wtf/PassOwnPtr.h>
  46. #include <wtf/RefPtr.h>
  47. #include <wtf/Vector.h>
  48. #include <wtf/text/AtomicString.h>
  49. namespace WebCore {
  50. class ContainerNode;
  51. class CharacterData;
  52. class DOMEditor;
  53. class Document;
  54. class Element;
  55. class Event;
  56. class InspectorClient;
  57. class InspectorFrontend;
  58. class InspectorHistory;
  59. class InspectorOverlay;
  60. class InspectorPageAgent;
  61. class HitTestResult;
  62. class HTMLElement;
  63. class InspectorState;
  64. class InstrumentingAgents;
  65. class NameNodeMap;
  66. class Node;
  67. class RevalidateStyleAttributeTask;
  68. class ScriptValue;
  69. class ShadowRoot;
  70. struct HighlightConfig;
  71. typedef String ErrorString;
  72. typedef int BackendNodeId;
  73. #if ENABLE(INSPECTOR)
  74. struct EventListenerInfo {
  75. EventListenerInfo(Node* node, const AtomicString& eventType, const EventListenerVector& eventListenerVector)
  76. : node(node)
  77. , eventType(eventType)
  78. , eventListenerVector(eventListenerVector)
  79. {
  80. }
  81. Node* node;
  82. const AtomicString eventType;
  83. const EventListenerVector eventListenerVector;
  84. };
  85. class InspectorDOMAgent : public InspectorBaseAgent<InspectorDOMAgent>, public InspectorBackendDispatcher::DOMCommandHandler {
  86. WTF_MAKE_NONCOPYABLE(InspectorDOMAgent);
  87. public:
  88. struct DOMListener {
  89. virtual ~DOMListener()
  90. {
  91. }
  92. virtual void didRemoveDocument(Document*) = 0;
  93. virtual void didRemoveDOMNode(Node*) = 0;
  94. virtual void didModifyDOMAttr(Element*) = 0;
  95. };
  96. static PassOwnPtr<InspectorDOMAgent> create(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay, InspectorClient* client)
  97. {
  98. return adoptPtr(new InspectorDOMAgent(instrumentingAgents, pageAgent, inspectorState, injectedScriptManager, overlay, client));
  99. }
  100. static String toErrorString(const ExceptionCode&);
  101. ~InspectorDOMAgent();
  102. virtual void setFrontend(InspectorFrontend*);
  103. virtual void clearFrontend();
  104. virtual void restore();
  105. Vector<Document*> documents();
  106. void reset();
  107. // Methods called from the frontend for DOM nodes inspection.
  108. virtual void querySelector(ErrorString*, int nodeId, const String& selectors, int* elementId);
  109. virtual void querySelectorAll(ErrorString*, int nodeId, const String& selectors, RefPtr<TypeBuilder::Array<int> >& result);
  110. virtual void getDocument(ErrorString*, RefPtr<TypeBuilder::DOM::Node>& root);
  111. virtual void requestChildNodes(ErrorString*, int nodeId, const int* depth);
  112. virtual void setAttributeValue(ErrorString*, int elementId, const String& name, const String& value);
  113. virtual void setAttributesAsText(ErrorString*, int elementId, const String& text, const String* name);
  114. virtual void removeAttribute(ErrorString*, int elementId, const String& name);
  115. virtual void removeNode(ErrorString*, int nodeId);
  116. virtual void setNodeName(ErrorString*, int nodeId, const String& name, int* newId);
  117. virtual void getOuterHTML(ErrorString*, int nodeId, WTF::String* outerHTML);
  118. virtual void setOuterHTML(ErrorString*, int nodeId, const String& outerHTML);
  119. virtual void setNodeValue(ErrorString*, int nodeId, const String& value);
  120. virtual void getEventListenersForNode(ErrorString*, int nodeId, const WTF::String* objectGroup, RefPtr<TypeBuilder::Array<TypeBuilder::DOM::EventListener> >& listenersArray);
  121. virtual void performSearch(ErrorString*, const String& whitespaceTrimmedQuery, String* searchId, int* resultCount);
  122. virtual void getSearchResults(ErrorString*, const String& searchId, int fromIndex, int toIndex, RefPtr<TypeBuilder::Array<int> >&);
  123. virtual void discardSearchResults(ErrorString*, const String& searchId);
  124. virtual void resolveNode(ErrorString*, int nodeId, const String* objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result);
  125. virtual void getAttributes(ErrorString*, int nodeId, RefPtr<TypeBuilder::Array<String> >& result);
  126. virtual void setInspectModeEnabled(ErrorString*, bool enabled, const RefPtr<InspectorObject>* highlightConfig);
  127. virtual void requestNode(ErrorString*, const String& objectId, int* nodeId);
  128. virtual void pushNodeByPathToFrontend(ErrorString*, const String& path, int* nodeId);
  129. virtual void pushNodeByBackendIdToFrontend(ErrorString*, BackendNodeId, int* nodeId);
  130. virtual void releaseBackendNodeIds(ErrorString*, const String& nodeGroup);
  131. virtual void hideHighlight(ErrorString*);
  132. virtual void highlightRect(ErrorString*, int x, int y, int width, int height, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor, const bool* usePageCoordinates);
  133. virtual void highlightQuad(ErrorString*, const RefPtr<InspectorArray>& quad, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor, const bool* usePageCoordinates);
  134. virtual void highlightNode(ErrorString*, const RefPtr<InspectorObject>& highlightConfig, const int* nodeId, const String* objectId);
  135. virtual void highlightFrame(ErrorString*, const String& frameId, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor);
  136. virtual void moveTo(ErrorString*, int nodeId, int targetNodeId, const int* anchorNodeId, int* newNodeId);
  137. virtual void undo(ErrorString*);
  138. virtual void redo(ErrorString*);
  139. virtual void markUndoableState(ErrorString*);
  140. virtual void focus(ErrorString*, int nodeId);
  141. virtual void setFileInputFiles(ErrorString*, int nodeId, const RefPtr<InspectorArray>& files);
  142. void getEventListeners(Node*, Vector<EventListenerInfo>& listenersArray, bool includeAncestors);
  143. // Methods called from the InspectorInstrumentation.
  144. void setDocument(Document*);
  145. void releaseDanglingNodes();
  146. void mainFrameDOMContentLoaded();
  147. void loadEventFired(Document*);
  148. void didInsertDOMNode(Node*);
  149. void didRemoveDOMNode(Node*);
  150. void willModifyDOMAttr(Element*, const AtomicString& oldValue, const AtomicString& newValue);
  151. void didModifyDOMAttr(Element*, const AtomicString& name, const AtomicString& value);
  152. void didRemoveDOMAttr(Element*, const AtomicString& name);
  153. void styleAttributeInvalidated(const Vector<Element*>& elements);
  154. void characterDataModified(CharacterData*);
  155. void didInvalidateStyleAttr(Node*);
  156. void didPushShadowRoot(Element* host, ShadowRoot*);
  157. void willPopShadowRoot(Element* host, ShadowRoot*);
  158. void frameDocumentUpdated(Frame*);
  159. int pushNodeToFrontend(ErrorString*, int documentNodeId, Node*);
  160. Node* nodeForId(int nodeId);
  161. int boundNodeId(Node*);
  162. void setDOMListener(DOMListener*);
  163. BackendNodeId backendNodeIdForNode(Node*, const String& nodeGroup);
  164. static String documentURLString(Document*);
  165. PassRefPtr<TypeBuilder::Runtime::RemoteObject> resolveNode(Node*, const String& objectGroup);
  166. bool handleMousePress();
  167. bool handleTouchEvent(Node*);
  168. void mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags);
  169. void inspect(Node*);
  170. void focusNode();
  171. InspectorHistory* history() { return m_history.get(); }
  172. // We represent embedded doms as a part of the same hierarchy. Hence we treat children of frame owners differently.
  173. // We also skip whitespace text nodes conditionally. Following methods encapsulate these specifics.
  174. static Node* innerFirstChild(Node*);
  175. static Node* innerNextSibling(Node*);
  176. static Node* innerPreviousSibling(Node*);
  177. static unsigned innerChildNodeCount(Node*);
  178. static Node* innerParentNode(Node*);
  179. static bool isWhitespace(Node*);
  180. Node* assertNode(ErrorString*, int nodeId);
  181. Element* assertElement(ErrorString*, int nodeId);
  182. Document* assertDocument(ErrorString*, int nodeId);
  183. // Methods called from other agents.
  184. InspectorPageAgent* pageAgent() { return m_pageAgent; }
  185. private:
  186. InspectorDOMAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorCompositeState*, InjectedScriptManager*, InspectorOverlay*, InspectorClient*);
  187. void setSearchingForNode(ErrorString*, bool enabled, InspectorObject* highlightConfig);
  188. PassOwnPtr<HighlightConfig> highlightConfigFromInspectorObject(ErrorString*, InspectorObject* highlightInspectorObject);
  189. // Node-related methods.
  190. typedef HashMap<RefPtr<Node>, int> NodeToIdMap;
  191. int bind(Node*, NodeToIdMap*);
  192. void unbind(Node*, NodeToIdMap*);
  193. Node* assertEditableNode(ErrorString*, int nodeId);
  194. Element* assertEditableElement(ErrorString*, int nodeId);
  195. int pushNodePathToFrontend(Node*);
  196. void pushChildNodesToFrontend(int nodeId, int depth = 1);
  197. bool hasBreakpoint(Node*, int type);
  198. void updateSubtreeBreakpoints(Node* root, uint32_t rootMask, bool value);
  199. void descriptionForDOMEvent(Node* target, int breakpointType, bool insertion, PassRefPtr<InspectorObject> description);
  200. PassRefPtr<TypeBuilder::DOM::Node> buildObjectForNode(Node*, int depth, NodeToIdMap*);
  201. PassRefPtr<TypeBuilder::Array<String> > buildArrayForElementAttributes(Element*);
  202. PassRefPtr<TypeBuilder::Array<TypeBuilder::DOM::Node> > buildArrayForContainerChildren(Node* container, int depth, NodeToIdMap* nodesMap);
  203. PassRefPtr<TypeBuilder::DOM::EventListener> buildObjectForEventListener(const RegisteredEventListener&, const AtomicString& eventType, Node*, const String* objectGroupId);
  204. Node* nodeForPath(const String& path);
  205. void discardBindings();
  206. void innerHighlightQuad(PassOwnPtr<FloatQuad>, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor, const bool* usePageCoordinates);
  207. InspectorPageAgent* m_pageAgent;
  208. InjectedScriptManager* m_injectedScriptManager;
  209. InspectorOverlay* m_overlay;
  210. InspectorClient* m_client;
  211. InspectorFrontend::DOM* m_frontend;
  212. DOMListener* m_domListener;
  213. NodeToIdMap m_documentNodeToIdMap;
  214. typedef HashMap<RefPtr<Node>, BackendNodeId> NodeToBackendIdMap;
  215. HashMap<String, NodeToBackendIdMap> m_nodeGroupToBackendIdMap;
  216. // Owns node mappings for dangling nodes.
  217. Vector<OwnPtr<NodeToIdMap> > m_danglingNodeToIdMaps;
  218. HashMap<int, Node*> m_idToNode;
  219. HashMap<int, NodeToIdMap*> m_idToNodesMap;
  220. HashSet<int> m_childrenRequested;
  221. HashMap<BackendNodeId, std::pair<Node*, String> > m_backendIdToNode;
  222. int m_lastNodeId;
  223. BackendNodeId m_lastBackendNodeId;
  224. RefPtr<Document> m_document;
  225. typedef HashMap<String, Vector<RefPtr<Node> > > SearchResults;
  226. SearchResults m_searchResults;
  227. OwnPtr<RevalidateStyleAttributeTask> m_revalidateStyleAttrTask;
  228. RefPtr<Node> m_nodeToFocus;
  229. bool m_searchingForNode;
  230. OwnPtr<HighlightConfig> m_inspectModeHighlightConfig;
  231. OwnPtr<InspectorHistory> m_history;
  232. OwnPtr<DOMEditor> m_domEditor;
  233. bool m_suppressAttributeModifiedEvent;
  234. };
  235. #endif // ENABLE(INSPECTOR)
  236. } // namespace WebCore
  237. #endif // !defined(InspectorDOMAgent_h)