• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
39 
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"
47 
48 namespace WebCore {
49 
50 class CharacterData;
51 class DOMEditor;
52 class Document;
53 class Element;
54 class EventTarget;
55 class ExceptionState;
56 class InspectorFrontend;
57 class InspectorHistory;
58 class InspectorOverlay;
59 class InspectorPageAgent;
60 class InspectorState;
61 class InstrumentingAgents;
62 class Node;
63 class PlatformGestureEvent;
64 class PlatformTouchEvent;
65 class RevalidateStyleAttributeTask;
66 class ShadowRoot;
67 
68 struct HighlightConfig;
69 
70 typedef String ErrorString;
71 
72 
73 struct EventListenerInfo {
EventListenerInfoEventListenerInfo74     EventListenerInfo(EventTarget* eventTarget, const AtomicString& eventType, const EventListenerVector& eventListenerVector)
75         : eventTarget(eventTarget)
76         , eventType(eventType)
77         , eventListenerVector(eventListenerVector)
78     {
79     }
80 
81     EventTarget* eventTarget;
82     const AtomicString eventType;
83     const EventListenerVector eventListenerVector;
84 };
85 
86 class InspectorDOMAgent FINAL : public InspectorBaseAgent<InspectorDOMAgent>, public InspectorBackendDispatcher::DOMCommandHandler {
87     WTF_MAKE_NONCOPYABLE(InspectorDOMAgent);
88 public:
89     struct DOMListener {
~DOMListenerDOMListener90         virtual ~DOMListener()
91         {
92         }
93         virtual void didRemoveDocument(Document*) = 0;
94         virtual void didRemoveDOMNode(Node*) = 0;
95         virtual void didModifyDOMAttr(Element*) = 0;
96     };
97 
create(InspectorPageAgent * pageAgent,InjectedScriptManager * injectedScriptManager,InspectorOverlay * overlay)98     static PassOwnPtr<InspectorDOMAgent> create(InspectorPageAgent* pageAgent, InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay)
99     {
100         return adoptPtr(new InspectorDOMAgent(pageAgent, injectedScriptManager, overlay));
101     }
102 
103     static String toErrorString(ExceptionState&);
104 
105     virtual ~InspectorDOMAgent();
106 
107     virtual void setFrontend(InspectorFrontend*) OVERRIDE;
108     virtual void clearFrontend() OVERRIDE;
109     virtual void restore() OVERRIDE;
110 
111     Vector<Document*> documents();
112     void reset();
113 
114     // Methods called from the frontend for DOM nodes inspection.
115     virtual void enable(ErrorString*) OVERRIDE;
116     virtual void disable(ErrorString*) OVERRIDE;
117     virtual void querySelector(ErrorString*, int nodeId, const String& selectors, int* elementId) OVERRIDE;
118     virtual void querySelectorAll(ErrorString*, int nodeId, const String& selectors, RefPtr<TypeBuilder::Array<int> >& result) OVERRIDE;
119     virtual void getDocument(ErrorString*, RefPtr<TypeBuilder::DOM::Node>& root) OVERRIDE;
120     virtual void requestChildNodes(ErrorString*, int nodeId, const int* depth) OVERRIDE;
121     virtual void setAttributeValue(ErrorString*, int elementId, const String& name, const String& value) OVERRIDE;
122     virtual void setAttributesAsText(ErrorString*, int elementId, const String& text, const String* name) OVERRIDE;
123     virtual void removeAttribute(ErrorString*, int elementId, const String& name) OVERRIDE;
124     virtual void removeNode(ErrorString*, int nodeId) OVERRIDE;
125     virtual void setNodeName(ErrorString*, int nodeId, const String& name, int* newId) OVERRIDE;
126     virtual void getOuterHTML(ErrorString*, int nodeId, WTF::String* outerHTML) OVERRIDE;
127     virtual void setOuterHTML(ErrorString*, int nodeId, const String& outerHTML) OVERRIDE;
128     virtual void setNodeValue(ErrorString*, int nodeId, const String& value) OVERRIDE;
129     virtual void getEventListenersForNode(ErrorString*, int nodeId, const WTF::String* objectGroup, RefPtr<TypeBuilder::Array<TypeBuilder::DOM::EventListener> >& listenersArray) OVERRIDE;
130     virtual void performSearch(ErrorString*, const String& whitespaceTrimmedQuery, String* searchId, int* resultCount) OVERRIDE;
131     virtual void getSearchResults(ErrorString*, const String& searchId, int fromIndex, int toIndex, RefPtr<TypeBuilder::Array<int> >&) OVERRIDE;
132     virtual void discardSearchResults(ErrorString*, const String& searchId) OVERRIDE;
133     virtual void resolveNode(ErrorString*, int nodeId, const String* objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result) OVERRIDE;
134     virtual void getAttributes(ErrorString*, int nodeId, RefPtr<TypeBuilder::Array<String> >& result) OVERRIDE;
135     virtual void setInspectModeEnabled(ErrorString*, bool enabled, const bool* inspectUAShadowDOM, const RefPtr<JSONObject>* highlightConfig) OVERRIDE;
136     virtual void requestNode(ErrorString*, const String& objectId, int* nodeId) OVERRIDE;
137     virtual void pushNodeByPathToFrontend(ErrorString*, const String& path, int* nodeId) OVERRIDE;
138     virtual void pushNodesByBackendIdsToFrontend(ErrorString*, const RefPtr<JSONArray>& nodeIds, RefPtr<TypeBuilder::Array<int> >&) OVERRIDE;
139     virtual void hideHighlight(ErrorString*) OVERRIDE;
140     virtual void highlightRect(ErrorString*, int x, int y, int width, int height, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor) OVERRIDE;
141     virtual void highlightQuad(ErrorString*, const RefPtr<JSONArray>& quad, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor) OVERRIDE;
142     virtual void highlightNode(ErrorString*, const RefPtr<JSONObject>& highlightConfig, const int* nodeId, const String* objectId) OVERRIDE;
143     virtual void highlightFrame(ErrorString*, const String& frameId, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor) OVERRIDE;
144 
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 
155     static void getEventListeners(EventTarget*, Vector<EventListenerInfo>& listenersArray, bool includeAncestors);
156 
157     class Listener {
158     public:
~Listener()159         virtual ~Listener() { }
160         virtual void domAgentWasEnabled() = 0;
161         virtual void domAgentWasDisabled() = 0;
162     };
setListener(Listener * listener)163     void setListener(Listener* listener) { m_listener = listener; }
164 
165     bool enabled() const;
166 
167     // Methods called from the InspectorInstrumentation.
168     void setDocument(Document*);
169     void releaseDanglingNodes();
170 
171     void domContentLoadedEventFired(LocalFrame*);
172     void didCommitLoad(LocalFrame*, DocumentLoader*);
173 
174     void didInsertDOMNode(Node*);
175     void willRemoveDOMNode(Node*);
176     void willModifyDOMAttr(Element*, const AtomicString& oldValue, const AtomicString& newValue);
177     void didModifyDOMAttr(Element*, const AtomicString& name, const AtomicString& value);
178     void didRemoveDOMAttr(Element*, const AtomicString& name);
179     void styleAttributeInvalidated(const WillBeHeapVector<RawPtrWillBeMember<Element> >& elements);
180     void characterDataModified(CharacterData*);
181     void didInvalidateStyleAttr(Node*);
182     void didPushShadowRoot(Element* host, ShadowRoot*);
183     void willPopShadowRoot(Element* host, ShadowRoot*);
184     void frameDocumentUpdated(LocalFrame*);
185     void pseudoElementCreated(PseudoElement*);
186     void pseudoElementDestroyed(PseudoElement*);
187 
188     Node* nodeForId(int nodeId);
189     int boundNodeId(Node*);
190     void setDOMListener(DOMListener*);
191 
192     static String documentURLString(Document*);
193 
194     PassRefPtr<TypeBuilder::Runtime::RemoteObject> resolveNode(Node*, const String& objectGroup);
195     bool handleMousePress();
196     bool handleGestureEvent(LocalFrame*, const PlatformGestureEvent&);
197     bool handleTouchEvent(LocalFrame*, const PlatformTouchEvent&);
198     void handleMouseMove(LocalFrame*, const PlatformMouseEvent&);
199 
history()200     InspectorHistory* history() { return m_history.get(); }
201 
202     // We represent embedded doms as a part of the same hierarchy. Hence we treat children of frame owners differently.
203     // We also skip whitespace text nodes conditionally. Following methods encapsulate these specifics.
204     static Node* innerFirstChild(Node*);
205     static Node* innerNextSibling(Node*);
206     static Node* innerPreviousSibling(Node*);
207     static unsigned innerChildNodeCount(Node*);
208     static Node* innerParentNode(Node*);
209     static bool isWhitespace(Node*);
210 
211     Node* assertNode(ErrorString*, int nodeId);
212     Element* assertElement(ErrorString*, int nodeId);
213     Document* assertDocument(ErrorString*, int nodeId);
214 
215 private:
216     enum SearchMode { NotSearching, SearchingForNormal, SearchingForUAShadow };
217 
218     InspectorDOMAgent(InspectorPageAgent*, InjectedScriptManager*, InspectorOverlay*);
219 
220     void setSearchingForNode(ErrorString*, SearchMode, JSONObject* highlightConfig);
221     PassOwnPtr<HighlightConfig> highlightConfigFromInspectorObject(ErrorString*, JSONObject* highlightInspectorObject);
222 
223     // Node-related methods.
224     typedef WillBeHeapHashMap<RefPtrWillBeMember<Node>, int> NodeToIdMap;
225     int bind(Node*, NodeToIdMap*);
226     void unbind(Node*, NodeToIdMap*);
227 
228     Node* assertEditableNode(ErrorString*, int nodeId);
229     Element* assertEditableElement(ErrorString*, int nodeId);
230 
231     void inspect(Node*);
232 
233     int pushNodePathToFrontend(Node*);
234     void pushChildNodesToFrontend(int nodeId, int depth = 1);
235 
236     void invalidateFrameOwnerElement(LocalFrame*);
237 
238     bool hasBreakpoint(Node*, int type);
239     void updateSubtreeBreakpoints(Node* root, uint32_t rootMask, bool value);
240     void descriptionForDOMEvent(Node* target, int breakpointType, bool insertion, PassRefPtr<JSONObject> description);
241 
242     PassRefPtr<TypeBuilder::DOM::Node> buildObjectForNode(Node*, int depth, NodeToIdMap*);
243     PassRefPtr<TypeBuilder::Array<String> > buildArrayForElementAttributes(Element*);
244     PassRefPtr<TypeBuilder::Array<TypeBuilder::DOM::Node> > buildArrayForContainerChildren(Node* container, int depth, NodeToIdMap* nodesMap);
245     PassRefPtr<TypeBuilder::DOM::EventListener> buildObjectForEventListener(const RegisteredEventListener&, const AtomicString& eventType, Node*, const String* objectGroupId);
246     PassRefPtr<TypeBuilder::Array<TypeBuilder::DOM::Node> > buildArrayForPseudoElements(Element*, NodeToIdMap* nodesMap);
247 
248     Node* nodeForPath(const String& path);
249 
250     void discardFrontendBindings();
251 
252     void innerHighlightQuad(PassOwnPtr<FloatQuad>, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor);
253 
254     bool pushDocumentUponHandlelessOperation(ErrorString*);
255 
256     InspectorPageAgent* m_pageAgent;
257     InjectedScriptManager* m_injectedScriptManager;
258     InspectorOverlay* m_overlay;
259     InspectorFrontend::DOM* m_frontend;
260     DOMListener* m_domListener;
261     OwnPtrWillBePersistent<NodeToIdMap> m_documentNodeToIdMap;
262     // Owns node mappings for dangling nodes.
263     WillBePersistentHeapVector<OwnPtrWillBeMember<NodeToIdMap> > m_danglingNodeToIdMaps;
264     WillBePersistentHeapHashMap<int, RawPtrWillBeMember<Node> > m_idToNode;
265     WillBePersistentHeapHashMap<int, RawPtrWillBeMember<NodeToIdMap> > m_idToNodesMap;
266     HashSet<int> m_childrenRequested;
267     HashMap<int, int> m_cachedChildCount;
268     int m_lastNodeId;
269     RefPtrWillBePersistent<Document> m_document;
270     typedef WillBePersistentHeapHashMap<String, WillBeHeapVector<RefPtrWillBeMember<Node> > > SearchResults;
271     SearchResults m_searchResults;
272     OwnPtr<RevalidateStyleAttributeTask> m_revalidateStyleAttrTask;
273     SearchMode m_searchingForNode;
274     OwnPtr<HighlightConfig> m_inspectModeHighlightConfig;
275     OwnPtrWillBePersistent<InspectorHistory> m_history;
276     OwnPtrWillBePersistent<DOMEditor> m_domEditor;
277     bool m_suppressAttributeModifiedEvent;
278     Listener* m_listener;
279 };
280 
281 
282 } // namespace WebCore
283 
284 #endif // !defined(InspectorDOMAgent_h)
285