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#include "config.h"
27#include "MockPageOverlayClient.h"
28
29#include "Document.h"
30#include "Frame.h"
31#include "GraphicsContext.h"
32#include "GraphicsLayer.h"
33#include "Page.h"
34#include "PageOverlayController.h"
35#include "PlatformMouseEvent.h"
36#include <wtf/NeverDestroyed.h>
37#include <wtf/text/StringBuilder.h>
38
39namespace WebCore {
40
41MockPageOverlayClient& MockPageOverlayClient::singleton()
42{
43 static NeverDestroyed<MockPageOverlayClient> sharedClient;
44 return sharedClient.get();
45}
46
47MockPageOverlayClient::MockPageOverlayClient() = default;
48
49Ref<MockPageOverlay> MockPageOverlayClient::installOverlay(Page& page, PageOverlay::OverlayType overlayType)
50{
51 auto overlay = PageOverlay::create(*this, overlayType);
52 page.pageOverlayController().installPageOverlay(overlay, PageOverlay::FadeMode::DoNotFade);
53
54 auto mockOverlay = MockPageOverlay::create(overlay.ptr());
55 m_overlays.add(mockOverlay.ptr());
56
57 return mockOverlay;
58}
59
60void MockPageOverlayClient::uninstallAllOverlays()
61{
62 while (!m_overlays.isEmpty()) {
63 RefPtr<MockPageOverlay> mockOverlay = m_overlays.takeAny();
64 PageOverlayController* overlayController = mockOverlay->overlay()->controller();
65 ASSERT(overlayController);
66 overlayController->uninstallPageOverlay(*mockOverlay->overlay(), PageOverlay::FadeMode::DoNotFade);
67 }
68}
69
70String MockPageOverlayClient::layerTreeAsText(Page& page, LayerTreeFlags flags)
71{
72 GraphicsLayer* viewOverlayRoot = page.pageOverlayController().viewOverlayRootLayer();
73 GraphicsLayer* documentOverlayRoot = page.pageOverlayController().documentOverlayRootLayer();
74
75 return "View-relative:\n" + (viewOverlayRoot ? viewOverlayRoot->layerTreeAsText(flags | LayerTreeAsTextIncludePageOverlayLayers) : "(no view-relative overlay root)")
76 + "\n\nDocument-relative:\n" + (documentOverlayRoot ? documentOverlayRoot->layerTreeAsText(flags | LayerTreeAsTextIncludePageOverlayLayers) : "(no document-relative overlay root)");
77}
78
79void MockPageOverlayClient::willMoveToPage(PageOverlay&, Page*)
80{
81}
82
83void MockPageOverlayClient::didMoveToPage(PageOverlay& overlay, Page* page)
84{
85 if (page)
86 overlay.setNeedsDisplay();
87}
88
89void MockPageOverlayClient::drawRect(PageOverlay& overlay, GraphicsContext& context, const IntRect& dirtyRect)
90{
91 StringBuilder message;
92 message.appendLiteral("MockPageOverlayClient::drawRect dirtyRect (");
93 message.appendNumber(dirtyRect.x());
94 message.appendLiteral(", ");
95 message.appendNumber(dirtyRect.y());
96 message.appendLiteral(", ");
97 message.appendNumber(dirtyRect.width());
98 message.appendLiteral(", ");
99 message.appendNumber(dirtyRect.height());
100 message.appendLiteral(")");
101 overlay.page()->mainFrame().document()->addConsoleMessage(MessageSource::Other, MessageLevel::Debug, message.toString());
102
103 GraphicsContextStateSaver stateSaver(context);
104
105 FloatRect insetRect = overlay.bounds();
106
107 if (overlay.overlayType() == PageOverlay::OverlayType::Document) {
108 context.setStrokeColor(Color(0, 255, 0));
109 insetRect.inflate(-50);
110 } else {
111 context.setStrokeColor(Color(0, 0, 255));
112 insetRect.inflate(-20);
113 }
114
115 context.strokeRect(insetRect, 20);
116}
117
118bool MockPageOverlayClient::mouseEvent(PageOverlay& overlay, const PlatformMouseEvent& event)
119{
120 StringBuilder message;
121 message.appendLiteral("MockPageOverlayClient::mouseEvent location (");
122 message.appendNumber(event.position().x());
123 message.appendLiteral(", ");
124 message.appendNumber(event.position().y());
125 message.appendLiteral(")");
126 overlay.page()->mainFrame().document()->addConsoleMessage(MessageSource::Other, MessageLevel::Debug, message.toString());
127
128 return false;
129}
130
131void MockPageOverlayClient::didScrollFrame(PageOverlay&, Frame&)
132{
133}
134
135bool MockPageOverlayClient::copyAccessibilityAttributeStringValueForPoint(PageOverlay&, String /* attribute */, FloatPoint, String&)
136{
137 return false;
138}
139
140bool MockPageOverlayClient::copyAccessibilityAttributeBoolValueForPoint(PageOverlay&, String /* attribute */, FloatPoint, bool&)
141{
142 return false;
143}
144
145Vector<String> MockPageOverlayClient::copyAccessibilityAttributeNames(PageOverlay&, bool /* parameterizedNames */)
146{
147 return Vector<String>();
148}
149
150}
151