tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / inspector / InspectorDOMAgent.h
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 "EventTarget.h"
34 #include "InjectedScript.h"
35 #include "InjectedScriptManager.h"
36 #include "InspectorBaseAgent.h"
37 #include "InspectorFrontend.h"
38 #include "InspectorValues.h"
39 #include "Timer.h"
40
41 #include <wtf/Deque.h>
42 #include <wtf/ListHashSet.h>
43 #include <wtf/HashMap.h>
44 #include <wtf/HashSet.h>
45 #include <wtf/OwnPtr.h>
46 #include <wtf/PassOwnPtr.h>
47 #include <wtf/RefPtr.h>
48 #include <wtf/Vector.h>
49 #include <wtf/text/AtomicString.h>
50
51 namespace WebCore {
52 class ContainerNode;
53 class CharacterData;
54 class Document;
55 class Element;
56 class Event;
57 class GraphicsContext;
58 class InspectorClient;
59 class InspectorDOMAgent;
60 class InspectorFrontend;
61 class InspectorPageAgent;
62 class IntRect;
63 class HitTestResult;
64 class HTMLElement;
65 struct HighlightData;
66 class InspectorState;
67 class InstrumentingAgents;
68 class NameNodeMap;
69 class Node;
70 class RevalidateStyleAttributeTask;
71 class ScriptValue;
72
73 typedef String ErrorString;
74
75 #if ENABLE(INSPECTOR)
76
77 struct EventListenerInfo {
78     EventListenerInfo(Node* node, const AtomicString& eventType, const EventListenerVector& eventListenerVector)
79         : node(node)
80         , eventType(eventType)
81         , eventListenerVector(eventListenerVector)
82     {
83     }
84
85     Node* node;
86     const AtomicString eventType;
87     const EventListenerVector eventListenerVector;
88 };
89
90 class InspectorDOMAgent : public InspectorBaseAgent<InspectorDOMAgent> {
91     WTF_MAKE_NONCOPYABLE(InspectorDOMAgent);
92 public:
93     struct DOMListener {
94         virtual ~DOMListener()
95         {
96         }
97         virtual void didRemoveDocument(Document*) = 0;
98         virtual void didRemoveDOMNode(Node*) = 0;
99         virtual void didModifyDOMAttr(Element*) = 0;
100     };
101
102     static PassOwnPtr<InspectorDOMAgent> create(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorClient* client, InspectorState* inspectorState, InjectedScriptManager* injectedScriptManager)
103     {
104         return adoptPtr(new InspectorDOMAgent(instrumentingAgents, pageAgent, client, inspectorState, injectedScriptManager));
105     }
106
107     ~InspectorDOMAgent();
108
109     virtual void setFrontend(InspectorFrontend*);
110     virtual void clearFrontend();
111     virtual void restore();
112
113     Vector<Document*> documents();
114     void reset();
115
116     // Methods called from the frontend for DOM nodes inspection.
117     void querySelector(ErrorString*, int nodeId, const String& selectors, int* elementId);
118     void querySelectorAll(ErrorString*, int nodeId, const String& selectors, RefPtr<InspectorArray>* result);
119     void getDocument(ErrorString*, RefPtr<InspectorObject>* root);
120     void requestChildNodes(ErrorString*, int nodeId);
121     void setAttributeValue(ErrorString*, int elementId, const String& name, const String& value);
122     void setAttributesAsText(ErrorString*, int elementId, const String& text, const String* const name);
123     void removeAttribute(ErrorString*, int elementId, const String& name);
124     void removeNode(ErrorString*, int nodeId);
125     void setNodeName(ErrorString*, int nodeId, const String& name, int* newId);
126     void getOuterHTML(ErrorString*, int nodeId, WTF::String* outerHTML);
127     void setOuterHTML(ErrorString*, int nodeId, const String& outerHTML, int* newId);
128     void setNodeValue(ErrorString*, int nodeId, const String& value);
129     void getEventListenersForNode(ErrorString*, int nodeId, RefPtr<InspectorArray>* listenersArray);
130     void performSearch(ErrorString*, const String& whitespaceTrimmedQuery, String* searchId, int* resultCount);
131     void getSearchResults(ErrorString*, const String& searchId, int fromIndex, int toIndex, RefPtr<InspectorArray>*);
132     void discardSearchResults(ErrorString*, const String& searchId);
133     void resolveNode(ErrorString*, int nodeId, const String* const objectGroup, RefPtr<InspectorObject>* result);
134     void getAttributes(ErrorString*, int nodeId, RefPtr<InspectorArray>* result);
135     void setInspectModeEnabled(ErrorString*, bool enabled, const RefPtr<InspectorObject>* highlightConfig);
136     void requestNode(ErrorString*, const String& objectId, int* nodeId);
137     void pushNodeByPathToFrontend(ErrorString*, const String& path, int* nodeId);
138     void hideHighlight(ErrorString*);
139     void highlightRect(ErrorString*, int x, int y, int width, int height, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor);
140     void highlightNode(ErrorString*, int nodeId, const RefPtr<InspectorObject> highlightConfig);
141     void highlightFrame(ErrorString*, const String& frameId, const RefPtr<InspectorObject>* color, const RefPtr<InspectorObject>* outlineColor);
142     void moveTo(ErrorString*, int nodeId, int targetNodeId, const int* const anchorNodeId, int* newNodeId);
143
144     Node* highlightedNode() const;
145
146     // Methods called from the InspectorInstrumentation.
147     void setDocument(Document*);
148     void releaseDanglingNodes();
149
150     void mainFrameDOMContentLoaded();
151     void loadEventFired(Document*);
152
153     void didInsertDOMNode(Node*);
154     void didRemoveDOMNode(Node*);
155     void didModifyDOMAttr(Element*, const AtomicString& name, const AtomicString& value);
156     void didRemoveDOMAttr(Element*, const AtomicString& name);
157     void styleAttributeInvalidated(const Vector<Element*>& elements);
158     void characterDataModified(CharacterData*);
159     void didInvalidateStyleAttr(Node*);
160
161     Node* nodeForId(int nodeId);
162     int boundNodeId(Node*);
163     void copyNode(ErrorString*, int nodeId);
164     void setDOMListener(DOMListener*);
165
166     static String documentURLString(Document*);
167
168     PassRefPtr<InspectorObject> resolveNode(Node*, const String& objectGroup);
169     bool handleMousePress();
170     bool searchingForNodeInPage() const;
171     void mouseDidMoveOverElement(const HitTestResult&, unsigned modifierFlags);
172     void inspect(Node*);
173     void focusNode();
174
175     void drawHighlight(GraphicsContext&) const;
176
177     // We represent embedded doms as a part of the same hierarchy. Hence we treat children of frame owners differently.
178     // We also skip whitespace text nodes conditionally. Following methods encapsulate these specifics.
179     static Node* innerFirstChild(Node*);
180     static Node* innerNextSibling(Node*);
181     static Node* innerPreviousSibling(Node*);
182     static unsigned innerChildNodeCount(Node*);
183     static Node* innerParentNode(Node*);
184     static bool isWhitespace(Node*);
185
186     Node* assertNode(ErrorString*, int nodeId);
187
188 private:
189     InspectorDOMAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorClient*, InspectorState*, InjectedScriptManager*);
190
191     void setSearchingForNode(bool enabled, InspectorObject* highlightConfig);
192     bool setHighlightDataFromConfig(InspectorObject* highlightConfig);
193     void highlight();
194
195     // Node-related methods.
196     typedef HashMap<RefPtr<Node>, int> NodeToIdMap;
197     int bind(Node*, NodeToIdMap*);
198     void unbind(Node*, NodeToIdMap*);
199     Element* assertElement(ErrorString*, int nodeId);
200     HTMLElement* assertHTMLElement(ErrorString*, int nodeId);
201
202     int pushNodePathToFrontend(Node*);
203     void pushChildNodesToFrontend(int nodeId);
204
205     bool hasBreakpoint(Node*, int type);
206     void updateSubtreeBreakpoints(Node* root, uint32_t rootMask, bool value);
207     void descriptionForDOMEvent(Node* target, int breakpointType, bool insertion, PassRefPtr<InspectorObject> description);
208
209     PassRefPtr<InspectorObject> buildObjectForNode(Node*, int depth, NodeToIdMap*);
210     PassRefPtr<InspectorArray> buildArrayForElementAttributes(Element*);
211     PassRefPtr<InspectorArray> buildArrayForContainerChildren(Node* container, int depth, NodeToIdMap* nodesMap);
212     PassRefPtr<InspectorObject> buildObjectForEventListener(const RegisteredEventListener&, const AtomicString& eventType, Node*);
213
214     Node* nodeForPath(const String& path);
215
216     void discardBindings();
217
218     InspectorPageAgent* m_pageAgent;
219     InspectorClient* m_client;
220     InjectedScriptManager* m_injectedScriptManager;
221     InspectorFrontend::DOM* m_frontend;
222     DOMListener* m_domListener;
223     NodeToIdMap m_documentNodeToIdMap;
224     // Owns node mappings for dangling nodes.
225     Vector<NodeToIdMap*> m_danglingNodeToIdMaps;
226     HashMap<int, Node*> m_idToNode;
227     HashMap<int, NodeToIdMap*> m_idToNodesMap;
228     HashSet<int> m_childrenRequested;
229     int m_lastNodeId;
230     RefPtr<Document> m_document;
231     typedef HashMap<String, Vector<RefPtr<Node> > > SearchResults;
232     SearchResults m_searchResults;
233     OwnPtr<RevalidateStyleAttributeTask> m_revalidateStyleAttrTask;
234     OwnPtr<HighlightData> m_highlightData;
235     RefPtr<Node> m_nodeToFocus;
236     bool m_searchingForNode;
237 };
238
239 #endif // ENABLE(INSPECTOR)
240
241 } // namespace WebCore
242
243 #endif // !defined(InspectorDOMAgent_h)