2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2011 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #ifndef InspectorDOMAgent_h
31 #define InspectorDOMAgent_h
33 #include "core/InspectorFrontend.h"
34 #include "core/inspector/InjectedScript.h"
35 #include "core/inspector/InjectedScriptManager.h"
36 #include "core/inspector/InspectorBaseAgent.h"
37 #include "core/rendering/RenderLayer.h"
38 #include "platform/JSONValues.h"
40 #include "wtf/HashMap.h"
41 #include "wtf/HashSet.h"
42 #include "wtf/OwnPtr.h"
43 #include "wtf/PassOwnPtr.h"
44 #include "wtf/RefPtr.h"
45 #include "wtf/Vector.h"
46 #include "wtf/text/AtomicString.h"
56 class InspectorFrontend;
57 class InspectorHistory;
58 class InspectorOverlay;
59 class InspectorPageAgent;
61 class PlatformGestureEvent;
62 class PlatformTouchEvent;
63 class RevalidateStyleAttributeTask;
66 struct HighlightConfig;
68 typedef String ErrorString;
71 struct EventListenerInfo {
72 EventListenerInfo(EventTarget* eventTarget, const AtomicString& eventType, const EventListenerVector& eventListenerVector)
73 : eventTarget(eventTarget)
74 , eventType(eventType)
75 , eventListenerVector(eventListenerVector)
79 EventTarget* eventTarget;
80 const AtomicString eventType;
81 const EventListenerVector eventListenerVector;
84 class InspectorDOMAgent final : public InspectorBaseAgent<InspectorDOMAgent>, public InspectorBackendDispatcher::DOMCommandHandler {
85 WTF_MAKE_NONCOPYABLE(InspectorDOMAgent);
87 struct DOMListener : public WillBeGarbageCollectedMixin {
88 virtual ~DOMListener()
91 virtual void didRemoveDocument(Document*) = 0;
92 virtual void didRemoveDOMNode(Node*) = 0;
93 virtual void didModifyDOMAttr(Element*) = 0;
96 static PassOwnPtrWillBeRawPtr<InspectorDOMAgent> create(InspectorPageAgent* pageAgent, InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay)
98 return adoptPtrWillBeNoop(new InspectorDOMAgent(pageAgent, injectedScriptManager, overlay));
101 static String toErrorString(ExceptionState&);
103 virtual ~InspectorDOMAgent();
104 virtual void trace(Visitor*) override;
106 virtual void setFrontend(InspectorFrontend*) override;
107 virtual void clearFrontend() override;
108 virtual void restore() override;
110 WillBeHeapVector<RawPtrWillBeMember<Document> > documents();
113 // Methods called from the frontend for DOM nodes inspection.
114 virtual void enable(ErrorString*) override;
115 virtual void disable(ErrorString*) override;
116 virtual void querySelector(ErrorString*, int nodeId, const String& selectors, int* elementId) override;
117 virtual void querySelectorAll(ErrorString*, int nodeId, const String& selectors, RefPtr<TypeBuilder::Array<int> >& result) override;
118 virtual void getDocument(ErrorString*, RefPtr<TypeBuilder::DOM::Node>& root) override;
119 virtual void requestChildNodes(ErrorString*, int nodeId, const int* depth) override;
120 virtual void setAttributeValue(ErrorString*, int elementId, const String& name, const String& value) override;
121 virtual void setAttributesAsText(ErrorString*, int elementId, const String& text, const String* name) override;
122 virtual void removeAttribute(ErrorString*, int elementId, const String& name) override;
123 virtual void removeNode(ErrorString*, int nodeId) override;
124 virtual void setNodeName(ErrorString*, int nodeId, const String& name, int* newId) override;
125 virtual void getOuterHTML(ErrorString*, int nodeId, WTF::String* outerHTML) override;
126 virtual void setOuterHTML(ErrorString*, int nodeId, const String& outerHTML) override;
127 virtual void setNodeValue(ErrorString*, int nodeId, const String& value) override;
128 virtual void getEventListenersForNode(ErrorString*, int nodeId, const WTF::String* objectGroup, RefPtr<TypeBuilder::Array<TypeBuilder::DOM::EventListener> >& listenersArray) override;
129 virtual void performSearch(ErrorString*, const String& whitespaceTrimmedQuery, const bool* includeUserAgentShadowDOM, String* searchId, int* resultCount) override;
130 virtual void getSearchResults(ErrorString*, const String& searchId, int fromIndex, int toIndex, RefPtr<TypeBuilder::Array<int> >&) override;
131 virtual void discardSearchResults(ErrorString*, const String& searchId) override;
132 virtual void resolveNode(ErrorString*, int nodeId, const String* objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result) override;
133 virtual void getAttributes(ErrorString*, int nodeId, RefPtr<TypeBuilder::Array<String> >& result) override;
134 virtual void setInspectModeEnabled(ErrorString*, bool enabled, const bool* inspectUAShadowDOM, const RefPtr<JSONObject>* highlightConfig) override;
135 virtual void requestNode(ErrorString*, const String& objectId, int* nodeId) override;
136 virtual void pushNodeByPathToFrontend(ErrorString*, const String& path, int* nodeId) override;
137 virtual void pushNodesByBackendIdsToFrontend(ErrorString*, const RefPtr<JSONArray>& nodeIds, RefPtr<TypeBuilder::Array<int> >&) override;
138 virtual void hideHighlight(ErrorString*) override;
139 virtual void highlightRect(ErrorString*, int x, int y, int width, int height, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor) override;
140 virtual void highlightQuad(ErrorString*, const RefPtr<JSONArray>& quad, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor) override;
141 virtual void highlightNode(ErrorString*, const RefPtr<JSONObject>& highlightConfig, const int* nodeId, const String* objectId) override;
142 virtual void highlightFrame(ErrorString*, const String& frameId, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor) override;
144 virtual void copyTo(ErrorString*, int nodeId, int targetElementId, const int* anchorNodeId, int* newNodeId) override;
145 virtual void moveTo(ErrorString*, int nodeId, int targetNodeId, const int* anchorNodeId, int* newNodeId) override;
146 virtual void undo(ErrorString*) override;
147 virtual void redo(ErrorString*) override;
148 virtual void markUndoableState(ErrorString*) override;
149 virtual void focus(ErrorString*, int nodeId) override;
150 virtual void setFileInputFiles(ErrorString*, int nodeId, const RefPtr<JSONArray>& files) override;
151 virtual void getBoxModel(ErrorString*, int nodeId, RefPtr<TypeBuilder::DOM::BoxModel>&) override;
152 virtual void getNodeForLocation(ErrorString*, int x, int y, int* nodeId) override;
153 virtual void getRelayoutBoundary(ErrorString*, int nodeId, int* relayoutBoundaryNodeId) override;
154 static void getEventListeners(EventTarget*, Vector<EventListenerInfo>& listenersArray, bool includeAncestors);
156 class Listener : public WillBeGarbageCollectedMixin {
158 virtual ~Listener() { }
159 virtual void domAgentWasEnabled() = 0;
160 virtual void domAgentWasDisabled() = 0;
162 void setListener(Listener* listener) { m_listener = listener; }
164 bool enabled() const;
166 // Methods called from the InspectorInstrumentation.
167 void setDocument(Document*);
168 void releaseDanglingNodes();
170 void domContentLoadedEventFired(LocalFrame*);
171 void didCommitLoad(LocalFrame*, DocumentLoader*);
173 void didInsertDOMNode(Node*);
174 void willRemoveDOMNode(Node*);
175 void willModifyDOMAttr(Element*, const AtomicString& oldValue, const AtomicString& newValue);
176 void didModifyDOMAttr(Element*, const String& name, const AtomicString& value);
177 void didRemoveDOMAttr(Element*, const String& name);
178 void styleAttributeInvalidated(const WillBeHeapVector<RawPtrWillBeMember<Element> >& elements);
179 void characterDataModified(CharacterData*);
180 void didInvalidateStyleAttr(Node*);
181 void didPushShadowRoot(Element* host, ShadowRoot*);
182 void willPopShadowRoot(Element* host, ShadowRoot*);
183 void frameDocumentUpdated(LocalFrame*);
184 void pseudoElementCreated(PseudoElement*);
185 void pseudoElementDestroyed(PseudoElement*);
187 Node* nodeForId(int nodeId);
188 int boundNodeId(Node*);
189 void setDOMListener(DOMListener*);
191 static String documentURLString(Document*);
193 PassRefPtr<TypeBuilder::Runtime::RemoteObject> resolveNode(Node*, const String& objectGroup);
194 bool handleMousePress();
195 bool handleGestureEvent(LocalFrame*, const PlatformGestureEvent&);
196 bool handleTouchEvent(LocalFrame*, const PlatformTouchEvent&);
197 bool handleMouseMove(LocalFrame*, const PlatformMouseEvent&);
199 InspectorHistory* history() { return m_history.get(); }
201 // We represent embedded doms as a part of the same hierarchy. Hence we treat children of frame owners differently.
202 // We also skip whitespace text nodes conditionally. Following methods encapsulate these specifics.
203 static Node* innerFirstChild(Node*);
204 static Node* innerNextSibling(Node*);
205 static Node* innerPreviousSibling(Node*);
206 static unsigned innerChildNodeCount(Node*);
207 static Node* innerParentNode(Node*);
208 static bool isWhitespace(Node*);
210 Node* assertNode(ErrorString*, int nodeId);
211 Element* assertElement(ErrorString*, int nodeId);
212 Document* assertDocument(ErrorString*, int nodeId);
215 enum SearchMode { NotSearching, SearchingForNormal, SearchingForUAShadow };
217 InspectorDOMAgent(InspectorPageAgent*, InjectedScriptManager*, InspectorOverlay*);
221 void setSearchingForNode(ErrorString*, SearchMode, JSONObject* highlightConfig);
222 PassOwnPtr<HighlightConfig> highlightConfigFromInspectorObject(ErrorString*, JSONObject* highlightInspectorObject);
224 // Node-related methods.
225 typedef WillBeHeapHashMap<RefPtrWillBeMember<Node>, int> NodeToIdMap;
226 int bind(Node*, NodeToIdMap*);
227 void unbind(Node*, NodeToIdMap*);
229 Node* assertEditableNode(ErrorString*, int nodeId);
230 Node* assertEditableChildNode(ErrorString*, Element* parentElement, int nodeId);
231 Element* assertEditableElement(ErrorString*, int nodeId);
235 int pushNodePathToFrontend(Node*);
236 void pushChildNodesToFrontend(int nodeId, int depth = 1);
238 void invalidateFrameOwnerElement(LocalFrame*);
240 PassRefPtr<TypeBuilder::DOM::Node> buildObjectForNode(Node*, int depth, NodeToIdMap*);
241 PassRefPtr<TypeBuilder::Array<String> > buildArrayForElementAttributes(Element*);
242 PassRefPtr<TypeBuilder::Array<TypeBuilder::DOM::Node> > buildArrayForContainerChildren(Node* container, int depth, NodeToIdMap* nodesMap);
243 PassRefPtr<TypeBuilder::DOM::EventListener> buildObjectForEventListener(const RegisteredEventListener&, const AtomicString& eventType, Node*, const String* objectGroupId);
244 PassRefPtr<TypeBuilder::Array<TypeBuilder::DOM::Node> > buildArrayForPseudoElements(Element*, NodeToIdMap* nodesMap);
246 Node* nodeForPath(const String& path);
248 void discardFrontendBindings();
250 void innerHighlightQuad(PassOwnPtr<FloatQuad>, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor);
252 bool pushDocumentUponHandlelessOperation(ErrorString*);
254 RawPtrWillBeMember<InspectorPageAgent> m_pageAgent;
255 RawPtrWillBeMember<InjectedScriptManager> m_injectedScriptManager;
256 InspectorOverlay* m_overlay;
257 InspectorFrontend::DOM* m_frontend;
258 RawPtrWillBeMember<DOMListener> m_domListener;
259 OwnPtrWillBeMember<NodeToIdMap> m_documentNodeToIdMap;
260 // Owns node mappings for dangling nodes.
261 WillBeHeapVector<OwnPtrWillBeMember<NodeToIdMap> > m_danglingNodeToIdMaps;
262 WillBeHeapHashMap<int, RawPtrWillBeMember<Node> > m_idToNode;
263 WillBeHeapHashMap<int, RawPtrWillBeMember<NodeToIdMap> > m_idToNodesMap;
264 HashSet<int> m_childrenRequested;
265 HashMap<int, int> m_cachedChildCount;
267 RefPtrWillBeMember<Document> m_document;
268 typedef WillBeHeapHashMap<String, WillBeHeapVector<RefPtrWillBeMember<Node> > > SearchResults;
269 SearchResults m_searchResults;
270 OwnPtrWillBeMember<RevalidateStyleAttributeTask> m_revalidateStyleAttrTask;
271 SearchMode m_searchingForNode;
272 OwnPtr<HighlightConfig> m_inspectModeHighlightConfig;
273 OwnPtrWillBeMember<InspectorHistory> m_history;
274 OwnPtrWillBeMember<DOMEditor> m_domEditor;
275 bool m_suppressAttributeModifiedEvent;
276 RawPtrWillBeMember<Listener> m_listener;
282 #endif // !defined(InspectorDOMAgent_h)