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/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 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     virtual void getBoxModel(ErrorString*, int nodeId, RefPtr<TypeBuilder::DOM::BoxModel>&);
    152     virtual void getNodeForLocation(ErrorString*, int x, int y, int* nodeId);
    153     virtual void getRelayoutBoundary(ErrorString*, int nodeId, int* relayoutBoundaryNodeId);
    154 
    155     static void getEventListeners(Node*, Vector<EventListenerInfo>& listenersArray, bool includeAncestors);
    156 
    157     // Methods called from the InspectorInstrumentation.
    158     void setDocument(Document*);
    159     void releaseDanglingNodes();
    160 
    161     void domContentLoadedEventFired(Frame*);
    162     void didCommitLoad(Frame*, DocumentLoader*);
    163 
    164     void didInsertDOMNode(Node*);
    165     void willRemoveDOMNode(Node*);
    166     void willModifyDOMAttr(Element*, const AtomicString& oldValue, const AtomicString& newValue);
    167     void didModifyDOMAttr(Element*, const AtomicString& name, const AtomicString& value);
    168     void didRemoveDOMAttr(Element*, const AtomicString& name);
    169     void styleAttributeInvalidated(const Vector<Element*>& elements);
    170     void characterDataModified(CharacterData*);
    171     void didInvalidateStyleAttr(Node*);
    172     void didPushShadowRoot(Element* host, ShadowRoot*);
    173     void willPopShadowRoot(Element* host, ShadowRoot*);
    174     void frameDocumentUpdated(Frame*);
    175     void pseudoElementCreated(PseudoElement*);
    176     void pseudoElementDestroyed(PseudoElement*);
    177 
    178     int pushNodeToFrontend(ErrorString*, int documentNodeId, Node*);
    179     Node* nodeForId(int nodeId);
    180     int boundNodeId(Node*);
    181     void setDOMListener(DOMListener*);
    182     BackendNodeId backendNodeIdForNode(Node*, const String& nodeGroup);
    183 
    184     static String documentURLString(Document*);
    185 
    186     PassRefPtr<TypeBuilder::Runtime::RemoteObject> resolveNode(Node*, const String& objectGroup);
    187     bool handleMousePress();
    188     bool handleGestureEvent(Frame*, const PlatformGestureEvent&);
    189     bool handleTouchEvent(Frame*, const PlatformTouchEvent&);
    190     void handleMouseMove(Frame*, const PlatformMouseEvent&);
    191 
    192     InspectorHistory* history() { return m_history.get(); }
    193 
    194     // We represent embedded doms as a part of the same hierarchy. Hence we treat children of frame owners differently.
    195     // We also skip whitespace text nodes conditionally. Following methods encapsulate these specifics.
    196     static Node* innerFirstChild(Node*);
    197     static Node* innerNextSibling(Node*);
    198     static Node* innerPreviousSibling(Node*);
    199     static unsigned innerChildNodeCount(Node*);
    200     static Node* innerParentNode(Node*);
    201     static bool isWhitespace(Node*);
    202 
    203     Node* assertNode(ErrorString*, int nodeId);
    204     Element* assertElement(ErrorString*, int nodeId);
    205     Document* assertDocument(ErrorString*, int nodeId);
    206 
    207 private:
    208     enum SearchMode { NotSearching, SearchingForNormal, SearchingForShadow };
    209 
    210     InspectorDOMAgent(InstrumentingAgents*, InspectorPageAgent*, InspectorCompositeState*, InjectedScriptManager*, InspectorOverlay*, InspectorClient*);
    211 
    212     void setSearchingForNode(ErrorString*, SearchMode, JSONObject* highlightConfig);
    213     PassOwnPtr<HighlightConfig> highlightConfigFromInspectorObject(ErrorString*, JSONObject* highlightInspectorObject);
    214 
    215     // Node-related methods.
    216     typedef HashMap<RefPtr<Node>, int> NodeToIdMap;
    217     int bind(Node*, NodeToIdMap*);
    218     void unbind(Node*, NodeToIdMap*);
    219 
    220     Node* assertEditableNode(ErrorString*, int nodeId);
    221     Element* assertEditableElement(ErrorString*, int nodeId);
    222 
    223     void inspect(Node*);
    224 
    225     int pushNodePathToFrontend(Node*);
    226     void pushChildNodesToFrontend(int nodeId, int depth = 1);
    227 
    228     void invalidateFrameOwnerElement(Frame*);
    229 
    230     bool hasBreakpoint(Node*, int type);
    231     void updateSubtreeBreakpoints(Node* root, uint32_t rootMask, bool value);
    232     void descriptionForDOMEvent(Node* target, int breakpointType, bool insertion, PassRefPtr<JSONObject> description);
    233 
    234     PassRefPtr<TypeBuilder::DOM::Node> buildObjectForNode(Node*, int depth, NodeToIdMap*);
    235     PassRefPtr<TypeBuilder::Array<String> > buildArrayForElementAttributes(Element*);
    236     PassRefPtr<TypeBuilder::Array<TypeBuilder::DOM::Node> > buildArrayForContainerChildren(Node* container, int depth, NodeToIdMap* nodesMap);
    237     PassRefPtr<TypeBuilder::DOM::EventListener> buildObjectForEventListener(const RegisteredEventListener&, const AtomicString& eventType, Node*, const String* objectGroupId);
    238     PassRefPtr<TypeBuilder::Array<TypeBuilder::DOM::Node> > buildArrayForPseudoElements(Element*, NodeToIdMap* nodesMap);
    239 
    240     Node* nodeForPath(const String& path);
    241 
    242     void discardBackendBindings();
    243     void discardFrontendBindings();
    244 
    245     void innerHighlightQuad(PassOwnPtr<FloatQuad>, const RefPtr<JSONObject>* color, const RefPtr<JSONObject>* outlineColor);
    246 
    247     bool pushDocumentUponHandlelessOperation(ErrorString*);
    248 
    249     InspectorPageAgent* m_pageAgent;
    250     InjectedScriptManager* m_injectedScriptManager;
    251     InspectorOverlay* m_overlay;
    252     InspectorClient* m_client;
    253     InspectorFrontend::DOM* m_frontend;
    254     DOMListener* m_domListener;
    255     NodeToIdMap m_documentNodeToIdMap;
    256     typedef HashMap<RefPtr<Node>, BackendNodeId> NodeToBackendIdMap;
    257     HashMap<String, NodeToBackendIdMap> m_nodeGroupToBackendIdMap;
    258     // Owns node mappings for dangling nodes.
    259     Vector<OwnPtr<NodeToIdMap> > m_danglingNodeToIdMaps;
    260     HashMap<int, Node*> m_idToNode;
    261     HashMap<int, NodeToIdMap*> m_idToNodesMap;
    262     HashSet<int> m_childrenRequested;
    263     HashMap<BackendNodeId, std::pair<Node*, String> > m_backendIdToNode;
    264     int m_lastNodeId;
    265     BackendNodeId m_lastBackendNodeId;
    266     RefPtr<Document> m_document;
    267     typedef HashMap<String, Vector<RefPtr<Node> > > SearchResults;
    268     SearchResults m_searchResults;
    269     OwnPtr<RevalidateStyleAttributeTask> m_revalidateStyleAttrTask;
    270     SearchMode m_searchingForNode;
    271     OwnPtr<HighlightConfig> m_inspectModeHighlightConfig;
    272     OwnPtr<InspectorHistory> m_history;
    273     OwnPtr<DOMEditor> m_domEditor;
    274     bool m_suppressAttributeModifiedEvent;
    275 };
    276 
    277 
    278 } // namespace WebCore
    279 
    280 #endif // !defined(InspectorDOMAgent_h)
    281