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 insertAdjacentHTML(ErrorString&, int nodeId, const String& position, const String& html) override;
125 void setNodeValue(ErrorString&, int nodeId, const String& value) override;
126 void getEventListenersForNode(ErrorString&, int nodeId, const WTF::String* const objectGroup, RefPtr<Inspector::Protocol::Array<Inspector::Protocol::DOM::EventListener>>& listenersArray) override;
127 void setEventListenerDisabled(ErrorString&, int eventListenerId, bool disabled) override;
128 void getAccessibilityPropertiesForNode(ErrorString&, int nodeId, RefPtr<Inspector::Protocol::DOM::AccessibilityProperties>& axProperties) override;
129 void performSearch(ErrorString&, const String& whitespaceTrimmedQuery, const Inspector::InspectorArray* nodeIds, String* searchId, int* resultCount) override;
130 void getSearchResults(ErrorString&, const String& searchId, int fromIndex, int toIndex, RefPtr<Inspector::Protocol::Array<int>>&) override;
131 void discardSearchResults(ErrorString&, const String& searchId) override;
132 void resolveNode(ErrorString&, int nodeId, const String* const objectGroup, RefPtr<Inspector::Protocol::Runtime::RemoteObject>& result) override;
133 void getAttributes(ErrorString&, int nodeId, RefPtr<Inspector::Protocol::Array<String>>& result) override;
134 void setInspectModeEnabled(ErrorString&, bool enabled, const Inspector::InspectorObject* highlightConfig) override;
135 void requestNode(ErrorString&, const String& objectId, int* nodeId) override;
136 void pushNodeByPathToFrontend(ErrorString&, const String& path, int* nodeId) override;
137 void pushNodeByBackendIdToFrontend(ErrorString&, BackendNodeId, int* nodeId) override;
138 void releaseBackendNodeIds(ErrorString&, const String& nodeGroup) override;
139 void hideHighlight(ErrorString&) override;
140 void highlightRect(ErrorString&, int x, int y, int width, int height, const Inspector::InspectorObject* color, const Inspector::InspectorObject* outlineColor, const bool* const usePageCoordinates) override;
141 void highlightQuad(ErrorString&, const Inspector::InspectorArray& quad, const Inspector::InspectorObject* color, const Inspector::InspectorObject* outlineColor, const bool* const usePageCoordinates) override;
142 void highlightSelector(ErrorString&, const Inspector::InspectorObject& highlightConfig, const String& selectorString, const String* const frameId) override;
143 void highlightNode(ErrorString&, const Inspector::InspectorObject& highlightConfig, const int* const nodeId, const String* const objectId) override;
144 void highlightNodeList(ErrorString&, const Inspector::InspectorArray& nodeIds, const Inspector::InspectorObject& highlightConfig) override;
145 void highlightFrame(ErrorString&, const String& frameId, const Inspector::InspectorObject* color, const Inspector::InspectorObject* outlineColor) override;
146 void moveTo(ErrorString&, int nodeId, int targetNodeId, const int* const anchorNodeId, int* newNodeId) override;
147 void undo(ErrorString&) override;
148 void redo(ErrorString&) override;
149 void markUndoableState(ErrorString&) override;
150 void focus(ErrorString&, int nodeId) override;
151 void setInspectedNode(ErrorString&, int nodeId) override;
153 void getEventListeners(Node*, Vector<EventListenerInfo>& listenersArray, bool includeAncestors);
156 // InspectorInstrumentation
157 void didInsertDOMNode(Node&);
158 void didRemoveDOMNode(Node&);
159 void willModifyDOMAttr(Element&, const AtomicString& oldValue, const AtomicString& newValue);
160 void didModifyDOMAttr(Element&, const AtomicString& name, const AtomicString& value);
161 void didRemoveDOMAttr(Element&, const AtomicString& name);
162 void characterDataModified(CharacterData&);
163 void didInvalidateStyleAttr(Node&);
164 void didPushShadowRoot(Element& host, ShadowRoot&);
165 void willPopShadowRoot(Element& host, ShadowRoot&);
166 void didChangeCustomElementState(Element&);
167 bool handleTouchEvent(Node&);
168 void didCommitLoad(Document*);
169 void frameDocumentUpdated(Frame&);
170 void pseudoElementCreated(PseudoElement&);
171 void pseudoElementDestroyed(PseudoElement&);
172 void didAddEventListener(EventTarget&);
173 void willRemoveEventListener(EventTarget&, const AtomicString& eventType, EventListener&, bool capture);
174 bool isEventListenerDisabled(EventTarget&, const AtomicString& eventType, EventListener&, bool capture);
176 // Callbacks that don't directly correspond to an instrumentation entry point.
177 void setDocument(Document*);
178 void releaseDanglingNodes();
179 void mainFrameDOMContentLoaded();
181 void styleAttributeInvalidated(const Vector<Element*>& elements);
183 int pushNodeToFrontend(ErrorString&, int documentNodeId, Node*);
184 Node* nodeForId(int nodeId);
185 int boundNodeId(const Node*);
186 void setDOMListener(DOMListener*);
187 BackendNodeId backendNodeIdForNode(Node*, const String& nodeGroup);
189 static String documentURLString(Document*);
191 RefPtr<Inspector::Protocol::Runtime::RemoteObject> resolveNode(Node*, const String& objectGroup);
192 bool handleMousePress();
193 void mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags);
197 InspectorHistory* history() { return m_history.get(); }
199 // We represent embedded doms as a part of the same hierarchy. Hence we treat children of frame owners differently.
200 // We also skip whitespace text nodes conditionally. Following methods encapsulate these specifics.
201 static Node* innerFirstChild(Node*);
202 static Node* innerNextSibling(Node*);
203 static Node* innerPreviousSibling(Node*);
204 static unsigned innerChildNodeCount(Node*);
205 static Node* innerParentNode(Node*);
206 static bool isWhitespace(Node*);
208 Node* assertNode(ErrorString&, int nodeId);
209 Element* assertElement(ErrorString&, int nodeId);
210 Document* assertDocument(ErrorString&, int nodeId);
212 static Node* scriptValueAsNode(JSC::JSValue);
213 static JSC::JSValue nodeAsScriptValue(JSC::ExecState&, Node*);
215 // Methods called from other agents.
216 InspectorPageAgent* pageAgent() { return m_pageAgent; }
219 void highlightMousedOverNode();
220 void setSearchingForNode(ErrorString&, bool enabled, const Inspector::InspectorObject* highlightConfig);
221 std::unique_ptr<HighlightConfig> highlightConfigFromInspectorObject(ErrorString&, const Inspector::InspectorObject* highlightInspectorObject);
223 // Node-related methods.
224 typedef HashMap<RefPtr<Node>, int> NodeToIdMap;
225 int bind(Node*, NodeToIdMap*);
226 void unbind(Node*, NodeToIdMap*);
228 Node* assertEditableNode(ErrorString&, int nodeId);
229 Element* assertEditableElement(ErrorString&, int nodeId);
231 int pushNodePathToFrontend(Node*);
232 void pushChildNodesToFrontend(int nodeId, int depth = 1);
234 Ref<Inspector::Protocol::DOM::Node> buildObjectForNode(Node*, int depth, NodeToIdMap*);
235 Ref<Inspector::Protocol::Array<String>> buildArrayForElementAttributes(Element*);
236 Ref<Inspector::Protocol::Array<Inspector::Protocol::DOM::Node>> buildArrayForContainerChildren(Node* container, int depth, NodeToIdMap* nodesMap);
237 RefPtr<Inspector::Protocol::Array<Inspector::Protocol::DOM::Node>> buildArrayForPseudoElements(const Element&, NodeToIdMap* nodesMap);
238 Ref<Inspector::Protocol::DOM::EventListener> buildObjectForEventListener(const RegisteredEventListener&, int identifier, const AtomicString& eventType, Node*, const String* objectGroupId, bool disabled = false);
239 RefPtr<Inspector::Protocol::DOM::AccessibilityProperties> buildObjectForAccessibilityProperties(Node*);
240 void processAccessibilityChildren(RefPtr<AccessibilityObject>&&, RefPtr<Inspector::Protocol::Array<int>>&&);
242 Node* nodeForPath(const String& path);
243 Node* nodeForObjectId(const String& objectId);
245 void discardBindings();
247 void innerHighlightQuad(std::unique_ptr<FloatQuad>, const Inspector::InspectorObject* color, const Inspector::InspectorObject* outlineColor, const bool* usePageCoordinates);
249 Inspector::InjectedScriptManager& m_injectedScriptManager;
250 std::unique_ptr<Inspector::DOMFrontendDispatcher> m_frontendDispatcher;
251 RefPtr<Inspector::DOMBackendDispatcher> m_backendDispatcher;
252 InspectorPageAgent* m_pageAgent { nullptr };
254 InspectorOverlay* m_overlay { nullptr };
255 DOMListener* m_domListener { nullptr };
256 NodeToIdMap m_documentNodeToIdMap;
257 typedef HashMap<RefPtr<Node>, BackendNodeId> NodeToBackendIdMap;
258 HashMap<String, NodeToBackendIdMap> m_nodeGroupToBackendIdMap;
259 // Owns node mappings for dangling nodes.
260 Vector<std::unique_ptr<NodeToIdMap>> m_danglingNodeToIdMaps;
261 HashMap<int, Node*> m_idToNode;
262 HashMap<int, NodeToIdMap*> m_idToNodesMap;
263 HashSet<int> m_childrenRequested;
264 HashMap<BackendNodeId, std::pair<Node*, String>> m_backendIdToNode;
265 int m_lastNodeId { 1 };
266 BackendNodeId m_lastBackendNodeId { -1 };
267 RefPtr<Document> m_document;
268 typedef HashMap<String, Vector<RefPtr<Node>>> SearchResults;
269 SearchResults m_searchResults;
270 std::unique_ptr<RevalidateStyleAttributeTask> m_revalidateStyleAttrTask;
271 RefPtr<Node> m_nodeToFocus;
272 RefPtr<Node> m_mousedOverNode;
273 std::unique_ptr<HighlightConfig> m_inspectModeHighlightConfig;
274 std::unique_ptr<InspectorHistory> m_history;
275 std::unique_ptr<DOMEditor> m_domEditor;
276 bool m_searchingForNode { false };
277 bool m_suppressAttributeModifiedEvent { false };
278 bool m_documentRequested { false };
280 struct InspectorEventListener {
281 int identifier { 1 };
282 RefPtr<EventTarget> eventTarget;
283 AtomicString eventType;
284 bool useCapture { false };
285 bool disabled { false };
287 InspectorEventListener() { }
289 InspectorEventListener(int identifier, EventTarget& eventTarget, const AtomicString& eventType, bool useCapture)
290 : identifier(identifier)
291 , eventTarget(&eventTarget)
292 , eventType(eventType)
293 , useCapture(useCapture)
298 HashMap<EventListener*, InspectorEventListener> m_eventListenerEntries;
299 int m_lastEventListenerId { 1 };
302 } // namespace WebCore