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 "core/platform/graphics/Color.h"
     33 #include "core/platform/graphics/FloatQuad.h"
     34 #include "core/platform/graphics/LayoutRect.h"
     35 #include "core/platform/Timer.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 PlatformMouseEvent;
     54 class PlatformTouchEvent;
     55 
     56 struct HighlightConfig {
     57     WTF_MAKE_FAST_ALLOCATED;
     58 public:
     59     Color content;
     60     Color contentOutline;
     61     Color padding;
     62     Color border;
     63     Color margin;
     64     Color eventTarget;
     65     bool showInfo;
     66     bool showRulers;
     67 };
     68 
     69 enum HighlightType {
     70     HighlightTypeNode,
     71     HighlightTypeRects,
     72 };
     73 
     74 struct Highlight {
     75     Highlight()
     76         : type(HighlightTypeNode)
     77         , showRulers(false)
     78     {
     79     }
     80 
     81     void setDataFromConfig(const HighlightConfig& highlightConfig)
     82     {
     83         contentColor = highlightConfig.content;
     84         contentOutlineColor = highlightConfig.contentOutline;
     85         paddingColor = highlightConfig.padding;
     86         borderColor = highlightConfig.border;
     87         marginColor = highlightConfig.margin;
     88         eventTargetColor = highlightConfig.eventTarget;
     89         showRulers = highlightConfig.showRulers;
     90     }
     91 
     92     Color contentColor;
     93     Color contentOutlineColor;
     94     Color paddingColor;
     95     Color borderColor;
     96     Color marginColor;
     97     Color eventTargetColor;
     98 
     99     // When the type is Node, there are 4 or 5 quads (margin, border, padding, content, optional eventTarget).
    100     // When the type is Rects, this is just a list of quads.
    101     HighlightType type;
    102     Vector<FloatQuad> quads;
    103     bool showRulers;
    104 };
    105 
    106 class InspectorOverlay {
    107     WTF_MAKE_FAST_ALLOCATED;
    108 public:
    109     // This must be kept in sync with the overrideEntries array in InspectorOverlayPage.html.
    110     enum OverrideType {
    111         UserAgentOverride = 1,
    112         DeviceMetricsOverride = 1 << 1,
    113         GeolocationOverride = 1 << 2,
    114         DeviceOrientationOverride = 1 << 3,
    115         TouchOverride = 1 << 4,
    116         CSSMediaOverride = 1 << 5
    117     };
    118     static PassOwnPtr<InspectorOverlay> create(Page* page, InspectorClient* client)
    119     {
    120         return adoptPtr(new InspectorOverlay(page, client));
    121     }
    122     ~InspectorOverlay();
    123 
    124     void update();
    125     void hide();
    126     void paint(GraphicsContext&);
    127     void drawOutline(GraphicsContext*, const LayoutRect&, const Color&);
    128     void getHighlight(Highlight*) const;
    129     void resize(const IntSize&);
    130     bool handleGestureEvent(const PlatformGestureEvent&);
    131     bool handleMouseEvent(const PlatformMouseEvent&);
    132     bool handleTouchEvent(const PlatformTouchEvent&);
    133 
    134     void setPausedInDebuggerMessage(const String*);
    135     void setInspectModeEnabled(bool);
    136     void setOverride(OverrideType, bool);
    137     void setOverridesTopOffset(int);
    138 
    139     void hideHighlight();
    140     void highlightNode(Node*, Node* eventTarget, const HighlightConfig&);
    141     void highlightQuad(PassOwnPtr<FloatQuad>, const HighlightConfig&);
    142     void showAndHideViewSize(bool showGrid);
    143 
    144     Node* highlightedNode() const;
    145 
    146     void freePage();
    147 
    148     InspectorOverlayHost* overlayHost() const { return m_overlayHost.get(); }
    149 
    150     // Methods supporting underlying overlay page.
    151     void invalidate();
    152 private:
    153     InspectorOverlay(Page*, InspectorClient*);
    154 
    155     bool isEmpty();
    156 
    157     void drawGutter();
    158     void drawNodeHighlight();
    159     void drawQuadHighlight();
    160     void drawPausedInDebuggerMessage();
    161     void drawViewSize();
    162     void drawOverridesMessage();
    163 
    164     Page* overlayPage();
    165     void reset(const IntSize& viewportSize, const IntSize& frameViewFullSize, int scrollX, int scrollY);
    166     void evaluateInOverlay(const String& method, const String& argument);
    167     void evaluateInOverlay(const String& method, PassRefPtr<JSONValue> argument);
    168     void onTimer(Timer<InspectorOverlay>*);
    169 
    170     Page* m_page;
    171     InspectorClient* m_client;
    172     String m_pausedInDebuggerMessage;
    173     bool m_inspectModeEnabled;
    174     RefPtr<Node> m_highlightNode;
    175     RefPtr<Node> m_eventTargetNode;
    176     HighlightConfig m_nodeHighlightConfig;
    177     OwnPtr<FloatQuad> m_highlightQuad;
    178     OwnPtr<Page> m_overlayPage;
    179     OwnPtr<EmptyChromeClient> m_overlayChromeClient;
    180     RefPtr<InspectorOverlayHost> m_overlayHost;
    181     HighlightConfig m_quadHighlightConfig;
    182     IntSize m_size;
    183     bool m_drawViewSize;
    184     bool m_drawViewSizeWithGrid;
    185     Timer<InspectorOverlay> m_timer;
    186     unsigned m_overrides;
    187     int m_overridesTopOffset;
    188 };
    189 
    190 } // namespace WebCore
    191 
    192 
    193 #endif // InspectorOverlay_h
    194