Remove "virtual" from all lines that have both "virtual" and "override".
[WebKit-https.git] / Source / WebKit2 / WebProcess / InjectedBundle / API / c / WKBundlePageOverlay.cpp
1 /*
2  * Copyright (C) 2010 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 "WKBundlePageOverlay.h"
28
29 #include "APIClient.h"
30 #include "InjectedBundleRangeHandle.h"
31 #include "WKAPICast.h"
32 #include "WKArray.h"
33 #include "WKBundleAPICast.h"
34 #include "WKNumber.h"
35 #include "WKRetainPtr.h"
36 #include "WKSharedAPICast.h"
37 #include "WKStringPrivate.h"
38 #include "WebPage.h"
39 #include <WebCore/GraphicsContext.h>
40 #include <WebCore/PageOverlay.h>
41 #include <WebCore/PlatformMouseEvent.h>
42 #include <WebKit/WebPageOverlay.h>
43
44 namespace API {
45
46 template<> struct ClientTraits<WKBundlePageOverlayClientBase> {
47     typedef std::tuple<WKBundlePageOverlayClientV0, WKBundlePageOverlayClientV1> Versions;
48 };
49
50 template<> struct ClientTraits<WKBundlePageOverlayAccessibilityClientBase> {
51     typedef std::tuple<WKBundlePageOverlayAccessibilityClientV0> Versions;
52 };
53
54 }
55
56 using namespace WebCore;
57 using namespace WebKit;
58
59 class PageOverlayClientImpl : API::Client<WKBundlePageOverlayClientBase>, public WebPageOverlay::Client {
60 public:
61     explicit PageOverlayClientImpl(WKBundlePageOverlayClientBase* client)
62     {
63         initialize(client);
64     }
65
66     virtual void setAccessibilityClient(WKBundlePageOverlayAccessibilityClientBase* client)
67     {
68         m_accessibilityClient.initialize(client);
69     }
70
71 private:
72     // WebPageOverlay::Client.
73     void pageOverlayDestroyed(WebPageOverlay&) override
74     {
75         delete this;
76     }
77
78     void willMoveToPage(WebPageOverlay& pageOverlay, WebPage* page) override
79     {
80         if (!m_client.willMoveToPage)
81             return;
82
83         m_client.willMoveToPage(toAPI(&pageOverlay), toAPI(page), m_client.base.clientInfo);
84     }
85     
86     void didMoveToPage(WebPageOverlay& pageOverlay, WebPage* page) override
87     {
88         if (!m_client.didMoveToPage)
89             return;
90
91         m_client.didMoveToPage(toAPI(&pageOverlay), toAPI(page), m_client.base.clientInfo);
92     }
93
94     void drawRect(WebPageOverlay& pageOverlay, GraphicsContext& graphicsContext, const IntRect& dirtyRect) override
95     {
96         if (!m_client.drawRect)
97             return;
98
99         m_client.drawRect(toAPI(&pageOverlay), graphicsContext.platformContext(), toAPI(dirtyRect), m_client.base.clientInfo);
100     }
101     
102     bool mouseEvent(WebPageOverlay& pageOverlay, const PlatformMouseEvent& event) override
103     {
104         switch (event.type()) {
105         case PlatformMouseEvent::Type::MousePressed: {
106             if (!m_client.mouseDown)
107                 return false;
108
109             return m_client.mouseDown(toAPI(&pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.base.clientInfo);
110         }
111         case PlatformMouseEvent::Type::MouseReleased: {
112             if (!m_client.mouseUp)
113                 return false;
114
115             return m_client.mouseUp(toAPI(&pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.base.clientInfo);
116         }
117         case PlatformMouseEvent::Type::MouseMoved: {
118             if (event.button() == MouseButton::NoButton) {
119                 if (!m_client.mouseMoved)
120                     return false;
121
122                 return m_client.mouseMoved(toAPI(&pageOverlay), toAPI(event.position()), m_client.base.clientInfo);
123             }
124
125             // This is a MouseMove event with a mouse button pressed. Call mouseDragged.
126             if (!m_client.mouseDragged)
127                 return false;
128
129             return m_client.mouseDragged(toAPI(&pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.base.clientInfo);
130         }
131
132         default:
133             return false;
134         }
135     }
136
137 #if PLATFORM(MAC)
138     DDActionContext *actionContextForResultAtPoint(WebPageOverlay& pageOverlay, WebCore::FloatPoint location, RefPtr<WebCore::Range>& rangeHandle) override
139     {
140         if (m_client.actionContextForResultAtPoint) {
141             WKBundleRangeHandleRef apiRange = nullptr;
142             DDActionContext *actionContext = (DDActionContext *)m_client.actionContextForResultAtPoint(toAPI(&pageOverlay), WKPointMake(location.x(), location.y()), &apiRange, m_client.base.clientInfo);
143
144             if (apiRange)
145                 rangeHandle = toImpl(apiRange)->coreRange();
146
147             return actionContext;
148         }
149
150         return nil;
151     }
152
153     void dataDetectorsDidPresentUI(WebPageOverlay& pageOverlay) override
154     {
155         if (!m_client.dataDetectorsDidPresentUI)
156             return;
157
158         m_client.dataDetectorsDidPresentUI(toAPI(&pageOverlay), m_client.base.clientInfo);
159     }
160
161     void dataDetectorsDidChangeUI(WebPageOverlay& pageOverlay) override
162     {
163         if (!m_client.dataDetectorsDidChangeUI)
164             return;
165
166         m_client.dataDetectorsDidChangeUI(toAPI(&pageOverlay), m_client.base.clientInfo);
167     }
168
169     void dataDetectorsDidHideUI(WebPageOverlay& pageOverlay) override
170     {
171         if (!m_client.dataDetectorsDidHideUI)
172             return;
173
174         m_client.dataDetectorsDidHideUI(toAPI(&pageOverlay), m_client.base.clientInfo);
175     }
176 #endif // PLATFORM(MAC)
177
178     bool copyAccessibilityAttributeStringValueForPoint(WebPageOverlay& pageOverlay, String attribute, WebCore::FloatPoint parameter, String& value) override
179     {
180         if (!m_accessibilityClient.client().copyAccessibilityAttributeValue)
181             return false;
182         auto wkType = m_accessibilityClient.client().copyAccessibilityAttributeValue(toAPI(&pageOverlay), toCopiedAPI(attribute), WKPointCreate(WKPointMake(parameter.x(), parameter.y())), m_accessibilityClient.client().base.clientInfo);
183         if (toImpl(wkType)->type() != API::String::APIType)
184             return false;
185         value = toWTFString(static_cast<WKStringRef>(wkType));
186         return true;
187     }
188
189     bool copyAccessibilityAttributeBoolValueForPoint(WebPageOverlay& pageOverlay, String attribute, WebCore::FloatPoint parameter, bool& value) override
190     {
191         if (!m_accessibilityClient.client().copyAccessibilityAttributeValue)
192             return false;
193         auto wkType = m_accessibilityClient.client().copyAccessibilityAttributeValue(toAPI(&pageOverlay), toCopiedAPI(attribute), WKPointCreate(WKPointMake(parameter.x(), parameter.y())), m_accessibilityClient.client().base.clientInfo);
194         if (toImpl(wkType)->type() != API::Boolean::APIType)
195             return false;
196         value = WKBooleanGetValue(static_cast<WKBooleanRef>(wkType));
197         return true;
198     }
199
200     Vector<String> copyAccessibilityAttributeNames(WebPageOverlay& pageOverlay, bool paramerizedNames) override
201     {
202         Vector<String> names;
203         if (!m_accessibilityClient.client().copyAccessibilityAttributeNames)
204             return names;
205         auto wkNames = m_accessibilityClient.client().copyAccessibilityAttributeNames(toAPI(&pageOverlay), paramerizedNames, m_accessibilityClient.client().base.clientInfo);
206
207         size_t count = WKArrayGetSize(wkNames);
208         for (size_t k = 0; k < count; k++) {
209             WKTypeRef item = WKArrayGetItemAtIndex(wkNames, k);
210             if (toImpl(item)->type() == API::String::APIType)
211                 names.append(toWTFString(static_cast<WKStringRef>(item)));
212         }
213
214         return names;
215     }
216     
217     API::Client<WKBundlePageOverlayAccessibilityClientBase> m_accessibilityClient;
218 };
219
220 WKTypeID WKBundlePageOverlayGetTypeID()
221 {
222     return toAPI(WebPageOverlay::APIType);
223 }
224
225 WKBundlePageOverlayRef WKBundlePageOverlayCreate(WKBundlePageOverlayClientBase* wkClient)
226 {
227     auto clientImpl = std::make_unique<PageOverlayClientImpl>(wkClient);
228     return toAPI(&WebPageOverlay::create(WTFMove(clientImpl)).leakRef());
229 }
230
231 void WKBundlePageOverlaySetAccessibilityClient(WKBundlePageOverlayRef bundlePageOverlayRef, WKBundlePageOverlayAccessibilityClientBase* client)
232 {
233     static_cast<PageOverlayClientImpl&>(toImpl(bundlePageOverlayRef)->client()).setAccessibilityClient(client);
234 }
235
236 void WKBundlePageOverlaySetNeedsDisplay(WKBundlePageOverlayRef bundlePageOverlayRef, WKRect rect)
237 {
238     toImpl(bundlePageOverlayRef)->setNeedsDisplay(enclosingIntRect(toFloatRect(rect)));
239 }
240
241 float WKBundlePageOverlayFractionFadedIn(WKBundlePageOverlayRef)
242 {
243     // Clients who include the fade opacity during painting interfere
244     // with composited fade, so we'll pretend we're opaque and do the
245     // fade on our own.
246
247     return 1;
248 }
249
250 void WKBundlePageOverlayClear(WKBundlePageOverlayRef bundlePageOverlayRef)
251 {
252     toImpl(bundlePageOverlayRef)->clear();
253 }