48d945485c3106e6b949f530afa70fcb23135c74
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / efl / PageUIClientEfl.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics. All rights reserved.
3  * Copyright (C) 2012 Intel Corporation. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "PageUIClientEfl.h"
29
30 #include "EwkViewImpl.h"
31 #include "WKAPICast.h"
32 #include "WKEvent.h"
33 #include "WKString.h"
34 #include <Ecore_Evas.h>
35 #include <WebCore/Color.h>
36
37 #if OS(TIZEN)
38 #include "WKGeolocationPermissionRequest.h"
39 #include "WKNotificationPermissionRequest.h"
40 #include "WKPage.h"
41 #include "WKRetainPtr.h"
42 #include "WKSecurityOrigin.h"
43 #include "ewk_geolocation_private.h"
44 #include "ewk_notification.h"
45 #include "ewk_notification_private.h"
46 #include "ewk_view_private.h"
47 #include "ewk_view.h"
48 #endif
49
50 namespace WebKit {
51
52 static inline PageUIClientEfl* toPageUIClientEfl(const void* clientInfo)
53 {
54     return static_cast<PageUIClientEfl*>(const_cast<void*>(clientInfo));
55 }
56
57 void PageUIClientEfl::closePage(WKPageRef, const void* clientInfo)
58 {
59     toPageUIClientEfl(clientInfo)->m_viewImpl->closePage();
60 }
61
62 WKPageRef PageUIClientEfl::createNewPage(WKPageRef, WKURLRequestRef, WKDictionaryRef, WKEventModifiers, WKEventMouseButton, const void* clientInfo)
63 {
64     return toPageUIClientEfl(clientInfo)->m_viewImpl->createNewPage();
65 }
66
67 #if OS(TIZEN)
68 bool PageUIClientEfl::runJavaScriptAlert(WKPageRef, WKStringRef alertText, WKFrameRef, const void* clientInfo)
69 {
70     return ewkViewRunJavaScriptAlert(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), alertText);
71 }
72
73 bool PageUIClientEfl::runJavaScriptConfirm(WKPageRef, WKStringRef message, WKFrameRef, const void* clientInfo)
74 {
75     return ewkViewRunJavaScriptConfirm(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), message);
76 }
77
78 bool PageUIClientEfl::runJavaScriptPrompt(WKPageRef, WKStringRef message, WKStringRef defaultValue, WKFrameRef, const void* clientInfo)
79 {
80     return ewkViewRunJavaScriptPrompt(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), message, defaultValue);
81 }
82 #else
83 void PageUIClientEfl::runJavaScriptAlert(WKPageRef, WKStringRef alertText, WKFrameRef, const void* clientInfo)
84 {
85     toPageUIClientEfl(clientInfo)->m_viewImpl->requestJSAlertPopup(WKEinaSharedString(alertText));
86 }
87
88 bool PageUIClientEfl::runJavaScriptConfirm(WKPageRef, WKStringRef message, WKFrameRef, const void* clientInfo)
89 {
90     return toPageUIClientEfl(clientInfo)->m_viewImpl->requestJSConfirmPopup(WKEinaSharedString(message));
91 }
92
93 WKStringRef PageUIClientEfl::runJavaScriptPrompt(WKPageRef, WKStringRef message, WKStringRef defaultValue, WKFrameRef, const void* clientInfo)
94 {
95     WKEinaSharedString value = toPageUIClientEfl(clientInfo)->m_viewImpl->requestJSPromptPopup(WKEinaSharedString(message), WKEinaSharedString(defaultValue));
96     return value ? WKStringCreateWithUTF8CString(value) : 0;
97 }
98 #endif
99
100 #if ENABLE(INPUT_TYPE_COLOR)
101 void PageUIClientEfl::showColorPicker(WKPageRef, WKStringRef initialColor, WKColorPickerResultListenerRef listener, const void* clientInfo)
102 {
103     PageUIClientEfl* pageUIClient = toPageUIClientEfl(clientInfo);
104     WebCore::Color color = WebCore::Color(WebKit::toWTFString(initialColor));
105     pageUIClient->m_viewImpl->requestColorPicker(color.red(), color.green(), color.blue(), color.alpha(), listener);
106 }
107
108 void PageUIClientEfl::hideColorPicker(WKPageRef, const void* clientInfo)
109 {
110     PageUIClientEfl* pageUIClient = toPageUIClientEfl(clientInfo);
111     pageUIClient->m_viewImpl->dismissColorPicker();
112 }
113 #endif
114
115 #if ENABLE(SQL_DATABASE)
116 #if ENABLE(TIZEN_SQL_DATABASE)
117 bool PageUIClientEfl::exceededDatabaseQuota(WKPageRef, WKFrameRef, WKSecurityOriginRef origin, WKStringRef displayName, unsigned long long expectedUsage, const void *clientInfo)
118 {
119     return ewkViewExceededDatabaseQuota(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), origin, displayName, expectedUsage);
120 }
121 #else
122 unsigned long long PageUIClientEfl::exceededDatabaseQuota(WKPageRef, WKFrameRef, WKSecurityOriginRef, WKStringRef databaseName, WKStringRef displayName, unsigned long long currentQuota, unsigned long long currentOriginUsage, unsigned long long currentDatabaseUsage, unsigned long long expectedUsage, const void* clientInfo)
123 {
124     EwkViewImpl* viewImpl = toPageUIClientEfl(clientInfo)->m_viewImpl;
125     return viewImpl->informDatabaseQuotaReached(toImpl(databaseName)->string(), toImpl(displayName)->string(), currentQuota, currentOriginUsage, currentDatabaseUsage, expectedUsage);
126 }
127 #endif
128 #endif
129
130 void PageUIClientEfl::focus(WKPageRef, const void* clientInfo)
131 {
132     evas_object_focus_set(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), true);
133 }
134
135 void PageUIClientEfl::unfocus(WKPageRef, const void* clientInfo)
136 {
137     evas_object_focus_set(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), false);
138 }
139
140 void PageUIClientEfl::takeFocus(WKPageRef, WKFocusDirection, const void* clientInfo)
141 {
142     // FIXME: this is only a partial implementation.
143     evas_object_focus_set(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), false);
144 }
145
146 WKRect PageUIClientEfl::getWindowFrame(WKPageRef, const void* clientInfo)
147 {
148     int x, y, width, height;
149
150 #if ENABLE(TIZEN_VIEWPORT_META_TAG)
151     ewkViewGetWindowFrame(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), &x, &y, &width, &height);
152     return WKRectMake(x, y, width, height);
153 #endif
154     Ecore_Evas* ee = ecore_evas_ecore_evas_get(evas_object_evas_get(toPageUIClientEfl(clientInfo)->m_viewImpl->view()));
155     ecore_evas_request_geometry_get(ee, &x, &y, &width, &height);
156
157     return WKRectMake(x, y, width, height);
158 }
159
160 void PageUIClientEfl::setWindowFrame(WKPageRef, WKRect frame, const void* clientInfo)
161 {
162     Ecore_Evas* ee = ecore_evas_ecore_evas_get(evas_object_evas_get(toPageUIClientEfl(clientInfo)->m_viewImpl->view()));
163     ecore_evas_move_resize(ee, frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
164 }
165
166 #if OS(TIZEN)
167 #if ENABLE(TIZEN_SUPPORT_BEFORE_UNLOAD_CONFIRM_PANEL)
168 bool PageUIClientEfl::runBeforeUnloadConfirmPanel(WKPageRef, WKStringRef message, WKFrameRef, const void* clientInfo)
169 {
170     return ewk_view_run_before_unload_confirm_panel(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), message);
171 }
172 #endif
173
174 bool PageUIClientEfl::runOpenPanel(WKPageRef, WKFrameRef, WKOpenPanelParametersRef parameters, WKOpenPanelResultListenerRef listener, const void* clientInfo)
175 {
176     return ewkViewRunOpenPanel(toPageUIClientEfl(clientInfo)->m_viewImpl->view(), parameters, listener);
177 }
178
179 void PageUIClientEfl::decidePolicyForGeolocationPermissionRequest(WKPageRef, WKFrameRef, WKSecurityOriginRef origin, WKGeolocationPermissionRequestRef permissionRequest, const void* clientInfo)
180 {
181 #if ENABLE(TIZEN_GEOLOCATION)
182     Evas_Object* ewkView = toPageUIClientEfl(clientInfo)->m_viewImpl->view();
183     Ewk_Geolocation_Permission_Request* geolocationPermissionRequest = ewkGeolocationCreatePermissionRequest(ewkView, permissionRequest, origin);
184     ewkViewRequestGeolocationPermission(ewkView, geolocationPermissionRequest);
185
186     if(!ewkGeolocationIsPermissionRequestSuspended(geolocationPermissionRequest))
187         if(!ewkGeolocationIsPermissionRequestDecided(geolocationPermissionRequest))
188             WKGeolocationPermissionRequestDeny(permissionRequest);
189 #endif
190 }
191
192 #if ENABLE(TIZEN_NOTIFICATIONS)
193 void PageUIClientEfl::decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef origin, WKNotificationPermissionRequestRef permissionRequest, const void* clientInfo)
194 {
195     Evas_Object* ewkView = toPageUIClientEfl(clientInfo)->m_viewImpl->view();
196     Ewk_Notification_Permission_Request* ewkNotificationPermissionRequest = ewkNotificationCreatePermissionRequest(ewkView, permissionRequest, origin);
197     ewkViewRequestNotificationPermission(ewkView, ewkNotificationPermissionRequest);
198
199     if(!ewkNotificationIsPermissionRequestSuspended(ewkNotificationPermissionRequest))
200         if(!ewkNotificationIsPermissionRequestDecided(ewkNotificationPermissionRequest))
201             WKNotificationPermissionRequestDeny(ewkNotificationGetWKNotificationPermissionRequest(ewkNotificationPermissionRequest));
202 }
203 #endif
204 #endif // #if OS(TIZEN)
205
206 PageUIClientEfl::PageUIClientEfl(EwkViewImpl* viewImpl)
207     : m_viewImpl(viewImpl)
208 {
209     WKPageRef pageRef = m_viewImpl->wkPage();
210     ASSERT(pageRef);
211
212     WKPageUIClient uiClient;
213     memset(&uiClient, 0, sizeof(WKPageUIClient));
214     uiClient.version = kWKPageUIClientCurrentVersion;
215     uiClient.clientInfo = this;
216     uiClient.close = closePage;
217     uiClient.createNewPage = createNewPage;
218     uiClient.runJavaScriptAlert = runJavaScriptAlert;
219     uiClient.runJavaScriptConfirm = runJavaScriptConfirm;
220     uiClient.runJavaScriptPrompt = runJavaScriptPrompt;
221     uiClient.takeFocus = takeFocus;
222     uiClient.focus = focus;
223     uiClient.unfocus = unfocus;
224     uiClient.getWindowFrame = getWindowFrame;
225     uiClient.setWindowFrame = setWindowFrame;
226 #if ENABLE(SQL_DATABASE)
227     uiClient.exceededDatabaseQuota = exceededDatabaseQuota;
228 #endif
229
230 #if ENABLE(INPUT_TYPE_COLOR)
231     uiClient.showColorPicker = showColorPicker;
232     uiClient.hideColorPicker = hideColorPicker;
233 #endif
234
235 #if OS(TIZEN)
236 #if ENABLE(TIZEN_SUPPORT_BEFORE_UNLOAD_CONFIRM_PANEL)
237     uiClient.runBeforeUnloadConfirmPanel = runBeforeUnloadConfirmPanel;
238 #endif
239     uiClient.runOpenPanel = runOpenPanel;
240     uiClient.decidePolicyForGeolocationPermissionRequest = decidePolicyForGeolocationPermissionRequest;
241 #if ENABLE(TIZEN_NOTIFICATIONS)
242     uiClient.decidePolicyForNotificationPermissionRequest = decidePolicyForNotificationPermissionRequest;
243 #endif
244 #endif
245
246     WKPageSetPageUIClient(pageRef, &uiClient);
247 }
248
249 } // namespace WebKit