Merge "Use EwkView's variables instead of drawingScaleFactor and drawingScrollPositio...
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / WebUIClient.cpp
1 /*
2  * Copyright (C) 2010, 2011 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 "WebUIClient.h"
28
29 #include "ImmutableDictionary.h"
30 #include "NativeWebKeyboardEvent.h"
31 #include "NativeWebWheelEvent.h"
32 #include "NotificationPermissionRequest.h"
33 #include "WKAPICast.h"
34 #if ENABLE(TIZEN_INPUT_COLOR_PICKER) // wait for upstream
35 #include "WebColorPickerResultListenerProxy.h"
36 #endif
37 #include "WebNumber.h"
38 #include "WebOpenPanelResultListenerProxy.h"
39 #include "WebPageProxy.h"
40 #include <WebCore/FloatRect.h>
41 #include <WebCore/IntSize.h>
42 #include <WebCore/WindowFeatures.h>
43 #include <string.h>
44 #include <wtf/text/WTFString.h>
45
46 using namespace WebCore;
47
48 namespace WebKit {
49
50 PassRefPtr<WebPageProxy> WebUIClient::createNewPage(WebPageProxy* page, const ResourceRequest& resourceRequest, const WindowFeatures& windowFeatures, WebEvent::Modifiers modifiers, WebMouseEvent::Button button)
51 {
52     if (!m_client.version && !m_client.createNewPage_deprecatedForUseWithV0)
53         return 0;
54     
55     if (m_client.version == kWKPageUIClientCurrentVersion && !m_client.createNewPage)
56         return 0;
57
58     ImmutableDictionary::MapType map;
59     if (windowFeatures.xSet)
60         map.set("x", WebDouble::create(windowFeatures.x));
61     if (windowFeatures.ySet)
62         map.set("y", WebDouble::create(windowFeatures.y));
63     if (windowFeatures.widthSet)
64         map.set("width", WebDouble::create(windowFeatures.width));
65     if (windowFeatures.heightSet)
66         map.set("height", WebDouble::create(windowFeatures.height));
67     map.set("menuBarVisible", WebBoolean::create(windowFeatures.menuBarVisible));
68     map.set("statusBarVisible", WebBoolean::create(windowFeatures.statusBarVisible));
69     map.set("toolBarVisible", WebBoolean::create(windowFeatures.toolBarVisible));
70     map.set("locationBarVisible", WebBoolean::create(windowFeatures.locationBarVisible));
71     map.set("scrollbarsVisible", WebBoolean::create(windowFeatures.scrollbarsVisible));
72     map.set("resizable", WebBoolean::create(windowFeatures.resizable));
73     map.set("fullscreen", WebBoolean::create(windowFeatures.fullscreen));
74     map.set("dialog", WebBoolean::create(windowFeatures.dialog));
75     RefPtr<ImmutableDictionary> featuresMap = ImmutableDictionary::adopt(map);
76
77     if (!m_client.version)
78         return adoptRef(toImpl(m_client.createNewPage_deprecatedForUseWithV0(toAPI(page), toAPI(featuresMap.get()), toAPI(modifiers), toAPI(button), m_client.clientInfo)));
79
80     RefPtr<WebURLRequest> request = WebURLRequest::create(resourceRequest);    
81     return adoptRef(toImpl(m_client.createNewPage(toAPI(page), toAPI(request.get()), toAPI(featuresMap.get()), toAPI(modifiers), toAPI(button), m_client.clientInfo)));
82
83
84 void WebUIClient::showPage(WebPageProxy* page)
85 {
86     if (!m_client.showPage)
87         return;
88     
89     m_client.showPage(toAPI(page), m_client.clientInfo);
90 }
91
92 void WebUIClient::close(WebPageProxy* page)
93 {
94     if (!m_client.close)
95         return;
96     
97     m_client.close(toAPI(page), m_client.clientInfo);
98 }
99
100 void WebUIClient::takeFocus(WebPageProxy* page, WKFocusDirection direction)
101 {
102     if (!m_client.takeFocus)
103         return;
104     
105     m_client.takeFocus(toAPI(page), direction, m_client.clientInfo);
106 }
107
108 void WebUIClient::focus(WebPageProxy* page)
109 {
110     if (!m_client.focus)
111         return;
112     
113     m_client.focus(toAPI(page), m_client.clientInfo);
114 }
115
116 void WebUIClient::unfocus(WebPageProxy* page)
117 {
118     if (!m_client.unfocus)
119         return;
120
121     m_client.unfocus(toAPI(page), m_client.clientInfo);
122 }
123
124 #if OS(TIZEN)
125 bool WebUIClient::runJavaScriptAlert(WebPageProxy* page, const String& message, WebFrameProxy* frame)
126 {
127 #if ENABLE(TIZEN_WEBKIT2_EFL_WTR)
128     return false;
129 #endif
130
131     if (!m_client.runJavaScriptAlert)
132         return false;
133
134     return m_client.runJavaScriptAlert(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
135 }
136
137 bool WebUIClient::runJavaScriptConfirm(WebPageProxy* page, const String& message, WebFrameProxy* frame)
138 {
139 #if ENABLE(TIZEN_WEBKIT2_EFL_WTR)
140     return false;
141 #endif
142
143     if (!m_client.runJavaScriptConfirm)
144         return false;
145
146     return m_client.runJavaScriptConfirm(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
147 }
148
149 bool WebUIClient::runJavaScriptPrompt(WebPageProxy* page, const String& message, const String& defaultValue, WebFrameProxy* frame)
150 {
151 #if ENABLE(TIZEN_WEBKIT2_EFL_WTR)
152     return false;
153 #endif
154
155     if (!m_client.runJavaScriptPrompt)
156         return false;
157
158     return m_client.runJavaScriptPrompt(toAPI(page), toAPI(message.impl()), toAPI(defaultValue.impl()), toAPI(frame), m_client.clientInfo);
159 }
160 #else
161 void WebUIClient::runJavaScriptAlert(WebPageProxy* page, const String& message, WebFrameProxy* frame)
162 {
163 #if ENABLE(TIZEN_WEBKIT2_EFL_WTR)
164     return;
165 #endif
166
167     if (!m_client.runJavaScriptAlert)
168         return;
169
170     m_client.runJavaScriptAlert(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
171 }
172
173 bool WebUIClient::runJavaScriptConfirm(WebPageProxy* page, const String& message, WebFrameProxy* frame)
174 {
175 #if ENABLE(TIZEN_WEBKIT2_EFL_WTR)
176     return false;
177 #endif
178
179     if (!m_client.runJavaScriptConfirm)
180         return false;
181
182     return m_client.runJavaScriptConfirm(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
183 }
184
185 String WebUIClient::runJavaScriptPrompt(WebPageProxy* page, const String& message, const String& defaultValue, WebFrameProxy* frame)
186 {
187 #if ENABLE(TIZEN_WEBKIT2_EFL_WTR)
188     return String();
189 #endif
190
191     if (!m_client.runJavaScriptPrompt)
192         return String();
193
194     WebString* string = toImpl(m_client.runJavaScriptPrompt(toAPI(page), toAPI(message.impl()), toAPI(defaultValue.impl()), toAPI(frame), m_client.clientInfo));
195     if (!string)
196         return String();
197
198     String result = string->string();
199     string->deref();
200
201     return result;
202 }
203 #endif
204
205 void WebUIClient::setStatusText(WebPageProxy* page, const String& text)
206 {
207     if (!m_client.setStatusText)
208         return;
209
210     m_client.setStatusText(toAPI(page), toAPI(text.impl()), m_client.clientInfo);
211 }
212
213 void WebUIClient::mouseDidMoveOverElement(WebPageProxy* page, const WebHitTestResult::Data& data, WebEvent::Modifiers modifiers, APIObject* userData)
214 {
215     if (!m_client.mouseDidMoveOverElement && !m_client.mouseDidMoveOverElement_deprecatedForUseWithV0)
216         return;
217
218     if (m_client.version == kWKPageUIClientCurrentVersion && !m_client.mouseDidMoveOverElement)
219         return;
220
221     if (!m_client.version) {
222         m_client.mouseDidMoveOverElement_deprecatedForUseWithV0(toAPI(page), toAPI(modifiers), toAPI(userData), m_client.clientInfo);
223         return;
224     }
225
226     RefPtr<WebHitTestResult> webHitTestResult = WebHitTestResult::create(data);
227     m_client.mouseDidMoveOverElement(toAPI(page), toAPI(webHitTestResult.get()), toAPI(modifiers), toAPI(userData), m_client.clientInfo);
228 }
229
230 void WebUIClient::unavailablePluginButtonClicked(WebPageProxy* page, WKPluginUnavailabilityReason pluginUnavailabilityReason, const String& mimeType, const String& url, const String& pluginsPageURL)
231 {
232     if (pluginUnavailabilityReason == kWKPluginUnavailabilityReasonPluginMissing) {
233         if (m_client.missingPluginButtonClicked_deprecatedForUseWithV0)
234             m_client.missingPluginButtonClicked_deprecatedForUseWithV0(toAPI(page), toAPI(mimeType.impl()), toAPI(url.impl()), toAPI(pluginsPageURL.impl()), m_client.clientInfo);
235     }
236
237     if (!m_client.unavailablePluginButtonClicked)
238         return;
239
240     m_client.unavailablePluginButtonClicked(toAPI(page), pluginUnavailabilityReason, toAPI(mimeType.impl()), toAPI(url.impl()), toAPI(pluginsPageURL.impl()), m_client.clientInfo);
241
242 }
243
244 bool WebUIClient::implementsDidNotHandleKeyEvent() const
245 {
246     return m_client.didNotHandleKeyEvent;
247 }
248
249 void WebUIClient::didNotHandleKeyEvent(WebPageProxy* page, const NativeWebKeyboardEvent& event)
250 {
251     if (!m_client.didNotHandleKeyEvent)
252         return;
253     m_client.didNotHandleKeyEvent(toAPI(page), event.nativeEvent(), m_client.clientInfo);
254 }
255
256 bool WebUIClient::implementsDidNotHandleWheelEvent() const
257 {
258     return m_client.didNotHandleWheelEvent;
259 }
260
261 void WebUIClient::didNotHandleWheelEvent(WebPageProxy* page, const NativeWebWheelEvent& event)
262 {
263     if (!m_client.didNotHandleWheelEvent)
264         return;
265     m_client.didNotHandleWheelEvent(toAPI(page), event.nativeEvent(), m_client.clientInfo);
266 }
267
268 bool WebUIClient::toolbarsAreVisible(WebPageProxy* page)
269 {
270     if (!m_client.toolbarsAreVisible)
271         return true;
272     return m_client.toolbarsAreVisible(toAPI(page), m_client.clientInfo);
273
274 }
275 void WebUIClient::setToolbarsAreVisible(WebPageProxy* page, bool visible)
276 {
277     if (!m_client.setToolbarsAreVisible)
278         return;
279     m_client.setToolbarsAreVisible(toAPI(page), visible, m_client.clientInfo);
280 }
281
282 bool WebUIClient::menuBarIsVisible(WebPageProxy* page)
283 {
284     if (!m_client.menuBarIsVisible)
285         return true;
286     return m_client.menuBarIsVisible(toAPI(page), m_client.clientInfo);
287 }
288
289 void WebUIClient::setMenuBarIsVisible(WebPageProxy* page, bool visible)
290 {
291     if (!m_client.setMenuBarIsVisible)
292         return;
293     m_client.setMenuBarIsVisible(toAPI(page), visible, m_client.clientInfo);
294 }
295
296 bool WebUIClient::statusBarIsVisible(WebPageProxy* page)
297 {
298     if (!m_client.statusBarIsVisible)
299         return true;
300     return m_client.statusBarIsVisible(toAPI(page), m_client.clientInfo);
301 }
302
303 void WebUIClient::setStatusBarIsVisible(WebPageProxy* page, bool visible)
304 {
305     if (!m_client.setStatusBarIsVisible)
306         return;
307     m_client.setStatusBarIsVisible(toAPI(page), visible, m_client.clientInfo);
308 }
309
310 bool WebUIClient::isResizable(WebPageProxy* page)
311 {
312     if (!m_client.isResizable)
313         return true;
314     return m_client.isResizable(toAPI(page), m_client.clientInfo);
315 }
316
317 void WebUIClient::setIsResizable(WebPageProxy* page, bool resizable)
318 {
319     if (!m_client.setIsResizable)
320         return;
321     m_client.setIsResizable(toAPI(page), resizable, m_client.clientInfo);
322 }
323
324 void WebUIClient::setWindowFrame(WebPageProxy* page, const FloatRect& frame)
325 {
326     if (!m_client.setWindowFrame)
327         return;
328
329     m_client.setWindowFrame(toAPI(page), toAPI(frame), m_client.clientInfo);
330 }
331
332 FloatRect WebUIClient::windowFrame(WebPageProxy* page)
333 {
334     if (!m_client.getWindowFrame)
335         return FloatRect();
336
337     return toFloatRect(m_client.getWindowFrame(toAPI(page), m_client.clientInfo));
338 }
339
340 bool WebUIClient::canRunBeforeUnloadConfirmPanel() const
341 {
342     return m_client.runBeforeUnloadConfirmPanel;
343 }
344
345 bool WebUIClient::runBeforeUnloadConfirmPanel(WebPageProxy* page, const String& message, WebFrameProxy* frame)
346 {
347     if (!m_client.runBeforeUnloadConfirmPanel)
348         return true;
349
350     return m_client.runBeforeUnloadConfirmPanel(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
351 }
352
353 void WebUIClient::didDraw(WebPageProxy* page)
354 {
355     if (!m_client.didDraw)
356         return;
357
358     m_client.didDraw(toAPI(page), m_client.clientInfo);
359 }
360
361 void WebUIClient::pageDidScroll(WebPageProxy* page)
362 {
363     if (!m_client.pageDidScroll)
364         return;
365
366     m_client.pageDidScroll(toAPI(page), m_client.clientInfo);
367 }
368
369 unsigned long long WebUIClient::exceededDatabaseQuota(WebPageProxy* page, WebFrameProxy* frame, WebSecurityOrigin* origin, const String& databaseName, const String& databaseDisplayName, unsigned long long currentQuota, unsigned long long currentOriginUsage, unsigned long long currentDatabaseUsage, unsigned long long expectedUsage)
370 {
371     if (!m_client.exceededDatabaseQuota)
372         return currentQuota;
373
374     return m_client.exceededDatabaseQuota(toAPI(page), toAPI(frame), toAPI(origin), toAPI(databaseName.impl()), toAPI(databaseDisplayName.impl()), currentQuota, currentOriginUsage, currentDatabaseUsage, expectedUsage, m_client.clientInfo);
375 }
376
377 bool WebUIClient::runOpenPanel(WebPageProxy* page, WebFrameProxy* frame, WebOpenPanelParameters* parameters, WebOpenPanelResultListenerProxy* listener)
378 {
379     if (!m_client.runOpenPanel)
380         return false;
381
382     m_client.runOpenPanel(toAPI(page), toAPI(frame), toAPI(parameters), toAPI(listener), m_client.clientInfo);
383     return true;
384 }
385
386 bool WebUIClient::decidePolicyForGeolocationPermissionRequest(WebPageProxy* page, WebFrameProxy* frame, WebSecurityOrigin* origin, GeolocationPermissionRequestProxy* permissionRequest)
387 {
388     if (!m_client.decidePolicyForGeolocationPermissionRequest)
389         return false;
390
391     m_client.decidePolicyForGeolocationPermissionRequest(toAPI(page), toAPI(frame), toAPI(origin), toAPI(permissionRequest), m_client.clientInfo);
392     return true;
393 }
394
395 bool WebUIClient::decidePolicyForNotificationPermissionRequest(WebPageProxy* page, WebSecurityOrigin* origin, NotificationPermissionRequest* permissionRequest)
396 {
397     if (!m_client.decidePolicyForNotificationPermissionRequest)
398         return false;
399     
400     m_client.decidePolicyForNotificationPermissionRequest(toAPI(page), toAPI(origin), toAPI(permissionRequest), m_client.clientInfo);
401     return true;
402 }
403
404 float WebUIClient::headerHeight(WebPageProxy* page, WebFrameProxy* frame)
405 {
406     if (!m_client.headerHeight)
407         return 0;
408
409     return m_client.headerHeight(toAPI(page), toAPI(frame), m_client.clientInfo);
410 }
411
412 float WebUIClient::footerHeight(WebPageProxy* page, WebFrameProxy* frame)
413 {
414     if (!m_client.footerHeight)
415         return 0;
416
417     return m_client.footerHeight(toAPI(page), toAPI(frame), m_client.clientInfo);
418 }
419
420 void WebUIClient::drawHeader(WebPageProxy* page, WebFrameProxy* frame, const WebCore::FloatRect& rect)
421 {
422     if (!m_client.drawHeader)
423         return;
424
425     m_client.drawHeader(toAPI(page), toAPI(frame), toAPI(rect), m_client.clientInfo);
426 }
427
428 void WebUIClient::drawFooter(WebPageProxy* page, WebFrameProxy* frame, const WebCore::FloatRect& rect)
429 {
430     if (!m_client.drawFooter)
431         return;
432
433     m_client.drawFooter(toAPI(page), toAPI(frame), toAPI(rect), m_client.clientInfo);
434 }
435
436 void WebUIClient::printFrame(WebPageProxy* page, WebFrameProxy* frame)
437 {
438     if (!m_client.printFrame)
439         return;
440
441     m_client.printFrame(toAPI(page), toAPI(frame), m_client.clientInfo);
442 }
443
444 bool WebUIClient::canRunModal() const
445 {
446     return m_client.runModal;
447 }
448
449 void WebUIClient::runModal(WebPageProxy* page)
450 {
451     if (!m_client.runModal)
452         return;
453
454     m_client.runModal(toAPI(page), m_client.clientInfo);
455 }
456
457 void WebUIClient::saveDataToFileInDownloadsFolder(WebPageProxy* page, const String& suggestedFilename, const String& mimeType, const String& originatingURLString, WebData* data)
458 {
459     if (!m_client.saveDataToFileInDownloadsFolder)
460         return;
461
462     m_client.saveDataToFileInDownloadsFolder(toAPI(page), toAPI(suggestedFilename.impl()), toAPI(mimeType.impl()), toURLRef(originatingURLString.impl()), toAPI(data), m_client.clientInfo);
463 }
464
465 bool WebUIClient::shouldInterruptJavaScript(WebPageProxy* page)
466 {
467     if (!m_client.shouldInterruptJavaScript)
468         return false;
469
470     return m_client.shouldInterruptJavaScript(toAPI(page), m_client.clientInfo);
471 }
472
473 #if ENABLE(TIZEN_INPUT_COLOR_PICKER) // wait for upstream
474 #if ENABLE(INPUT_TYPE_COLOR)
475 bool WebUIClient::showColorPicker(WebPageProxy* page, const String& initialColor, WebColorPickerResultListenerProxy* listener)
476 {
477     if (!m_client.showColorPicker)
478         return false;
479
480     m_client.showColorPicker(toAPI(page), toAPI(initialColor.impl()), toAPI(listener), m_client.clientInfo);
481     return true;
482 }
483
484 bool WebUIClient::hideColorPicker(WebPageProxy* page)
485 {
486     if (!m_client.hideColorPicker)
487         return false;
488
489     m_client.hideColorPicker(toAPI(page), m_client.clientInfo);
490     return true;
491 }
492 #endif
493 #endif
494
495 } // namespace WebKit