WebUIClient.cpp revision 2fc2651226baac27029e38c9d6ef883fa32084db
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::runJavaScriptAlert(WebPageProxy* page, const String& message, WebFrameProxy* frame)
88{
89    if (!m_client.runJavaScriptAlert)
90        return;
91
92    m_client.runJavaScriptAlert(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
93}
94
95bool WebUIClient::runJavaScriptConfirm(WebPageProxy* page, const String& message, WebFrameProxy* frame)
96{
97    if (!m_client.runJavaScriptConfirm)
98        return false;
99
100    return m_client.runJavaScriptConfirm(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
101}
102
103String WebUIClient::runJavaScriptPrompt(WebPageProxy* page, const String& message, const String& defaultValue, WebFrameProxy* frame)
104{
105    if (!m_client.runJavaScriptPrompt)
106        return String();
107
108    WebString* string = toImpl(m_client.runJavaScriptPrompt(toAPI(page), toAPI(message.impl()), toAPI(defaultValue.impl()), toAPI(frame), m_client.clientInfo));
109    if (!string)
110        return String();
111
112    String result = string->string();
113    string->deref();
114
115    return result;
116}
117
118void WebUIClient::setStatusText(WebPageProxy* page, const String& text)
119{
120    if (!m_client.setStatusText)
121        return;
122
123    m_client.setStatusText(toAPI(page), toAPI(text.impl()), m_client.clientInfo);
124}
125
126void WebUIClient::mouseDidMoveOverElement(WebPageProxy* page, WebEvent::Modifiers modifiers, APIObject* userData)
127{
128    if (!m_client.mouseDidMoveOverElement)
129        return;
130
131    m_client.mouseDidMoveOverElement(toAPI(page), toAPI(modifiers), toAPI(userData), m_client.clientInfo);
132}
133
134void WebUIClient::missingPluginButtonClicked(WebPageProxy* page, const String& mimeType, const String& url)
135{
136    if (!m_client.missingPluginButtonClicked)
137        return;
138
139    m_client.missingPluginButtonClicked(toAPI(page), toAPI(mimeType.impl()), toAPI(url.impl()), m_client.clientInfo);
140}
141
142void WebUIClient::didNotHandleKeyEvent(WebPageProxy* page, const NativeWebKeyboardEvent& event)
143{
144    if (!m_client.didNotHandleKeyEvent)
145        return;
146    m_client.didNotHandleKeyEvent(toAPI(page), event.nativeEvent(), m_client.clientInfo);
147}
148
149bool WebUIClient::toolbarsAreVisible(WebPageProxy* page)
150{
151    if (!m_client.toolbarsAreVisible)
152        return true;
153    return m_client.toolbarsAreVisible(toAPI(page), m_client.clientInfo);
154
155}
156void WebUIClient::setToolbarsAreVisible(WebPageProxy* page, bool visible)
157{
158    if (!m_client.setToolbarsAreVisible)
159        return;
160    m_client.setToolbarsAreVisible(toAPI(page), visible, m_client.clientInfo);
161}
162
163bool WebUIClient::menuBarIsVisible(WebPageProxy* page)
164{
165    if (!m_client.menuBarIsVisible)
166        return true;
167    return m_client.menuBarIsVisible(toAPI(page), m_client.clientInfo);
168}
169
170void WebUIClient::setMenuBarIsVisible(WebPageProxy* page, bool visible)
171{
172    if (!m_client.setMenuBarIsVisible)
173        return;
174    m_client.setMenuBarIsVisible(toAPI(page), visible, m_client.clientInfo);
175}
176
177bool WebUIClient::statusBarIsVisible(WebPageProxy* page)
178{
179    if (!m_client.statusBarIsVisible)
180        return true;
181    return m_client.statusBarIsVisible(toAPI(page), m_client.clientInfo);
182}
183
184void WebUIClient::setStatusBarIsVisible(WebPageProxy* page, bool visible)
185{
186    if (!m_client.setStatusBarIsVisible)
187        return;
188    m_client.setStatusBarIsVisible(toAPI(page), visible, m_client.clientInfo);
189}
190
191bool WebUIClient::isResizable(WebPageProxy* page)
192{
193    if (!m_client.isResizable)
194        return true;
195    return m_client.isResizable(toAPI(page), m_client.clientInfo);
196}
197
198void WebUIClient::setIsResizable(WebPageProxy* page, bool resizable)
199{
200    if (!m_client.setIsResizable)
201        return;
202    m_client.setIsResizable(toAPI(page), resizable, m_client.clientInfo);
203}
204
205void WebUIClient::setWindowFrame(WebPageProxy* page, const FloatRect& frame)
206{
207    if (!m_client.setWindowFrame)
208        return;
209
210    m_client.setWindowFrame(toAPI(page), toAPI(frame), m_client.clientInfo);
211}
212
213FloatRect WebUIClient::windowFrame(WebPageProxy* page)
214{
215    if (!m_client.getWindowFrame)
216        return FloatRect();
217
218    return toFloatRect(m_client.getWindowFrame(toAPI(page), m_client.clientInfo));
219}
220
221bool WebUIClient::canRunBeforeUnloadConfirmPanel()
222{
223    return m_client.runBeforeUnloadConfirmPanel;
224}
225
226bool WebUIClient::runBeforeUnloadConfirmPanel(WebPageProxy* page, const String& message, WebFrameProxy* frame)
227{
228    if (!m_client.runBeforeUnloadConfirmPanel)
229        return true;
230
231    return m_client.runBeforeUnloadConfirmPanel(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.clientInfo);
232}
233
234void WebUIClient::didDraw(WebPageProxy* page)
235{
236    if (!m_client.didDraw)
237        return;
238
239    m_client.didDraw(toAPI(page), m_client.clientInfo);
240}
241
242void WebUIClient::pageDidScroll(WebPageProxy* page)
243{
244    if (!m_client.pageDidScroll)
245        return;
246
247    m_client.pageDidScroll(toAPI(page), m_client.clientInfo);
248}
249
250unsigned 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)
251{
252    if (!m_client.exceededDatabaseQuota)
253        return currentQuota;
254
255    return m_client.exceededDatabaseQuota(toAPI(page), toAPI(frame), toAPI(origin), toAPI(databaseName.impl()), toAPI(databaseDisplayName.impl()), currentQuota, currentUsage, expectedUsage, m_client.clientInfo);
256}
257
258bool WebUIClient::runOpenPanel(WebPageProxy* page, WebFrameProxy* frame, const WebOpenPanelParameters::Data& parameterData, WebOpenPanelResultListenerProxy* listener)
259{
260    if (!m_client.runOpenPanel)
261        return false;
262
263    RefPtr<WebOpenPanelParameters> parameters = WebOpenPanelParameters::create(parameterData);
264    m_client.runOpenPanel(toAPI(page), toAPI(frame), toAPI(parameters.get()), toAPI(listener), m_client.clientInfo);
265    return true;
266}
267
268bool WebUIClient::decidePolicyForGeolocationPermissionRequest(WebPageProxy* page, WebFrameProxy* frame, WebSecurityOrigin* origin, GeolocationPermissionRequestProxy* permissionRequest)
269{
270    if (!m_client.decidePolicyForGeolocationPermissionRequest)
271        return false;
272
273    m_client.decidePolicyForGeolocationPermissionRequest(toAPI(page), toAPI(frame), toAPI(origin), toAPI(permissionRequest), m_client.clientInfo);
274    return true;
275}
276
277float WebUIClient::headerHeight(WebPageProxy* page, WebFrameProxy* frame)
278{
279    if (!m_client.headerHeight)
280        return 0;
281
282    return m_client.headerHeight(toAPI(page), toAPI(frame), m_client.clientInfo);
283}
284
285float WebUIClient::footerHeight(WebPageProxy* page, WebFrameProxy* frame)
286{
287    if (!m_client.footerHeight)
288        return 0;
289
290    return m_client.footerHeight(toAPI(page), toAPI(frame), m_client.clientInfo);
291}
292
293void WebUIClient::drawHeader(WebPageProxy* page, WebFrameProxy* frame, const WebCore::FloatRect& rect)
294{
295    if (!m_client.drawHeader)
296        return;
297
298    m_client.drawHeader(toAPI(page), toAPI(frame), toAPI(rect), m_client.clientInfo);
299}
300
301void WebUIClient::drawFooter(WebPageProxy* page, WebFrameProxy* frame, const WebCore::FloatRect& rect)
302{
303    if (!m_client.drawFooter)
304        return;
305
306    m_client.drawFooter(toAPI(page), toAPI(frame), toAPI(rect), m_client.clientInfo);
307}
308
309void WebUIClient::printFrame(WebPageProxy* page, WebFrameProxy* frame)
310{
311    if (!m_client.printFrame)
312        return;
313
314    m_client.printFrame(toAPI(page), toAPI(frame), m_client.clientInfo);
315}
316
317bool WebUIClient::canRunModal() const
318{
319    return m_client.runModal;
320}
321
322void WebUIClient::runModal(WebPageProxy* page)
323{
324    if (!m_client.runModal)
325        return;
326
327    m_client.runModal(toAPI(page), m_client.clientInfo);
328}
329
330void WebUIClient::didCompleteRubberBandForMainFrame(WebPageProxy* page, const IntSize& initialOverhang)
331{
332    if (!m_client.runModal)
333        return;
334
335    m_client.didCompleteRubberBandForMainFrame(toAPI(page), toAPI(initialOverhang), m_client.clientInfo);
336}
337
338} // namespace WebKit
339