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