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