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 "WKAPICast.h"
32#include "WebNumber.h"
33#include "WebOpenPanelResultListenerProxy.h"
34#include "WebPageProxy.h"
35#include <WebCore/FloatRect.h>
36#include <WebCore/IntSize.h>
37#include <WebCore/WindowFeatures.h>
38#include <string.h>
39#include <wtf/text/WTFString.h>
40
41using namespace WebCore;
42
43namespace WebKit {
44
45PassRefPtr<WebPageProxy> WebUIClient::createNewPage(WebPageProxy* page, const WindowFeatures& windowFeatures, WebEvent::Modifiers modifiers, WebMouseEvent::Button button)
46{
47    if (!m_client.createNewPage)
48        return 0;
49
50    ImmutableDictionary::MapType map;
51    if (windowFeatures.xSet)
52        map.set("x", WebDouble::create(windowFeatures.x));
53    if (windowFeatures.ySet)
54        map.set("y", WebDouble::create(windowFeatures.y));
55    if (windowFeatures.widthSet)
56        map.set("width", WebDouble::create(windowFeatures.width));
57    if (windowFeatures.heightSet)
58        map.set("height", WebDouble::create(windowFeatures.height));
59    map.set("menuBarVisible", WebBoolean::create(windowFeatures.menuBarVisible));
60    map.set("statusBarVisible", WebBoolean::create(windowFeatures.statusBarVisible));
61    map.set("toolBarVisible", WebBoolean::create(windowFeatures.toolBarVisible));
62    map.set("scrollbarsVisible", WebBoolean::create(windowFeatures.scrollbarsVisible));
63    map.set("resizable", WebBoolean::create(windowFeatures.resizable));
64    map.set("fullscreen", WebBoolean::create(windowFeatures.fullscreen));
65    map.set("dialog", WebBoolean::create(windowFeatures.dialog));
66    RefPtr<ImmutableDictionary> featuresMap = ImmutableDictionary::adopt(map);
67
68    return adoptRef(toImpl(m_client.createNewPage(toAPI(page), toAPI(featuresMap.get()), toAPI(modifiers), toAPI(button), m_client.clientInfo)));
69}
70
71void WebUIClient::showPage(WebPageProxy* page)
72{
73    if (!m_client.showPage)
74        return;
75
76    m_client.showPage(toAPI(page), m_client.clientInfo);
77}
78
79void WebUIClient::close(WebPageProxy* page)
80{
81    if (!m_client.close)
82        return;
83
84    m_client.close(toAPI(page), m_client.clientInfo);
85}
86
87void WebUIClient::takeFocus(WebPageProxy* page, WKFocusDirection direction)
88{
89    if (!m_client.takeFocus)
90        return;
91
92    m_client.takeFocus(toAPI(page), direction, m_client.clientInfo);
93}
94
95void WebUIClient::focus(WebPageProxy* page)
96{
97    if (!m_client.focus)
98        return;
99
100    m_client.focus(toAPI(page), m_client.clientInfo);
101}
102
103void WebUIClient::unfocus(WebPageProxy* page)
104{
105    if (!m_client.unfocus)
106        return;
107
108    m_client.unfocus(toAPI(page), m_client.clientInfo);
109}
110
111void WebUIClient::runJavaScriptAlert(WebPageProxy* page, const String& message, WebFrameProxy* frame)
112{
113    if (!m_client.runJavaScriptAlert)
114        return;
115
116    m_client.runJavaScriptAlert(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
117}
118
119bool WebUIClient::runJavaScriptConfirm(WebPageProxy* page, const String& message, WebFrameProxy* frame)
120{
121    if (!m_client.runJavaScriptConfirm)
122        return false;
123
124    return m_client.runJavaScriptConfirm(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
125}
126
127String WebUIClient::runJavaScriptPrompt(WebPageProxy* page, const String& message, const String& defaultValue, WebFrameProxy* frame)
128{
129    if (!m_client.runJavaScriptPrompt)
130        return String();
131
132    WebString* string = toImpl(m_client.runJavaScriptPrompt(toAPI(page), toAPI(message.impl()), toAPI(defaultValue.impl()), toAPI(frame), m_client.clientInfo));
133    if (!string)
134        return String();
135
136    String result = string->string();
137    string->deref();
138
139    return result;
140}
141
142void WebUIClient::setStatusText(WebPageProxy* page, const String& text)
143{
144    if (!m_client.setStatusText)
145        return;
146
147    m_client.setStatusText(toAPI(page), toAPI(text.impl()), m_client.clientInfo);
148}
149
150void WebUIClient::mouseDidMoveOverElement(WebPageProxy* page, WebEvent::Modifiers modifiers, APIObject* userData)
151{
152    if (!m_client.mouseDidMoveOverElement)
153        return;
154
155    m_client.mouseDidMoveOverElement(toAPI(page), toAPI(modifiers), toAPI(userData), m_client.clientInfo);
156}
157
158void WebUIClient::missingPluginButtonClicked(WebPageProxy* page, const String& mimeType, const String& url, const String& pluginsPageURL)
159{
160    if (!m_client.missingPluginButtonClicked)
161        return;
162
163    m_client.missingPluginButtonClicked(toAPI(page), toAPI(mimeType.impl()), toAPI(url.impl()), toAPI(pluginsPageURL.impl()), m_client.clientInfo);
164}
165
166bool WebUIClient::implementsDidNotHandleKeyEvent() const
167{
168    return m_client.didNotHandleKeyEvent;
169}
170
171void WebUIClient::didNotHandleKeyEvent(WebPageProxy* page, const NativeWebKeyboardEvent& event)
172{
173    if (!m_client.didNotHandleKeyEvent)
174        return;
175    m_client.didNotHandleKeyEvent(toAPI(page), event.nativeEvent(), m_client.clientInfo);
176}
177
178bool WebUIClient::toolbarsAreVisible(WebPageProxy* page)
179{
180    if (!m_client.toolbarsAreVisible)
181        return true;
182    return m_client.toolbarsAreVisible(toAPI(page), m_client.clientInfo);
183
184}
185void WebUIClient::setToolbarsAreVisible(WebPageProxy* page, bool visible)
186{
187    if (!m_client.setToolbarsAreVisible)
188        return;
189    m_client.setToolbarsAreVisible(toAPI(page), visible, m_client.clientInfo);
190}
191
192bool WebUIClient::menuBarIsVisible(WebPageProxy* page)
193{
194    if (!m_client.menuBarIsVisible)
195        return true;
196    return m_client.menuBarIsVisible(toAPI(page), m_client.clientInfo);
197}
198
199void WebUIClient::setMenuBarIsVisible(WebPageProxy* page, bool visible)
200{
201    if (!m_client.setMenuBarIsVisible)
202        return;
203    m_client.setMenuBarIsVisible(toAPI(page), visible, m_client.clientInfo);
204}
205
206bool WebUIClient::statusBarIsVisible(WebPageProxy* page)
207{
208    if (!m_client.statusBarIsVisible)
209        return true;
210    return m_client.statusBarIsVisible(toAPI(page), m_client.clientInfo);
211}
212
213void WebUIClient::setStatusBarIsVisible(WebPageProxy* page, bool visible)
214{
215    if (!m_client.setStatusBarIsVisible)
216        return;
217    m_client.setStatusBarIsVisible(toAPI(page), visible, m_client.clientInfo);
218}
219
220bool WebUIClient::isResizable(WebPageProxy* page)
221{
222    if (!m_client.isResizable)
223        return true;
224    return m_client.isResizable(toAPI(page), m_client.clientInfo);
225}
226
227void WebUIClient::setIsResizable(WebPageProxy* page, bool resizable)
228{
229    if (!m_client.setIsResizable)
230        return;
231    m_client.setIsResizable(toAPI(page), resizable, m_client.clientInfo);
232}
233
234void WebUIClient::setWindowFrame(WebPageProxy* page, const FloatRect& frame)
235{
236    if (!m_client.setWindowFrame)
237        return;
238
239    m_client.setWindowFrame(toAPI(page), toAPI(frame), m_client.clientInfo);
240}
241
242FloatRect WebUIClient::windowFrame(WebPageProxy* page)
243{
244    if (!m_client.getWindowFrame)
245        return FloatRect();
246
247    return toFloatRect(m_client.getWindowFrame(toAPI(page), m_client.clientInfo));
248}
249
250bool WebUIClient::canRunBeforeUnloadConfirmPanel() const
251{
252    return m_client.runBeforeUnloadConfirmPanel;
253}
254
255bool WebUIClient::runBeforeUnloadConfirmPanel(WebPageProxy* page, const String& message, WebFrameProxy* frame)
256{
257    if (!m_client.runBeforeUnloadConfirmPanel)
258        return true;
259
260    return m_client.runBeforeUnloadConfirmPanel(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
261}
262
263void WebUIClient::didDraw(WebPageProxy* page)
264{
265    if (!m_client.didDraw)
266        return;
267
268    m_client.didDraw(toAPI(page), m_client.clientInfo);
269}
270
271void WebUIClient::pageDidScroll(WebPageProxy* page)
272{
273    if (!m_client.pageDidScroll)
274        return;
275
276    m_client.pageDidScroll(toAPI(page), m_client.clientInfo);
277}
278
279unsigned long long WebUIClient::exceededDatabaseQuota(WebPageProxy* page, WebFrameProxy* frame, WebSecurityOrigin* origin, const String& databaseName, const String& databaseDisplayName, unsigned long long currentQuota, unsigned long long currentUsage, unsigned long long expectedUsage)
280{
281    if (!m_client.exceededDatabaseQuota)
282        return currentQuota;
283
284    return m_client.exceededDatabaseQuota(toAPI(page), toAPI(frame), toAPI(origin), toAPI(databaseName.impl()), toAPI(databaseDisplayName.impl()), currentQuota, currentUsage, expectedUsage, m_client.clientInfo);
285}
286
287bool WebUIClient::runOpenPanel(WebPageProxy* page, WebFrameProxy* frame, const WebOpenPanelParameters::Data& parameterData, WebOpenPanelResultListenerProxy* listener)
288{
289    if (!m_client.runOpenPanel)
290        return false;
291
292    RefPtr<WebOpenPanelParameters> parameters = WebOpenPanelParameters::create(parameterData);
293    m_client.runOpenPanel(toAPI(page), toAPI(frame), toAPI(parameters.get()), toAPI(listener), m_client.clientInfo);
294    return true;
295}
296
297bool WebUIClient::decidePolicyForGeolocationPermissionRequest(WebPageProxy* page, WebFrameProxy* frame, WebSecurityOrigin* origin, GeolocationPermissionRequestProxy* permissionRequest)
298{
299    if (!m_client.decidePolicyForGeolocationPermissionRequest)
300        return false;
301
302    m_client.decidePolicyForGeolocationPermissionRequest(toAPI(page), toAPI(frame), toAPI(origin), toAPI(permissionRequest), m_client.clientInfo);
303    return true;
304}
305
306float WebUIClient::headerHeight(WebPageProxy* page, WebFrameProxy* frame)
307{
308    if (!m_client.headerHeight)
309        return 0;
310
311    return m_client.headerHeight(toAPI(page), toAPI(frame), m_client.clientInfo);
312}
313
314float WebUIClient::footerHeight(WebPageProxy* page, WebFrameProxy* frame)
315{
316    if (!m_client.footerHeight)
317        return 0;
318
319    return m_client.footerHeight(toAPI(page), toAPI(frame), m_client.clientInfo);
320}
321
322void WebUIClient::drawHeader(WebPageProxy* page, WebFrameProxy* frame, const WebCore::FloatRect& rect)
323{
324    if (!m_client.drawHeader)
325        return;
326
327    m_client.drawHeader(toAPI(page), toAPI(frame), toAPI(rect), m_client.clientInfo);
328}
329
330void WebUIClient::drawFooter(WebPageProxy* page, WebFrameProxy* frame, const WebCore::FloatRect& rect)
331{
332    if (!m_client.drawFooter)
333        return;
334
335    m_client.drawFooter(toAPI(page), toAPI(frame), toAPI(rect), m_client.clientInfo);
336}
337
338void WebUIClient::printFrame(WebPageProxy* page, WebFrameProxy* frame)
339{
340    if (!m_client.printFrame)
341        return;
342
343    m_client.printFrame(toAPI(page), toAPI(frame), m_client.clientInfo);
344}
345
346bool WebUIClient::canRunModal() const
347{
348    return m_client.runModal;
349}
350
351void WebUIClient::runModal(WebPageProxy* page)
352{
353    if (!m_client.runModal)
354        return;
355
356    m_client.runModal(toAPI(page), m_client.clientInfo);
357}
358
359void WebUIClient::didCompleteRubberBandForMainFrame(WebPageProxy* page, const IntSize& initialOverhang)
360{
361    if (!m_client.didCompleteRubberBandForMainFrame)
362        return;
363
364    m_client.didCompleteRubberBandForMainFrame(toAPI(page), toAPI(initialOverhang), m_client.clientInfo);
365}
366
367void WebUIClient::saveDataToFileInDownloadsFolder(WebPageProxy* page, const String& suggestedFilename, const String& mimeType, const String& originatingURLString, WebData* data)
368{
369    if (!m_client.saveDataToFileInDownloadsFolder)
370        return;
371
372    m_client.saveDataToFileInDownloadsFolder(toAPI(page), toAPI(suggestedFilename.impl()), toAPI(mimeType.impl()), toURLRef(originatingURLString.impl()), toAPI(data), m_client.clientInfo);
373}
374
375} // namespace WebKit
376