1/*
2 * Copyright (C) 2014 Apple 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "Frame.h"
29#include "Settings.h"
30#include <wtf/HashMap.h>
31#include <wtf/Vector.h>
32
33namespace WebCore {
34
35class Page;
36class RegionOverlay;
37
38class DebugPageOverlays {
39public:
40 static DebugPageOverlays& singleton();
41
42 enum class RegionType {
43 WheelEventHandlers,
44 NonFastScrollableRegion,
45 };
46 static const unsigned NumberOfRegionTypes = NonFastScrollableRegion + 1;
47
48 static void didLayout(Frame&);
49 static void didChangeEventHandlers(Frame&);
50
51 WEBCORE_EXPORT static void settingsChanged(Page&);
52
53private:
54 static bool hasOverlays(Page&);
55
56 void showRegionOverlay(Page&, RegionType);
57 void hideRegionOverlay(Page&, RegionType);
58
59 void regionChanged(Frame&, RegionType);
60
61 bool hasOverlaysForPage(Page& page) const
62 {
63 return m_pageRegionOverlays.contains(&page);
64 }
65
66 void updateOverlayRegionVisibility(Page&, DebugOverlayRegions);
67
68 RegionOverlay* regionOverlayForPage(Page&, RegionType) const;
69 RegionOverlay& ensureRegionOverlayForPage(Page&, RegionType);
70
71 HashMap<Page*, Vector<RefPtr<RegionOverlay>>> m_pageRegionOverlays;
72
73 static DebugPageOverlays* sharedDebugOverlays;
74};
75
76#define FAST_RETURN_IF_NO_OVERLAYS(page) if (LIKELY(!page || !hasOverlays(*page))) return;
77
78inline bool DebugPageOverlays::hasOverlays(Page& page)
79{
80 if (!sharedDebugOverlays)
81 return false;
82
83 return sharedDebugOverlays->hasOverlaysForPage(page);
84}
85
86inline void DebugPageOverlays::didLayout(Frame& frame)
87{
88 FAST_RETURN_IF_NO_OVERLAYS(frame.page());
89
90 sharedDebugOverlays->regionChanged(frame, RegionType::WheelEventHandlers);
91 sharedDebugOverlays->regionChanged(frame, RegionType::NonFastScrollableRegion);
92}
93
94inline void DebugPageOverlays::didChangeEventHandlers(Frame& frame)
95{
96 FAST_RETURN_IF_NO_OVERLAYS(frame.page());
97
98 sharedDebugOverlays->regionChanged(frame, RegionType::WheelEventHandlers);
99 sharedDebugOverlays->regionChanged(frame, RegionType::NonFastScrollableRegion);
100}
101
102} // namespace WebCore
103