[Qt] [WK2] Allow user to inject JS scripts when the page loads
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / qt / qquickwebview_p.h
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 #ifndef qquickwebview_p_h
22 #define qquickwebview_p_h
23
24 #include "qquickurlschemedelegate_p.h"
25 #include "qwebkitglobal.h"
26 #include <QtQml/qqmllist.h>
27 #include <QtQuick/qquickitem.h>
28 #include <private/qquickflickable_p.h>
29
30 class QWebNavigationRequest;
31 class QQmlComponent;
32 class QQuickWebPage;
33 class QQuickWebViewAttached;
34 class QWebLoadRequest;
35 class QQuickWebViewPrivate;
36 class QQuickWebViewExperimental;
37 class QWebDownloadItem;
38 class QWebNavigationHistory;
39 class QWebPreferences;
40 class QWebPermissionRequest;
41 class QWebKitTest;
42 class QQuickNetworkReply;
43
44 namespace WTR {
45 class PlatformWebView;
46 }
47
48 namespace WebKit {
49 class QtRefCountedNetworkRequestData;
50 class QtViewportInteractionEngine;
51 class QtWebPageLoadClient;
52 class QtWebPagePolicyClient;
53 class QtWebPageUIClient;
54 }
55
56 namespace WTF {
57 template<class T> class PassRefPtr;
58 }
59
60 typedef const struct OpaqueWKContext* WKContextRef;
61 typedef const struct OpaqueWKPageGroup* WKPageGroupRef;
62 typedef const struct OpaqueWKPage* WKPageRef;
63
64 QT_BEGIN_NAMESPACE
65 class QPainter;
66 class QUrl;
67 class QQuickFlickable;
68 class QJSValue;
69 QT_END_NAMESPACE
70
71
72 // Instantiating the WebView in C++ is only possible by creating
73 // a QQmlComponent as the initialization depends on the
74 // componentComplete method being called.
75 class QWEBKIT_EXPORT QQuickWebView : public QQuickFlickable {
76     Q_OBJECT
77     Q_PROPERTY(QString title READ title NOTIFY titleChanged)
78     Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
79     Q_PROPERTY(QUrl icon READ icon NOTIFY iconChanged FINAL)
80     Q_PROPERTY(bool canGoBack READ canGoBack NOTIFY navigationHistoryChanged FINAL)
81     Q_PROPERTY(bool canGoForward READ canGoForward NOTIFY navigationHistoryChanged FINAL)
82     Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged FINAL)
83     Q_PROPERTY(int loadProgress READ loadProgress NOTIFY loadProgressChanged)
84     Q_ENUMS(NavigationRequestAction)
85     Q_ENUMS(LoadStatus)
86     Q_ENUMS(ErrorDomain)
87     Q_ENUMS(NavigationType)
88
89 public:
90     enum NavigationRequestAction {
91         AcceptRequest,
92         // Make room in the valid range of the enum for extra actions exposed in Experimental.
93         IgnoreRequest = 0xFF
94     };
95     enum LoadStatus {
96         LoadStartedStatus,
97         LoadSucceededStatus,
98         LoadFailedStatus
99     };
100     enum ErrorDomain {
101         NoErrorDomain,
102         InternalErrorDomain,
103         NetworkErrorDomain,
104         HttpErrorDomain,
105         DownloadErrorDomain
106     };
107
108     enum NavigationType {
109         LinkClickedNavigation,
110         FormSubmittedNavigation,
111         BackForwardNavigation,
112         ReloadNavigation,
113         FormResubmittedNavigation,
114         OtherNavigation
115     };
116
117     QQuickWebView(QQuickItem* parent = 0);
118     virtual ~QQuickWebView();
119
120     QUrl url() const;
121     void setUrl(const QUrl&);
122     QUrl icon() const;
123     QString title() const;
124     int loadProgress() const;
125
126     bool canGoBack() const;
127     bool canGoForward() const;
128     bool loading() const;
129
130     virtual QVariant inputMethodQuery(Qt::InputMethodQuery property) const;
131
132     QPointF mapToWebContent(const QPointF&) const;
133     QRectF mapRectToWebContent(const QRectF&) const;
134     QPointF mapFromWebContent(const QPointF&) const;
135     QRectF mapRectFromWebContent(const QRectF&) const;
136
137     QQuickWebPage* page();
138
139     QQuickWebViewExperimental* experimental() const;
140     static QQuickWebViewAttached* qmlAttachedProperties(QObject*);
141
142     static void platformInitialize(); // Only needed by WTR.
143
144     // Private C++-only API.
145     qreal zoomFactor() const;
146     void setZoomFactor(qreal);
147     void runJavaScriptInMainFrame(const QString& script, QObject* receiver, const char* method);
148     // Used to automatically accept the HTTPS certificate in WTR. No other use intended.
149     bool allowAnyHTTPSCertificateForLocalHost() const;
150     void setAllowAnyHTTPSCertificateForLocalHost(bool allow);
151
152 public Q_SLOTS:
153     void loadHtml(const QString& html, const QUrl& baseUrl = QUrl(), const QUrl& unreachableUrl = QUrl());
154
155     void goBack();
156     void goForward();
157     void stop();
158     void reload();
159
160 Q_SIGNALS:
161     void titleChanged();
162     void navigationHistoryChanged();
163     void loadingChanged(QWebLoadRequest* loadRequest);
164     void loadProgressChanged();
165     void urlChanged();
166     void iconChanged();
167     void linkHovered(const QUrl& hoveredUrl, const QString& hoveredTitle);
168     void navigationRequested(QWebNavigationRequest* request);
169
170 protected:
171     virtual void geometryChanged(const QRectF&, const QRectF&);
172     virtual void componentComplete();
173     virtual void keyPressEvent(QKeyEvent*);
174     virtual void keyReleaseEvent(QKeyEvent*);
175     virtual void inputMethodEvent(QInputMethodEvent*);
176     virtual void focusInEvent(QFocusEvent*);
177     virtual void focusOutEvent(QFocusEvent*);
178     virtual void touchEvent(QTouchEvent*);
179     virtual void mousePressEvent(QMouseEvent*);
180     virtual void mouseMoveEvent(QMouseEvent*);
181     virtual void mouseReleaseEvent(QMouseEvent *);
182     virtual void mouseDoubleClickEvent(QMouseEvent*);
183     virtual void wheelEvent(QWheelEvent*);
184     virtual void hoverEnterEvent(QHoverEvent*);
185     virtual void hoverMoveEvent(QHoverEvent*);
186     virtual void hoverLeaveEvent(QHoverEvent*);
187     virtual void dragMoveEvent(QDragMoveEvent*);
188     virtual void dragEnterEvent(QDragEnterEvent*);
189     virtual void dragLeaveEvent(QDragLeaveEvent*);
190     virtual void dropEvent(QDropEvent*);
191     virtual bool event(QEvent*);
192
193 private:
194     Q_DECLARE_PRIVATE(QQuickWebView)
195
196     void handleFlickableMousePress(const QPointF& position, qint64 eventTimestampMillis);
197     void handleFlickableMouseMove(const QPointF& position, qint64 eventTimestampMillis);
198     void handleFlickableMouseRelease(const QPointF& position, qint64 eventTimestampMillis);
199
200     QPointF contentPos() const;
201     void setContentPos(const QPointF&);
202
203     QQuickWebView(WKContextRef, WKPageGroupRef, QQuickItem* parent = 0);
204     WKPageRef pageRef() const;
205
206     void emitUrlChangeIfNeeded();
207
208     Q_PRIVATE_SLOT(d_func(), void _q_suspend());
209     Q_PRIVATE_SLOT(d_func(), void _q_resume());
210     Q_PRIVATE_SLOT(d_func(), void _q_onInformVisibleContentChange(const QPointF&));
211
212     Q_PRIVATE_SLOT(d_func(), void _q_onVisibleChanged());
213     Q_PRIVATE_SLOT(d_func(), void _q_onUrlChanged());
214     Q_PRIVATE_SLOT(d_func(), void _q_onReceivedResponseFromDownload(QWebDownloadItem*));
215     Q_PRIVATE_SLOT(d_func(), void _q_onIconChangedForPageURL(const QUrl&, const QUrl&));
216     // Hides QObject::d_ptr allowing us to use the convenience macros.
217     QScopedPointer<QQuickWebViewPrivate> d_ptr;
218     QQuickWebViewExperimental* m_experimental;
219
220     friend class WebKit::QtViewportInteractionEngine;
221     friend class WebKit::QtWebPageLoadClient;
222     friend class WebKit::QtWebPagePolicyClient;
223     friend class WebKit::QtWebPageUIClient;
224     friend class WTR::PlatformWebView;
225     friend class QQuickWebViewExperimental;
226 };
227
228 QML_DECLARE_TYPE(QQuickWebView)
229
230 class QWEBKIT_EXPORT QQuickWebViewAttached : public QObject {
231     Q_OBJECT
232     Q_PROPERTY(QQuickWebView* view READ view NOTIFY viewChanged FINAL)
233
234 public:
235     QQuickWebViewAttached(QObject* object);
236     QQuickWebView* view() const { return m_view; }
237     void setView(QQuickWebView*);
238
239 Q_SIGNALS:
240     void viewChanged();
241
242 private:
243     QQuickWebView* m_view;
244 };
245
246 QML_DECLARE_TYPEINFO(QQuickWebView, QML_HAS_ATTACHED_PROPERTIES)
247
248 class QWEBKIT_EXPORT QQuickWebViewExperimental : public QObject {
249     Q_OBJECT
250     Q_PROPERTY(QQuickWebPage* page READ page CONSTANT FINAL)
251
252     Q_PROPERTY(bool transparentBackground WRITE setTransparentBackground READ transparentBackground)
253     Q_PROPERTY(bool useDefaultContentItemSize WRITE setUseDefaultContentItemSize READ useDefaultContentItemSize)
254     Q_PROPERTY(int preferredMinimumContentsWidth WRITE setPreferredMinimumContentsWidth READ preferredMinimumContentsWidth)
255
256     Q_PROPERTY(QWebNavigationHistory* navigationHistory READ navigationHistory CONSTANT FINAL)
257     Q_PROPERTY(QQmlComponent* alertDialog READ alertDialog WRITE setAlertDialog NOTIFY alertDialogChanged)
258     Q_PROPERTY(QQmlComponent* confirmDialog READ confirmDialog WRITE setConfirmDialog NOTIFY confirmDialogChanged)
259     Q_PROPERTY(QQmlComponent* promptDialog READ promptDialog WRITE setPromptDialog NOTIFY promptDialogChanged)
260     Q_PROPERTY(QQmlComponent* authenticationDialog READ authenticationDialog WRITE setAuthenticationDialog NOTIFY authenticationDialogChanged)
261     Q_PROPERTY(QQmlComponent* proxyAuthenticationDialog READ proxyAuthenticationDialog WRITE setProxyAuthenticationDialog NOTIFY proxyAuthenticationDialogChanged)
262     Q_PROPERTY(QQmlComponent* certificateVerificationDialog READ certificateVerificationDialog WRITE setCertificateVerificationDialog NOTIFY certificateVerificationDialogChanged)
263     Q_PROPERTY(QQmlComponent* itemSelector READ itemSelector WRITE setItemSelector NOTIFY itemSelectorChanged)
264     Q_PROPERTY(QQmlComponent* filePicker READ filePicker WRITE setFilePicker NOTIFY filePickerChanged)
265     Q_PROPERTY(QQmlComponent* databaseQuotaDialog READ databaseQuotaDialog WRITE setDatabaseQuotaDialog NOTIFY databaseQuotaDialogChanged)
266     Q_PROPERTY(QWebPreferences* preferences READ preferences CONSTANT FINAL)
267     Q_PROPERTY(QWebKitTest* test READ test CONSTANT FINAL)
268     Q_PROPERTY(QQmlListProperty<QQuickUrlSchemeDelegate> urlSchemeDelegates READ schemeDelegates)
269     Q_PROPERTY(QString userAgent READ userAgent WRITE setUserAgent NOTIFY userAgentChanged)
270     Q_PROPERTY(double devicePixelRatio READ devicePixelRatio WRITE setDevicePixelRatio NOTIFY devicePixelRatioChanged)
271     Q_PROPERTY(QList<QUrl> userScripts READ userScripts WRITE setUserScripts NOTIFY userScriptsChanged)
272     Q_ENUMS(NavigationRequestActionExperimental)
273
274 public:
275     enum NavigationRequestActionExperimental {
276         DownloadRequest = QQuickWebView::IgnoreRequest - 1
277     };
278
279     QQuickWebViewExperimental(QQuickWebView* webView);
280     virtual ~QQuickWebViewExperimental();
281
282     QQmlComponent* alertDialog() const;
283     void setAlertDialog(QQmlComponent*);
284     QQmlComponent* confirmDialog() const;
285     void setConfirmDialog(QQmlComponent*);
286     QQmlComponent* promptDialog() const;
287     void setPromptDialog(QQmlComponent*);
288     QQmlComponent* authenticationDialog() const;
289     void setAuthenticationDialog(QQmlComponent*);
290     QQmlComponent* certificateVerificationDialog() const;
291     void setCertificateVerificationDialog(QQmlComponent*);
292     QQmlComponent* itemSelector() const;
293     void setItemSelector(QQmlComponent*);
294     QQmlComponent* proxyAuthenticationDialog() const;
295     void setProxyAuthenticationDialog(QQmlComponent*);
296     QQmlComponent* filePicker() const;
297     void setFilePicker(QQmlComponent*);
298     QQmlComponent* databaseQuotaDialog() const;
299     void setDatabaseQuotaDialog(QQmlComponent*);
300     QString userAgent() const;
301     void setUserAgent(const QString& userAgent);
302     double devicePixelRatio() const;
303     void setDevicePixelRatio(double);
304     QList<QUrl> userScripts() const;
305     void setUserScripts(const QList<QUrl>& userScripts);
306
307     QWebKitTest* test();
308
309     QWebPreferences* preferences() const;
310     QWebNavigationHistory* navigationHistory() const;
311     QQuickWebPage* page();
312
313     static QQuickUrlSchemeDelegate* schemeDelegates_At(QQmlListProperty<QQuickUrlSchemeDelegate>*, int index);
314     static void schemeDelegates_Append(QQmlListProperty<QQuickUrlSchemeDelegate>*, QQuickUrlSchemeDelegate*);
315     static int schemeDelegates_Count(QQmlListProperty<QQuickUrlSchemeDelegate>*);
316     static void schemeDelegates_Clear(QQmlListProperty<QQuickUrlSchemeDelegate>*);
317     QQmlListProperty<QQuickUrlSchemeDelegate> schemeDelegates();
318     void invokeApplicationSchemeHandler(WTF::PassRefPtr<WebKit::QtRefCountedNetworkRequestData>);
319     void sendApplicationSchemeReply(QQuickNetworkReply*);
320
321     bool transparentBackground() const;
322     void setTransparentBackground(bool);
323
324     bool useDefaultContentItemSize() const;
325     void setUseDefaultContentItemSize(bool enable);
326
327     int preferredMinimumContentsWidth() const;
328     void setPreferredMinimumContentsWidth(int);
329
330     // C++ only
331     bool renderToOffscreenBuffer() const;
332     void setRenderToOffscreenBuffer(bool enable);
333     static void setFlickableViewportEnabled(bool enable);
334     static bool flickableViewportEnabled();
335
336 public Q_SLOTS:
337     void goBackTo(int index);
338     void goForwardTo(int index);
339     void postMessage(const QString&);
340     void evaluateJavaScript(const QString& script, const QJSValue& value = QJSValue());
341
342 Q_SIGNALS:
343     void alertDialogChanged();
344     void confirmDialogChanged();
345     void promptDialogChanged();
346     void authenticationDialogChanged();
347     void certificateVerificationDialogChanged();
348     void itemSelectorChanged();
349     void filePickerChanged();
350     void databaseQuotaDialogChanged();
351     void downloadRequested(QWebDownloadItem* downloadItem);
352     void permissionRequested(QWebPermissionRequest* permission);
353     void messageReceived(const QVariantMap& message);
354     void proxyAuthenticationDialogChanged();
355     void userAgentChanged();
356     void devicePixelRatioChanged();
357     void enterFullScreenRequested();
358     void exitFullScreenRequested();
359     void userScriptsChanged();
360
361 private:
362     QQuickWebView* q_ptr;
363     QQuickWebViewPrivate* d_ptr;
364     QObject* schemeParent;
365     QWebKitTest* m_test;
366
367     friend class WebKit::QtWebPageUIClient;
368
369     Q_DECLARE_PRIVATE(QQuickWebView)
370     Q_DECLARE_PUBLIC(QQuickWebView)
371 };
372
373 #endif // qquickwebview_p_h