2 * Copyright (C) 2009, 2015-2016 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 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.
32 #include "EventTarget.h"
33 #include "InspectorWebAgentBase.h"
34 #include <inspector/InspectorBackendDispatchers.h>
35 #include <inspector/InspectorFrontendDispatchers.h>
36 #include <inspector/InspectorValues.h>
37 #include <wtf/HashMap.h>
38 #include <wtf/HashSet.h>
39 #include <wtf/RefPtr.h>
40 #include <wtf/Vector.h>
41 #include <wtf/text/AtomicString.h>
44 class InjectedScriptManager;
54 class AccessibilityObject;
63 class InspectorHistory;
64 class InspectorOverlay;
65 class InspectorPageAgent;
69 class RevalidateStyleAttributeTask;
72 struct HighlightConfig;
74 typedef String ErrorString;
75 typedef int BackendNodeId;
77 struct EventListenerInfo {
78 EventListenerInfo(Node* node, const AtomicString& eventType, EventListenerVector&& eventListenerVector)
80 , eventType(eventType)
81 , eventListenerVector(WTFMove(eventListenerVector))
86 const AtomicString eventType;
87 const EventListenerVector eventListenerVector;
90 class InspectorDOMAgent final : public InspectorAgentBase, public Inspector::DOMBackendDispatcherHandler {
91 WTF_MAKE_NONCOPYABLE(InspectorDOMAgent);
92 WTF_MAKE_FAST_ALLOCATED;
95 virtual ~DOMListener() = default;
96 virtual void didRemoveDOMNode(Node&, int nodeId) = 0;
97 virtual void didModifyDOMAttr(Element&) = 0;
100 InspectorDOMAgent(WebAgentContext&, InspectorPageAgent*, InspectorOverlay*);
101 virtual ~InspectorDOMAgent();
103 static String toErrorString(ExceptionCode);
104 static String toErrorString(Exception&&);
106 void didCreateFrontendAndBackend(Inspector::FrontendRouter*, Inspector::BackendDispatcher*) override;
107 void willDestroyFrontendAndBackend(Inspector::DisconnectReason) override;
109 Vector<Document*> documents();
112 // Methods called from the frontend for DOM nodes inspection.
113 void querySelector(ErrorString&, int nodeId, const String& selectors, int* elementId) override;
114 void querySelectorAll(ErrorString&, int nodeId, const String& selectors, RefPtr<Inspector::Protocol::Array<int>>& result) override;
115 void getDocument(ErrorString&, RefPtr<Inspector::Protocol::DOM::Node>& root) override;
116 void requestChildNodes(ErrorString&, int nodeId, const int* const depth) override;
117 void setAttributeValue(ErrorString&, int elementId, const String& name, const String& value) override;
118 void setAttributesAsText(ErrorString&, int elementId, const String& text, const String* const name) override;
119 void removeAttribute(ErrorString&, int elementId, const String& name) override;
120 void removeNode(ErrorString&, int nodeId) override;
121 void setNodeName(ErrorString&, int nodeId, const String& name, int* newId) override;
122 void getOuterHTML(ErrorString&, int nodeId, WTF::String* outerHTML) override;
123 void setOuterHTML(ErrorString&, int nodeId, const String& outerHTML) override;
124 void setNodeValue(ErrorString&, int nodeId, const String& value) override;
125 void getEventListenersForNode(ErrorString&, int nodeId, const WTF::String* const objectGroup, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::DOM::EventListener>>& listenersArray) override;
126 void setEventListenerDisabled(ErrorString&, int eventListenerId, bool disabled) override;
127 void getAccessibilityPropertiesForNode(ErrorString&, int nodeId, RefPtr<Inspector::Protocol::DOM::AccessibilityProperties>& axProperties) override;
128 void performSearch(ErrorString&, const String& whitespaceTrimmedQuery, const Inspector::InspectorArray* nodeIds, String* searchId, int* resultCount) override;
129 void getSearchResults(ErrorString&, const String& searchId, int fromIndex, int toIndex, RefPtr<Inspector::Protocol::Array<int>>&) override;
130 void discardSearchResults(ErrorString&, const String& searchId) override;
131 void resolveNode(ErrorString&, int nodeId, const String* const objectGroup, RefPtr<Inspector::Protocol::Runtime::RemoteObject>& result) override;
132 void getAttributes(ErrorString&, int nodeId, RefPtr<Inspector::Protocol::Array<String>>& result) override;
133 void setInspectModeEnabled(ErrorString&, bool enabled, const Inspector::InspectorObject* highlightConfig) override;
134 void requestNode(ErrorString&, const String& objectId, int* nodeId) override;
135 void pushNodeByPathToFrontend(ErrorString&, const String& path, int* nodeId) override;
136 void pushNodeByBackendIdToFrontend(ErrorString&, BackendNodeId, int* nodeId) override;
137 void releaseBackendNodeIds(ErrorString&, const String& nodeGroup) override;
138 void hideHighlight(ErrorString&) override;
139 void highlightRect(ErrorString&, int x, int y, int width, int height, const Inspector::InspectorObject* color, const Inspector::InspectorObject* outlineColor, const bool* const usePageCoordinates) override;
140 void highlightQuad(ErrorString&, const Inspector::InspectorArray& quad, const Inspector::InspectorObject* color, const Inspector::InspectorObject* outlineColor, const bool* const usePageCoordinates) override;
141 void highlightSelector(ErrorString&, const Inspector::InspectorObject& highlightConfig, const String& selectorString, const String* const frameId) override;
142 void highlightNode(ErrorString&, const Inspector::InspectorObject& highlightConfig, const int* const nodeId, const String* const objectId) override;
143 void highlightNodeList(ErrorString&, const Inspector::InspectorArray& nodeIds, const Inspector::InspectorObject& highlightConfig) override;
144 void highlightFrame(ErrorString&, const String& frameId, const Inspector::InspectorObject* color, const Inspector::InspectorObject* outlineColor) override;
145 void moveTo(ErrorString&, int nodeId, int targetNodeId, const int* const anchorNodeId, int* newNodeId) override;
146 void undo(ErrorString&) override;
147 void redo(ErrorString&) override;
148 void markUndoableState(ErrorString&) override;
149 void focus(ErrorString&, int nodeId) override;
150 void setInspectedNode(ErrorString&, int nodeId) override;
152 void getEventListeners(Node*, Vector<EventListenerInfo>& listenersArray, bool includeAncestors);
155 // InspectorInstrumentation
156 void didInsertDOMNode(Node&);
157 void didRemoveDOMNode(Node&);
158 void willModifyDOMAttr(Element&, const AtomicString& oldValue, const AtomicString& newValue);
159 void didModifyDOMAttr(Element&, const AtomicString& name, const AtomicString& value);
160 void didRemoveDOMAttr(Element&, const AtomicString& name);
161 void characterDataModified(CharacterData&);
162 void didInvalidateStyleAttr(Node&);
163 void didPushShadowRoot(Element& host, ShadowRoot&);
164 void willPopShadowRoot(Element& host, ShadowRoot&);
165 void didChangeCustomElementState(Element&);
166 bool handleTouchEvent(Node&);
167 void didCommitLoad(Document*);
168 void frameDocumentUpdated(Frame&);
169 void pseudoElementCreated(PseudoElement&);
170 void pseudoElementDestroyed(PseudoElement&);
171 void didAddEventListener(EventTarget&);
172 void willRemoveEventListener(EventTarget&, const AtomicString& eventType, EventListener&, bool capture);
173 bool isEventListenerDisabled(EventTarget&, const AtomicString& eventType, EventListener&, bool capture);
175 // Callbacks that don't directly correspond to an instrumentation entry point.
176 void setDocument(Document*);
177 void releaseDanglingNodes();
178 void mainFrameDOMContentLoaded();
180 void styleAttributeInvalidated(const Vector<Element*>& elements);
182 int pushNodeToFrontend(ErrorString&, int documentNodeId, Node*);
183 Node* nodeForId(int nodeId);
184 int boundNodeId(const Node*);
185 void setDOMListener(DOMListener*);
186 BackendNodeId backendNodeIdForNode(Node*, const String& nodeGroup);
188 static String documentURLString(Document*);
190 RefPtr<Inspector::Protocol::Runtime::RemoteObject> resolveNode(Node*, const String& objectGroup);
191 bool handleMousePress();
192 void mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags);
196 InspectorHistory* history() { return m_history.get(); }
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*);
207 Node* assertNode(ErrorString&, int nodeId);
208 Element* assertElement(ErrorString&, int nodeId);
209 Document* assertDocument(ErrorString&, int nodeId);
211 static Node* scriptValueAsNode(JSC::JSValue);
212 static JSC::JSValue nodeAsScriptValue(JSC::ExecState&, Node*);
214 // Methods called from other agents.
215 InspectorPageAgent* pageAgent() { return m_pageAgent; }
218 void highlightMousedOverNode();
219 void setSearchingForNode(ErrorString&, bool enabled, const Inspector::InspectorObject* highlightConfig);
220 std::unique_ptr<HighlightConfig> highlightConfigFromInspectorObject(ErrorString&, const Inspector::InspectorObject* highlightInspectorObject);
222 // Node-related methods.
223 typedef HashMap<RefPtr<Node>, int> NodeToIdMap;
224 int bind(Node*, NodeToIdMap*);
225 void unbind(Node*, NodeToIdMap*);
227 Node* assertEditableNode(ErrorString&, int nodeId);
228 Element* assertEditableElement(ErrorString&, int nodeId);
230 int pushNodePathToFrontend(Node*);
231 void pushChildNodesToFrontend(int nodeId, int depth = 1);
233 Ref<Inspector::Protocol::DOM::Node> buildObjectForNode(Node*, int depth, NodeToIdMap*);
234 Ref<Inspector::Protocol::Array<String>> buildArrayForElementAttributes(Element*);
235 Ref<Inspector::Protocol::Array<Inspector::Protocol::DOM::Node>> buildArrayForContainerChildren(Node* container, int depth, NodeToIdMap* nodesMap);
236 RefPtr<Inspector::Protocol::Array<Inspector::Protocol::DOM::Node>> buildArrayForPseudoElements(const Element&, NodeToIdMap* nodesMap);
237 Ref<Inspector::Protocol::DOM::EventListener> buildObjectForEventListener(const RegisteredEventListener&, int identifier, const AtomicString& eventType, Node*, const String* objectGroupId, bool disabled = false);
238 RefPtr<Inspector::Protocol::DOM::AccessibilityProperties> buildObjectForAccessibilityProperties(Node*);
239 void processAccessibilityChildren(RefPtr<AccessibilityObject>&&, RefPtr<Inspector::Protocol::Array<int>>&&);
241 Node* nodeForPath(const String& path);
242 Node* nodeForObjectId(const String& objectId);
244 void discardBindings();
246 void innerHighlightQuad(std::unique_ptr<FloatQuad>, const Inspector::InspectorObject* color, const Inspector::InspectorObject* outlineColor, const bool* usePageCoordinates);
248 Inspector::InjectedScriptManager& m_injectedScriptManager;
249 std::unique_ptr<Inspector::DOMFrontendDispatcher> m_frontendDispatcher;
250 RefPtr<Inspector::DOMBackendDispatcher> m_backendDispatcher;
251 InspectorPageAgent* m_pageAgent { nullptr };
253 InspectorOverlay* m_overlay { nullptr };
254 DOMListener* m_domListener { nullptr };
255 NodeToIdMap m_documentNodeToIdMap;
256 typedef HashMap<RefPtr<Node>, BackendNodeId> NodeToBackendIdMap;
257 HashMap<String, NodeToBackendIdMap> m_nodeGroupToBackendIdMap;
258 // Owns node mappings for dangling nodes.
259 Vector<std::unique_ptr<NodeToIdMap>> m_danglingNodeToIdMaps;
260 HashMap<int, Node*> m_idToNode;
261 HashMap<int, NodeToIdMap*> m_idToNodesMap;
262 HashSet<int> m_childrenRequested;
263 HashMap<BackendNodeId, std::pair<Node*, String>> m_backendIdToNode;
264 int m_lastNodeId { 1 };
265 BackendNodeId m_lastBackendNodeId { -1 };
266 RefPtr<Document> m_document;
267 typedef HashMap<String, Vector<RefPtr<Node>>> SearchResults;
268 SearchResults m_searchResults;
269 std::unique_ptr<RevalidateStyleAttributeTask> m_revalidateStyleAttrTask;
270 RefPtr<Node> m_nodeToFocus;
271 RefPtr<Node> m_mousedOverNode;
272 std::unique_ptr<HighlightConfig> m_inspectModeHighlightConfig;
273 std::unique_ptr<InspectorHistory> m_history;
274 std::unique_ptr<DOMEditor> m_domEditor;
275 bool m_searchingForNode { false };
276 bool m_suppressAttributeModifiedEvent { false };
277 bool m_documentRequested { false };
279 struct InspectorEventListener {
280 int identifier { 1 };
281 RefPtr<EventTarget> eventTarget;
282 AtomicString eventType;
283 bool useCapture { false };
284 bool disabled { false };
286 InspectorEventListener() { }
288 InspectorEventListener(int identifier, EventTarget& eventTarget, const AtomicString& eventType, bool useCapture)
289 : identifier(identifier)
290 , eventTarget(&eventTarget)
291 , eventType(eventType)
292 , useCapture(useCapture)
297 HashMap<EventListener*, InspectorEventListener> m_eventListenerEntries;
298 int m_lastEventListenerId { 1 };
301 } // namespace WebCore