Home | History | Annotate | Download | only in inspector
      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 "InspectorFrontend.h"
     34 #include "core/inspector/InjectedScript.h"
     35 #include "core/inspector/InjectedScriptManager.h"
     36 #include "core/inspector/InspectorBaseAgent.h"
     37 #include "core/platform/JSONValues.h"
     38 #include "core/rendering/RenderLayer.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 ExceptionState;
     55 class InspectorClient;
     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 typedef int BackendNodeId;
     72 
     73 
     74 struct EventListenerInfo {
     75     EventListenerInfo(Node* node, const AtomicString& eventType, const EventListenerVector& eventListenerVector)
     76         : node(node)
     77         , eventType(eventType)
     78         , eventListenerVector(eventListenerVector)
     79     {
     80     }
     81 
     82     Node* node;
     83     const AtomicString eventType;
     84     const EventListenerVector eventListenerVector;
     85 };
     86 
     87 class InspectorDOMAgent : public InspectorBaseAgent<InspectorDOMAgent>, public InspectorBackendDispatcher::DOMCommandHandler {
     88     WTF_MAKE_NONCOPYABLE(InspectorDOMAgent);
     89 public:
     90     struct DOMListener {
     91         virtual ~DOMListener()
     92         {
     93         }
     94         virtual void didRemoveDocument(Document*) = 0;
     95         virtual void didRemoveDOMNode(Node*) = 0;
     96         virtual void didModifyDOMAttr(Element*) = 0;
     97     };
     98 
     99     static PassOwnPtr<InspectorDOMAgent> create(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorCompositeState* inspectorState, InjectedScriptManager* injectedScriptManager, InspectorOverlay* overlay, InspectorClient* client)
    100     {
    101         return adoptPtr(new InspectorDOMAgent(instrumentingAgents, pageAgent, inspectorState, injectedScriptManager, overlay, client));
    102     }
    103 
    104     static String toErrorString(ExceptionState&);
    105 
    106     ~InspectorDOMAgent();
    107 
    108     virtual void setFrontend(InspectorFrontend*);
    109     virtual void clearFrontend();
    110     virtual void restore();
    111 
    112     Vector<Document*> documents();
    113     void reset();
    114 
    115     // Methods called from the frontend for DOM nodes inspection.
    116     virtual void querySelector(ErrorString*, int nodeId, const String& selectors, int* elementId);
    117     virtual void querySelectorAll(ErrorString*, int nodeId, const String& selectors, RefPtr<TypeBuilder::Array<int> >& result);
    118     virtual void getDocument(ErrorString*, RefPtr<TypeBuilder::DOM::Node>& root);
    119     virtual void requestChildNodes(ErrorString*, int nodeId, const int* depth);
    120     virtual void setAttributeValue(ErrorString*, int elementId, const String& name, const String& value);
    121     virtual void setAttributesAsText(ErrorString*, int elementId, const String& text, const String* name);
    122     virtual void removeAttribute(ErrorString*, int elementId, const String& name);
    123     virtual void removeNode(ErrorString*, int nodeId);
    124     virtual void setNodeName(ErrorString*, int nodeId, const String& name, int* newId);
    125     virtual void getOuterHTML(ErrorString*, int nodeId, WTF::String* outerHTML);
    126     virtual void setOuterHTML(ErrorString*, int nodeId, const String& outerHTML);
    127     virtual void setNodeValue(ErrorString*, int nodeId, const String& value);
    128     virtual void getEventListenersForNode(ErrorString*, int nodeId, const WTF::String* objectGroup, RefPtr<TypeBuilder::Array<TypeBuilder::DOM::EventListener> >& listenersArray);
    129     virtual void performSearch(ErrorString*, const String& whitespaceTrimmedQuery, String* searchId, int* resultCount);
    130     virtual void getSearchResults(ErrorString*, const String& searchId, int fromIndex, int toIndex, RefPtr<TypeBuilder::Array<int> >&);
    131     virtual void discardSearchResults(ErrorString*, const String& searchId);
    132     virtual void resolveNode(ErrorString*, int nodeId, const String* objectGroup, RefPtr<TypeBuilder::Runtime::RemoteObject>& result);
    133     virtual void getAttributes(ErrorString*, int nodeId, RefPtr<TypeBuilder::Array<String> >& result);
    134     virtual void setInspectModeEnabled(ErrorString*, bool enabled, const bool* inspectShadowDOM, const RefPtr<JSONObject>* highlightConfig);
    135     virtual void requestNode(ErrorString*, const String& objectId, int* nodeId);
    136     virtual void pushNodeByPathToFrontend(ErrorString*, const String& path, int* nodeId);
    137     virtual void pushNodeByBackendIdToFrontend(ErrorString*, BackendNodeId, int* nodeId);
    138     virtual void releaseBackendNodeIds(ErrorString*, const String& nodeGroup);
    139     virtual void hideHighlight(ErrorString*);
    140     virtual void highlightRect(ErrorString*, int x, int y, int width, int height, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor);
    141     virtual void highlightQuad(ErrorString*, const RefPtr<JSONArray>& quad, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor);
    142     virtual void highlightNode(ErrorString*, const RefPtr<JSONObject>& highlightConfig, const int* nodeId, const String* objectId);
    143     virtual void highlightFrame(ErrorString*, const String& frameId, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor);
    144 
    145     virtual void moveTo(ErrorString*, int nodeId, int targetNodeId, const int* anchorNodeId, int* newNodeId);
    146     virtual void undo(ErrorString*);
    147     virtual void redo(ErrorString*);
    148     virtual void markUndoableState(ErrorString*);
    149     virtual void focus(ErrorString*, int nodeId);
    150     virtual void setFileInputFiles(ErrorString*, int nodeId, const RefPtr<JSONArray>& files);
    151 
    152     static void getEventListeners(Node*, Vector<EventListenerInfo>& listenersArray, bool includeAncestors);
    153 
    154     // Methods called from the InspectorInstrumentation.
    155     void setDocument(Document*);
    156     void releaseDanglingNodes();
    157 
    158     void domContentLoadedEventFired(Frame*);
    159     void loadEventFired(Frame*);
    160     void didCommitLoad(Frame*, DocumentLoader*);
    161 
    162     void didInsertDOMNode(Node*);
    163     void willRemoveDOMNode(Node*);
    164     void willModifyDOMAttr(Element*, const AtomicString& oldValue, const AtomicString& newValue);
    165     void didModifyDOMAttr(Element*, const AtomicString& name, const AtomicString& value);
    166     void didRemoveDOMAttr(Element*, const AtomicString& name);
    167     void styleAttributeInvalidated(const Vector<Element*>& elements);
    168     void characterDataModified(CharacterData*);
    169     void didInvalidateStyleAttr(Node*);
    170     void didPushShadowRoot(Element* host, ShadowRoot*);
    171     void willPopShadowRoot(Element* host, ShadowRoot*);
    172     void frameDocumentUpdated(Frame*);
    173 
    174     int pushNodeToFrontend(ErrorString*, int documentNodeId, Node*);
    175     Node* nodeForId(int nodeId);
    176     int boundNodeId(Node*);
    177     void setDOMListener(DOMListener*);
    178     BackendNodeId backendNodeIdForNode(Node*, const String& nodeGroup);
    179 
    180     static String documentURLString(Document*);
    181 
    182     PassRefPtr<TypeBuilder::Runtime::RemoteObject> resolveNode(Node*, const String& objectGroup);
    183     bool handleMousePress();
    184     bool handleGestureEvent(Frame*, const PlatformGestureEvent&);
    185     bool handleTouchEvent(Frame*, const PlatformTouchEvent&);
    186     void handleMouseMove(Frame*, const PlatformMouseEvent&);
    187 
    188     InspectorHistory* history() { return m_history.get(); }
    189 
    190     // We represent embedded doms as a part of the same hierarchy. Hence we treat children of frame owners differently.
    191     // We also skip whitespace text nodes conditionally. Following methods encapsulate these specifics.
    192     static Node* innerFirstChild(Node*);
    193     static Node* innerNextSibling(Node*);
    194     static Node* innerPreviousSibling(Node*);
    195     static unsigned innerChildNodeCount(Node*);
    196     static Node* innerParentNode(Node*);
    197     static bool isWhitespace(Node*);
    198 
    199     Node* assertNode(ErrorString*, int nodeId);
    200     Element* assertElement(ErrorString*, int nodeId);
    201     Document* assertDocument(ErrorString*, int nodeId);
    202 
    203     // Methods called from other agents.
    204     InspectorPageAgent* pageAgent() { return m_pageAgent; }
    205 
    206 private:
    207     enum SearchMode { NotSearching, SearchingForNormal, SearchingForShadow };
    208 
    209     InspectorDOMAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorCompositeState*, InjectedScriptManager*, InspectorOverlay*, InspectorClient*);
    210 
    211     void setSearchingForNode(ErrorString*, SearchMode, JSONObject* highlightConfig);
    212     PassOwnPtr<HighlightConfig> highlightConfigFromInspectorObject(ErrorString*, JSONObject* highlightInspectorObject);
    213 
    214     // Node-related methods.
    215     typedef HashMap<RefPtr<Node>, int> NodeToIdMap;
    216     int bind(Node*, NodeToIdMap*);
    217     void unbind(Node*, NodeToIdMap*);
    218 
    219     Node* assertEditableNode(ErrorString*, int nodeId);
    220     Element* assertEditableElement(ErrorString*, int nodeId);
    221 
    222     void inspect(Node*);
    223 
    224     int pushNodePathToFrontend(Node*);
    225     void pushChildNodesToFrontend(int nodeId, int depth = 1);
    226 
    227     bool hasBreakpoint(Node*, int type);
    228     void updateSubtreeBreakpoints(Node* root, uint32_t rootMask, bool value);
    229     void descriptionForDOMEvent(Node* target, int breakpointType, bool insertion, PassRefPtr<JSONObject> description);
    230 
    231     PassRefPtr<TypeBuilder::DOM::Node> buildObjectForNode(Node*, int depth, NodeToIdMap*);
    232     PassRefPtr<TypeBuilder::Array<String> > buildArrayForElementAttributes(Element*);
    233     PassRefPtr<TypeBuilder::Array<TypeBuilder::DOM::Node> > buildArrayForContainerChildren(Node* container, int depth, NodeToIdMap* nodesMap);
    234     PassRefPtr<TypeBuilder::DOM::EventListener> buildObjectForEventListener(const RegisteredEventListener&, const AtomicString& eventType, Node*, const String* objectGroupId);
    235 
    236     Node* nodeForPath(const String& path);
    237 
    238     void discardBackendBindings();
    239     void discardFrontendBindings();
    240 
    241     void innerHighlightQuad(PassOwnPtr<FloatQuad>, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor);
    242 
    243     InspectorPageAgent* m_pageAgent;
    244     InjectedScriptManager* m_injectedScriptManager;
    245     InspectorOverlay* m_overlay;
    246     InspectorClient* m_client;
    247     InspectorFrontend::DOM* m_frontend;
    248     DOMListener* m_domListener;
    249     NodeToIdMap m_documentNodeToIdMap;
    250     typedef HashMap<RefPtr<Node>, BackendNodeId> NodeToBackendIdMap;
    251     HashMap<String, NodeToBackendIdMap> m_nodeGroupToBackendIdMap;
    252     // Owns node mappings for dangling nodes.
    253     Vector<OwnPtr<NodeToIdMap> > m_danglingNodeToIdMaps;
    254     HashMap<int, Node*> m_idToNode;
    255     HashMap<int, NodeToIdMap*> m_idToNodesMap;
    256     HashSet<int> m_childrenRequested;
    257     HashMap<BackendNodeId, std::pair<Node*, String> > m_backendIdToNode;
    258     int m_lastNodeId;
    259     BackendNodeId m_lastBackendNodeId;
    260     RefPtr<Document> m_document;
    261     typedef HashMap<String, Vector<RefPtr<Node> > > SearchResults;
    262     SearchResults m_searchResults;
    263     OwnPtr<RevalidateStyleAttributeTask> m_revalidateStyleAttrTask;
    264     SearchMode m_searchingForNode;
    265     OwnPtr<HighlightConfig> m_inspectModeHighlightConfig;
    266     OwnPtr<InspectorHistory> m_history;
    267     OwnPtr<DOMEditor> m_domEditor;
    268     bool m_suppressAttributeModifiedEvent;
    269 };
    270 
    271 
    272 } // namespace WebCore
    273 
    274 #endif // !defined(InspectorDOMAgent_h)
    275