Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2011 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #ifndef InspectorOverlay_h
     30 #define InspectorOverlay_h
     31 
     32 #include "platform/Timer.h"
     33 #include "platform/geometry/FloatQuad.h"
     34 #include "platform/geometry/LayoutRect.h"
     35 #include "platform/graphics/Color.h"
     36 #include "wtf/OwnPtr.h"
     37 #include "wtf/PassOwnPtr.h"
     38 #include "wtf/RefPtr.h"
     39 #include "wtf/Vector.h"
     40 #include "wtf/text/WTFString.h"
     41 
     42 namespace WebCore {
     43 
     44 class Color;
     45 class EmptyChromeClient;
     46 class GraphicsContext;
     47 class InspectorClient;
     48 class InspectorOverlayHost;
     49 class JSONValue;
     50 class Node;
     51 class Page;
     52 class PlatformGestureEvent;
     53 class PlatformKeyboardEvent;
     54 class PlatformMouseEvent;
     55 class PlatformTouchEvent;
     56 
     57 struct HighlightConfig {
     58     WTF_MAKE_FAST_ALLOCATED;
     59 public:
     60     Color content;
     61     Color contentOutline;
     62     Color padding;
     63     Color border;
     64     Color margin;
     65     Color eventTarget;
     66     bool showInfo;
     67     bool showRulers;
     68 };
     69 
     70 enum HighlightType {
     71     HighlightTypeNode,
     72     HighlightTypeRects,
     73 };
     74 
     75 struct Highlight {
     76     Highlight()
     77         : type(HighlightTypeNode)
     78         , showRulers(false)
     79     {
     80     }
     81 
     82     void setDataFromConfig(const HighlightConfig& highlightConfig)
     83     {
     84         contentColor = highlightConfig.content;
     85         contentOutlineColor = highlightConfig.contentOutline;
     86         paddingColor = highlightConfig.padding;
     87         borderColor = highlightConfig.border;
     88         marginColor = highlightConfig.margin;
     89         eventTargetColor = highlightConfig.eventTarget;
     90         showRulers = highlightConfig.showRulers;
     91     }
     92 
     93     Color contentColor;
     94     Color contentOutlineColor;
     95     Color paddingColor;
     96     Color borderColor;
     97     Color marginColor;
     98     Color eventTargetColor;
     99 
    100     // When the type is Node, there are 4 or 5 quads (margin, border, padding, content, optional eventTarget).
    101     // When the type is Rects, this is just a list of quads.
    102     HighlightType type;
    103     Vector<FloatQuad> quads;
    104     bool showRulers;
    105 };
    106 
    107 class InspectorOverlay {
    108     WTF_MAKE_FAST_ALLOCATED;
    109 public:
    110     static PassOwnPtr<InspectorOverlay> create(Page* page, InspectorClient* client)
    111     {
    112         return adoptPtr(new InspectorOverlay(page, client));
    113     }
    114 
    115     ~InspectorOverlay();
    116 
    117     void update();
    118     void hide();
    119     void paint(GraphicsContext&);
    120     void drawOutline(GraphicsContext*, const LayoutRect&, const Color&);
    121     void getHighlight(Highlight*) const;
    122     void resize(const IntSize&);
    123     bool handleGestureEvent(const PlatformGestureEvent&);
    124     bool handleMouseEvent(const PlatformMouseEvent&);
    125     bool handleTouchEvent(const PlatformTouchEvent&);
    126     bool handleKeyboardEvent(const PlatformKeyboardEvent&);
    127 
    128     void setPausedInDebuggerMessage(const String*);
    129     void setInspectModeEnabled(bool);
    130 
    131     void hideHighlight();
    132     void highlightNode(Node*, Node* eventTarget, const HighlightConfig&);
    133     void highlightQuad(PassOwnPtr<FloatQuad>, const HighlightConfig&);
    134     void showAndHideViewSize(bool showGrid);
    135 
    136     Node* highlightedNode() const;
    137     bool getBoxModel(Node*, Vector<FloatQuad>*);
    138 
    139     void freePage();
    140 
    141     InspectorOverlayHost* overlayHost() const { return m_overlayHost.get(); }
    142 
    143     void startedRecordingProfile();
    144     void finishedRecordingProfile() { m_activeProfilerCount--; }
    145 
    146     // Methods supporting underlying overlay page.
    147     void invalidate();
    148 private:
    149     InspectorOverlay(Page*, InspectorClient*);
    150 
    151     bool isEmpty();
    152 
    153     void drawNodeHighlight();
    154     void drawQuadHighlight();
    155     void drawPausedInDebuggerMessage();
    156     void drawViewSize();
    157 
    158     Page* overlayPage();
    159     void reset(const IntSize& viewportSize, const IntSize& frameViewFullSize, int scrollX, int scrollY);
    160     void evaluateInOverlay(const String& method, const String& argument);
    161     void evaluateInOverlay(const String& method, PassRefPtr<JSONValue> argument);
    162     void onTimer(Timer<InspectorOverlay>*);
    163 
    164     Page* m_page;
    165     InspectorClient* m_client;
    166     String m_pausedInDebuggerMessage;
    167     bool m_inspectModeEnabled;
    168     RefPtr<Node> m_highlightNode;
    169     RefPtr<Node> m_eventTargetNode;
    170     HighlightConfig m_nodeHighlightConfig;
    171     OwnPtr<FloatQuad> m_highlightQuad;
    172     OwnPtr<Page> m_overlayPage;
    173     OwnPtr<EmptyChromeClient> m_overlayChromeClient;
    174     RefPtr<InspectorOverlayHost> m_overlayHost;
    175     HighlightConfig m_quadHighlightConfig;
    176     IntSize m_size;
    177     bool m_drawViewSize;
    178     bool m_drawViewSizeWithGrid;
    179     Timer<InspectorOverlay> m_timer;
    180     int m_activeProfilerCount;
    181 };
    182 
    183 } // namespace WebCore
    184 
    185 
    186 #endif // InspectorOverlay_h
    187