Page overlay action context override should indicate the source of the request
[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, WKBundlePageOverlayClientV2> 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     virtual void pageOverlayDestroyed(WebPageOverlay&) override
74     {
75         delete this;
76     }
77
78     virtual 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     virtual 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     virtual 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     virtual 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     virtual DDActionContext *actionContextForResultAtPoint(WebPageOverlay& pageOverlay, WebCore::FloatPoint location, RefPtr<WebCore::Range>& rangeHandle, bool forImmediateAction) 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, forImmediateAction ? kWKBundlePageOverlayActionContextForImmediateActionRequestType : kWKBundlePageOverlayActionContextForActionMenuRequestType, m_client.base.clientInfo);
143
144             if (apiRange)
145                 rangeHandle = toImpl(apiRange)->coreRange();
146
147             return actionContext;
148         }
149
150         if (m_client.actionContextForResultAtPoint_deprecatedForUseWithV1) {
151             WKBundleRangeHandleRef apiRange = nullptr;
152             DDActionContext *actionContext = (DDActionContext *)m_client.actionContextForResultAtPoint_deprecatedForUseWithV1(toAPI(&pageOverlay), WKPointMake(location.x(), location.y()), &apiRange, m_client.base.clientInfo);
153
154             if (apiRange)
155                 rangeHandle = toImpl(apiRange)->coreRange();
156
157             return actionContext;
158         }
159
160         return nil;
161     }
162
163     virtual void dataDetectorsDidPresentUI(WebPageOverlay& pageOverlay) override
164     {
165         if (!m_client.dataDetectorsDidPresentUI)
166             return;
167
168         m_client.dataDetectorsDidPresentUI(toAPI(&pageOverlay), m_client.base.clientInfo);
169     }
170
171     virtual void dataDetectorsDidChangeUI(WebPageOverlay& pageOverlay) override
172     {
173         if (!m_client.dataDetectorsDidChangeUI)
174             return;
175
176         m_client.dataDetectorsDidChangeUI(toAPI(&pageOverlay), m_client.base.clientInfo);
177     }
178
179     virtual void dataDetectorsDidHideUI(WebPageOverlay& pageOverlay) override
180     {
181         if (!m_client.dataDetectorsDidHideUI)
182             return;
183
184         m_client.dataDetectorsDidHideUI(toAPI(&pageOverlay), m_client.base.clientInfo);
185     }
186 #endif // PLATFORM(MAC)
187
188     virtual bool copyAccessibilityAttributeStringValueForPoint(WebPageOverlay& pageOverlay, String attribute, WebCore::FloatPoint parameter, String& value) override
189     {
190         if (!m_accessibilityClient.client().copyAccessibilityAttributeValue)
191             return false;
192         auto wkType = m_accessibilityClient.client().copyAccessibilityAttributeValue(toAPI(&pageOverlay), toCopiedAPI(attribute), WKPointCreate(WKPointMake(parameter.x(), parameter.y())), m_accessibilityClient.client().base.clientInfo);
193         if (toImpl(wkType)->type() != API::String::APIType)
194             return false;
195         value = toWTFString(static_cast<WKStringRef>(wkType));
196         return true;
197     }
198
199     virtual bool copyAccessibilityAttributeBoolValueForPoint(WebPageOverlay& pageOverlay, String attribute, WebCore::FloatPoint parameter, bool& value) override
200     {
201         if (!m_accessibilityClient.client().copyAccessibilityAttributeValue)
202             return false;
203         auto wkType = m_accessibilityClient.client().copyAccessibilityAttributeValue(toAPI(&pageOverlay), toCopiedAPI(attribute), WKPointCreate(WKPointMake(parameter.x(), parameter.y())), m_accessibilityClient.client().base.clientInfo);
204         if (toImpl(wkType)->type() != API::Boolean::APIType)
205             return false;
206         value = WKBooleanGetValue(static_cast<WKBooleanRef>(wkType));
207         return true;
208     }
209
210     virtual Vector<String> copyAccessibilityAttributeNames(WebPageOverlay& pageOverlay, bool paramerizedNames) override
211     {
212         Vector<String> names;
213         if (!m_accessibilityClient.client().copyAccessibilityAttributeNames)
214             return names;
215         auto wkNames = m_accessibilityClient.client().copyAccessibilityAttributeNames(toAPI(&pageOverlay), paramerizedNames, m_accessibilityClient.client().base.clientInfo);
216
217         size_t count = WKArrayGetSize(wkNames);
218         for (size_t k = 0; k < count; k++) {
219             WKTypeRef item = WKArrayGetItemAtIndex(wkNames, k);
220             if (toImpl(item)->type() == API::String::APIType)
221                 names.append(toWTFString(static_cast<WKStringRef>(item)));
222         }
223
224         return names;
225     }
226     
227     API::Client<WKBundlePageOverlayAccessibilityClientBase> m_accessibilityClient;
228 };
229
230 WKTypeID WKBundlePageOverlayGetTypeID()
231 {
232     return toAPI(WebPageOverlay::APIType);
233 }
234
235 WKBundlePageOverlayRef WKBundlePageOverlayCreate(WKBundlePageOverlayClientBase* wkClient)
236 {
237     auto clientImpl = std::make_unique<PageOverlayClientImpl>(wkClient);
238     return toAPI(WebPageOverlay::create(WTF::move(clientImpl)).leakRef());
239 }
240
241 void WKBundlePageOverlaySetAccessibilityClient(WKBundlePageOverlayRef bundlePageOverlayRef, WKBundlePageOverlayAccessibilityClientBase* client)
242 {
243     static_cast<PageOverlayClientImpl&>(toImpl(bundlePageOverlayRef)->client()).setAccessibilityClient(client);
244 }
245
246 void WKBundlePageOverlaySetNeedsDisplay(WKBundlePageOverlayRef bundlePageOverlayRef, WKRect rect)
247 {
248     toImpl(bundlePageOverlayRef)->setNeedsDisplay(enclosingIntRect(toFloatRect(rect)));
249 }
250
251 float WKBundlePageOverlayFractionFadedIn(WKBundlePageOverlayRef)
252 {
253     // Clients who include the fade opacity during painting interfere
254     // with composited fade, so we'll pretend we're opaque and do the
255     // fade on our own.
256
257     return 1;
258 }
259
260 void WKBundlePageOverlayClear(WKBundlePageOverlayRef bundlePageOverlayRef)
261 {
262     toImpl(bundlePageOverlayRef)->clear();
263 }