e963b819ac570eb1740534eee42501bbec80b89e
[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 "PageOverlay.h"
30 #include "WKAPICast.h"
31 #include "WKBundleAPICast.h"
32 #include <WebCore/GraphicsContext.h>
33 #include <wtf/PassOwnPtr.h>
34
35 using namespace WebCore;
36 using namespace WebKit;
37
38 class PageOverlayClientImpl : public PageOverlay::Client {
39 public:
40     static PassOwnPtr<PageOverlayClientImpl> create(WKBundlePageOverlayClient* client)
41     {
42         return adoptPtr(new PageOverlayClientImpl(client));
43     }
44
45     virtual void setAccessibilityClient(WKBundlePageOverlayAccessibilityClient* client)
46     {
47         if (client)
48             m_accessibilityClient = *client;
49     }
50
51 private:
52     explicit PageOverlayClientImpl(WKBundlePageOverlayClient* client)
53         : m_client()
54         , m_accessibilityClient()
55     {
56         if (client)
57             m_client = *client;
58     }
59
60     // PageOverlay::Client.
61     virtual void pageOverlayDestroyed(PageOverlay*)
62     {
63         delete this;
64     }
65
66     virtual void willMoveToWebPage(PageOverlay* pageOverlay, WebPage* page)
67     {
68         if (!m_client.willMoveToPage)
69             return;
70
71         m_client.willMoveToPage(toAPI(pageOverlay), toAPI(page), m_client.clientInfo);
72     }
73     
74     virtual void didMoveToWebPage(PageOverlay* pageOverlay, WebPage* page)
75     {
76         if (!m_client.didMoveToPage)
77             return;
78
79         m_client.didMoveToPage(toAPI(pageOverlay), toAPI(page), m_client.clientInfo);
80     }
81
82     virtual void drawRect(PageOverlay* pageOverlay, GraphicsContext& graphicsContext, const IntRect& dirtyRect)
83     {
84         if (!m_client.drawRect)
85             return;
86
87         m_client.drawRect(toAPI(pageOverlay), graphicsContext.platformContext(), toAPI(dirtyRect), m_client.clientInfo);
88     }
89     
90     virtual bool mouseEvent(PageOverlay* pageOverlay, const WebMouseEvent& event)
91     {
92         switch (event.type()) {
93         case WebEvent::MouseDown: {
94             if (!m_client.mouseDown)
95                 return false;
96
97             return m_client.mouseDown(toAPI(pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.clientInfo);
98         }
99         case WebEvent::MouseUp: {
100             if (!m_client.mouseUp)
101                 return false;
102
103             return m_client.mouseUp(toAPI(pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.clientInfo);
104         }
105         case WebEvent::MouseMove: {
106             if (event.button() == WebMouseEvent::NoButton) {
107                 if (!m_client.mouseMoved)
108                     return false;
109
110                 return m_client.mouseMoved(toAPI(pageOverlay), toAPI(event.position()), m_client.clientInfo);
111             }
112
113             // This is a MouseMove event with a mouse button pressed. Call mouseDragged.
114             if (!m_client.mouseDragged)
115                 return false;
116
117             return m_client.mouseDragged(toAPI(pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.clientInfo);
118         }
119
120         default:
121             return false;
122         }
123     }
124     
125     virtual WKTypeRef copyAccessibilityAttributeValue(PageOverlay* pageOverlay, WKStringRef attribute, WKTypeRef parameter)
126     {
127         if (!m_accessibilityClient.copyAccessibilityAttributeValue)
128             return 0;
129         return m_accessibilityClient.copyAccessibilityAttributeValue(toAPI(pageOverlay), attribute, parameter, m_client.clientInfo);
130     }
131
132     virtual WKArrayRef copyAccessibilityAttributeNames(PageOverlay* pageOverlay, bool paramerizedNames)
133     {
134         if (!m_accessibilityClient.copyAccessibilityAttributeNames)
135             return 0;
136         return m_accessibilityClient.copyAccessibilityAttributeNames(toAPI(pageOverlay), paramerizedNames, m_client.clientInfo);
137     }
138     
139     WKBundlePageOverlayClient m_client;
140     WKBundlePageOverlayAccessibilityClient m_accessibilityClient;
141 };
142
143 WKTypeID WKBundlePageOverlayGetTypeID()
144 {
145     return toAPI(PageOverlay::APIType);
146 }
147
148 WKBundlePageOverlayRef WKBundlePageOverlayCreate(WKBundlePageOverlayClient* wkClient)
149 {
150     if (wkClient && wkClient->version)
151         return 0;
152
153     OwnPtr<PageOverlayClientImpl> clientImpl = PageOverlayClientImpl::create(wkClient);
154
155     return toAPI(PageOverlay::create(clientImpl.leakPtr()).leakRef());
156 }
157
158 void WKBundlePageOverlaySetAccessibilityClient(WKBundlePageOverlayRef bundlePageOverlayRef, WKBundlePageOverlayAccessibilityClient* client)
159 {
160     if (client && client->version)
161         return;
162     static_cast<PageOverlayClientImpl*>(toImpl(bundlePageOverlayRef)->client())->setAccessibilityClient(client);
163 }
164
165 void WKBundlePageOverlaySetNeedsDisplay(WKBundlePageOverlayRef bundlePageOverlayRef, WKRect rect)
166 {
167     toImpl(bundlePageOverlayRef)->setNeedsDisplay(enclosingIntRect(toFloatRect(rect)));
168 }
169
170 float WKBundlePageOverlayFractionFadedIn(WKBundlePageOverlayRef)
171 {
172     // Clients who include the fade opacity during painting interfere
173     // with composited fade, so we'll pretend we're opaque and do the
174     // fade on our own.
175
176     return 1;
177 }