[Qt][WK2] Move non-api classes to WebKit namespace at WebKit2/UiProcess/qt
[WebKit-https.git] / Source / WebKit2 / UIProcess / qt / QtWebPageUIClient.cpp
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #include "config.h"
22 #include "QtWebPageUIClient.h"
23
24 #include "WKStringQt.h"
25 #include "WKURLQt.h"
26 #include "qquickwebview_p.h"
27 #include "qquickwebview_p_p.h"
28 #include "qwebpermissionrequest_p.h"
29 #include <WKAPICast.h>
30 #include <WKHitTestResult.h>
31 #include <WKOpenPanelParameters.h>
32 #include <WKOpenPanelResultListener.h>
33 #include <WKRetainPtr.h>
34
35 namespace WebKit {
36
37 QtWebPageUIClient::QtWebPageUIClient(WKPageRef pageRef, QQuickWebView* webView)
38     : m_webView(webView)
39 {
40     WKPageUIClient uiClient;
41     memset(&uiClient, 0, sizeof(WKPageUIClient));
42     uiClient.version = kWKPageUIClientCurrentVersion;
43     uiClient.clientInfo = this;
44     uiClient.runJavaScriptAlert = runJavaScriptAlert;
45     uiClient.runJavaScriptConfirm = runJavaScriptConfirm;
46     uiClient.runJavaScriptPrompt = runJavaScriptPrompt;
47     uiClient.runOpenPanel = runOpenPanel;
48     uiClient.mouseDidMoveOverElement = mouseDidMoveOverElement;
49     uiClient.exceededDatabaseQuota = exceededDatabaseQuota;
50     uiClient.decidePolicyForGeolocationPermissionRequest = policyForGeolocationPermissionRequest;
51     WKPageSetPageUIClient(pageRef, &uiClient);
52 }
53
54 quint64 QtWebPageUIClient::exceededDatabaseQuota(const QString& databaseName, const QString& displayName, WKSecurityOriginRef securityOrigin, quint64 currentQuota, quint64 currentOriginUsage, quint64 currentDatabaseUsage, quint64 expectedUsage)
55 {
56     return m_webView->d_func()->exceededDatabaseQuota(databaseName, displayName, securityOrigin, currentQuota, currentOriginUsage, currentDatabaseUsage, expectedUsage);
57 }
58
59 void QtWebPageUIClient::runJavaScriptAlert(const QString& message)
60 {
61     m_webView->d_func()->runJavaScriptAlert(message);
62 }
63
64 bool QtWebPageUIClient::runJavaScriptConfirm(const QString& message)
65 {
66     return m_webView->d_func()->runJavaScriptConfirm(message);
67 }
68
69 QString QtWebPageUIClient::runJavaScriptPrompt(const QString& message, const QString& defaultValue, bool& ok)
70 {
71     return m_webView->d_func()->runJavaScriptPrompt(message, defaultValue, ok);
72 }
73
74 void QtWebPageUIClient::runOpenPanel(WKOpenPanelResultListenerRef listenerRef, const QStringList& selectedFileNames, FileChooserType type)
75 {
76     m_webView->d_func()->chooseFiles(listenerRef, selectedFileNames, type);
77 }
78
79 void QtWebPageUIClient::mouseDidMoveOverElement(const QUrl& linkURL, const QString& linkTitle)
80 {
81     if (linkURL == m_lastHoveredURL && linkTitle == m_lastHoveredTitle)
82         return;
83     m_lastHoveredURL = linkURL;
84     m_lastHoveredTitle = linkTitle;
85     emit m_webView->linkHovered(m_lastHoveredURL, m_lastHoveredTitle);
86 }
87
88 void QtWebPageUIClient::permissionRequest(QWebPermissionRequest* request)
89 {
90     request->setParent(m_webView);
91     emit m_webView->experimental()->permissionRequested(request);
92 }
93
94 static QtWebPageUIClient* toQtWebPageUIClient(const void* clientInfo)
95 {
96     ASSERT(clientInfo);
97     return reinterpret_cast<QtWebPageUIClient*>(const_cast<void*>(clientInfo));
98 }
99
100 unsigned long long QtWebPageUIClient::exceededDatabaseQuota(WKPageRef, WKFrameRef, WKSecurityOriginRef securityOrigin, WKStringRef databaseName, WKStringRef displayName, unsigned long long currentQuota, unsigned long long currentOriginUsage, unsigned long long currentDatabaseUsage, unsigned long long expectedUsage, const void *clientInfo)
101 {
102     QString qDisplayName = WKStringCopyQString(displayName);
103     QString qDatabaseName = WKStringCopyQString(databaseName);
104     return toQtWebPageUIClient(clientInfo)->exceededDatabaseQuota(qDatabaseName, qDisplayName, securityOrigin, currentQuota, currentOriginUsage, currentDatabaseUsage, expectedUsage);
105 }
106
107 void QtWebPageUIClient::runJavaScriptAlert(WKPageRef, WKStringRef alertText, WKFrameRef, const void* clientInfo)
108 {
109     QString qAlertText = WKStringCopyQString(alertText);
110     toQtWebPageUIClient(clientInfo)->runJavaScriptAlert(qAlertText);
111 }
112
113 bool QtWebPageUIClient::runJavaScriptConfirm(WKPageRef, WKStringRef message, WKFrameRef, const void* clientInfo)
114 {
115     QString qMessage = WKStringCopyQString(message);
116     return toQtWebPageUIClient(clientInfo)->runJavaScriptConfirm(qMessage);
117 }
118
119 static inline WKStringRef createNullWKString()
120 {
121     RefPtr<WebString> webString = WebString::createNull();
122     return toAPI(webString.release().leakRef());
123 }
124
125 WKStringRef QtWebPageUIClient::runJavaScriptPrompt(WKPageRef, WKStringRef message, WKStringRef defaultValue, WKFrameRef, const void* clientInfo)
126 {
127     QString qMessage = WKStringCopyQString(message);
128     QString qDefaultValue = WKStringCopyQString(defaultValue);
129     bool ok = false;
130     QString result = toQtWebPageUIClient(clientInfo)->runJavaScriptPrompt(qMessage, qDefaultValue, ok);
131     if (!ok)
132         return createNullWKString();
133     return WKStringCreateWithQString(result);
134 }
135
136 void QtWebPageUIClient::runOpenPanel(WKPageRef, WKFrameRef, WKOpenPanelParametersRef parameters, WKOpenPanelResultListenerRef listener, const void* clientInfo)
137 {
138     Vector<String> wkSelectedFileNames = toImpl(parameters)->selectedFileNames();
139
140     QStringList selectedFileNames;
141     for (size_t i = 0; i < wkSelectedFileNames.size(); ++i)
142         selectedFileNames += wkSelectedFileNames.at(i);
143
144     FileChooserType allowMultipleFiles = WKOpenPanelParametersGetAllowsMultipleFiles(parameters) ? MultipleFilesSelection : SingleFileSelection;
145     toQtWebPageUIClient(clientInfo)->runOpenPanel(listener, selectedFileNames, allowMultipleFiles);
146 }
147
148 void QtWebPageUIClient::mouseDidMoveOverElement(WKPageRef page, WKHitTestResultRef hitTestResult, WKEventModifiers modifiers, WKTypeRef userData, const void* clientInfo)
149 {
150     const QUrl absoluteLinkUrl = WKURLCopyQUrl(adoptWK(WKHitTestResultCopyAbsoluteLinkURL(hitTestResult)).get());
151     const QString linkTitle = WKStringCopyQString(adoptWK(WKHitTestResultCopyLinkTitle(hitTestResult)).get());
152     toQtWebPageUIClient(clientInfo)->mouseDidMoveOverElement(absoluteLinkUrl, linkTitle);
153 }
154
155 void QtWebPageUIClient::policyForGeolocationPermissionRequest(WKPageRef page, WKFrameRef frame, WKSecurityOriginRef origin, WKGeolocationPermissionRequestRef request, const void* clientInfo)
156 {
157     if (!request)
158         return;
159
160     QWebPermissionRequest* req = QWebPermissionRequest::create(origin, request);
161     toQtWebPageUIClient(clientInfo)->permissionRequest(req);
162 }
163
164 } // namespace WebKit