d1d00ee986039c033673d2907a45a5b81680c0cb
[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 QDeclarativeComponent;
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 QWebViewportInfo;
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 QT_END_NAMESPACE
69
70
71 // Instantiating the WebView in C++ is only possible by creating
72 // a QDeclarativeComponent as the initialization depends on the
73 // componentComplete method being called.
74 class QWEBKIT_EXPORT QQuickWebView : public QQuickFlickable {
75     Q_OBJECT
76     Q_PROPERTY(QString title READ title NOTIFY titleChanged)
77     Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged)
78     Q_PROPERTY(QUrl icon READ icon NOTIFY iconChanged FINAL)
79     Q_PROPERTY(bool canGoBack READ canGoBack NOTIFY navigationHistoryChanged FINAL)
80     Q_PROPERTY(bool canGoForward READ canGoForward NOTIFY navigationHistoryChanged FINAL)
81     Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged FINAL)
82     Q_PROPERTY(int loadProgress READ loadProgress NOTIFY loadProgressChanged)
83     Q_ENUMS(NavigationRequestAction)
84     Q_ENUMS(LoadStatus)
85     Q_ENUMS(ErrorDomain)
86     Q_ENUMS(NavigationType)
87
88 public:
89     enum NavigationRequestAction {
90         AcceptRequest,
91         // Make room in the valid range of the enum for extra actions exposed in Experimental.
92         IgnoreRequest = 0xFF
93     };
94     enum LoadStatus {
95         LoadStartedStatus,
96         LoadSucceededStatus,
97         LoadFailedStatus
98     };
99     enum ErrorDomain {
100         NoErrorDomain,
101         InternalErrorDomain,
102         NetworkErrorDomain,
103         HttpErrorDomain,
104         DownloadErrorDomain
105     };
106
107     enum NavigationType {
108         LinkClickedNavigation,
109         FormSubmittedNavigation,
110         BackForwardNavigation,
111         ReloadNavigation,
112         FormResubmittedNavigation,
113         OtherNavigation
114     };
115
116     QQuickWebView(QQuickItem* parent = 0);
117     virtual ~QQuickWebView();
118
119     QUrl url() const;
120     void setUrl(const QUrl&);
121     QUrl icon() const;
122     QString title() const;
123     int loadProgress() const;
124
125     bool canGoBack() const;
126     bool canGoForward() const;
127     bool loading() const;
128
129     virtual QVariant inputMethodQuery(Qt::InputMethodQuery property) const;
130
131     QPointF mapToWebContent(const QPointF&) const;
132     QRectF mapRectToWebContent(const QRectF&) const;
133     QPointF mapFromWebContent(const QPointF&) const;
134     QRectF mapRectFromWebContent(const QRectF&) const;
135
136     QQuickWebPage* page();
137
138     QQuickWebViewExperimental* experimental() const;
139     static QQuickWebViewAttached* qmlAttachedProperties(QObject*);
140
141     static void platformInitialize(); // Only needed by WTR.
142
143     // Internal API used by WebPage.
144     void updateContentsSize(const QSizeF&);
145     QPointF pageItemPos();
146
147     // Private C++-only API.
148     qreal zoomFactor() const;
149     void setZoomFactor(qreal);
150     void runJavaScriptInMainFrame(const QString& script, QObject* receiver, const char* method);
151
152 public Q_SLOTS:
153     void loadHtml(const QString& html, const QUrl& baseUrl = 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     Q_PRIVATE_SLOT(d_func(), void _q_suspend());
207     Q_PRIVATE_SLOT(d_func(), void _q_resume());
208     Q_PRIVATE_SLOT(d_func(), void _q_contentViewportChanged(const QPointF&));
209
210     Q_PRIVATE_SLOT(d_func(), void _q_onVisibleChanged());
211     Q_PRIVATE_SLOT(d_func(), void _q_onUrlChanged());
212     Q_PRIVATE_SLOT(d_func(), void _q_onReceivedResponseFromDownload(QWebDownloadItem*));
213     Q_PRIVATE_SLOT(d_func(), void _q_onIconChangedForPageURL(const QUrl&, const QUrl&));
214     // Hides QObject::d_ptr allowing us to use the convenience macros.
215     QScopedPointer<QQuickWebViewPrivate> d_ptr;
216     QQuickWebViewExperimental* m_experimental;
217
218     friend class WebKit::QtViewportInteractionEngine;
219     friend class WebKit::QtWebPageLoadClient;
220     friend class WebKit::QtWebPagePolicyClient;
221     friend class WebKit::QtWebPageUIClient;
222     friend class WTR::PlatformWebView;
223     friend class QQuickWebViewExperimental;
224 };
225
226 QML_DECLARE_TYPE(QQuickWebView)
227
228 class QWEBKIT_EXPORT QQuickWebViewAttached : public QObject {
229     Q_OBJECT
230     Q_PROPERTY(QQuickWebView* view READ view NOTIFY viewChanged FINAL)
231
232 public:
233     QQuickWebViewAttached(QObject* object);
234     QQuickWebView* view() const { return m_view; }
235     void setView(QQuickWebView*);
236
237 Q_SIGNALS:
238     void viewChanged();
239
240 private:
241     QQuickWebView* m_view;
242 };
243
244 QML_DECLARE_TYPEINFO(QQuickWebView, QML_HAS_ATTACHED_PROPERTIES)
245
246 class QWEBKIT_EXPORT QQuickWebViewExperimental : public QObject {
247     Q_OBJECT
248     Q_PROPERTY(QQuickWebPage* page READ page CONSTANT FINAL)
249
250     Q_PROPERTY(bool transparentBackground WRITE setTransparentBackground READ transparentBackground)
251     Q_PROPERTY(bool useDefaultContentItemSize WRITE setUseDefaultContentItemSize READ useDefaultContentItemSize)
252     Q_PROPERTY(int preferredMinimumContentsWidth WRITE setPreferredMinimumContentsWidth READ preferredMinimumContentsWidth)
253
254     Q_PROPERTY(QWebNavigationHistory* navigationHistory READ navigationHistory CONSTANT FINAL)
255     Q_PROPERTY(QDeclarativeComponent* alertDialog READ alertDialog WRITE setAlertDialog NOTIFY alertDialogChanged)
256     Q_PROPERTY(QDeclarativeComponent* confirmDialog READ confirmDialog WRITE setConfirmDialog NOTIFY confirmDialogChanged)
257     Q_PROPERTY(QDeclarativeComponent* promptDialog READ promptDialog WRITE setPromptDialog NOTIFY promptDialogChanged)
258     Q_PROPERTY(QDeclarativeComponent* authenticationDialog READ authenticationDialog WRITE setAuthenticationDialog NOTIFY authenticationDialogChanged)
259     Q_PROPERTY(QDeclarativeComponent* proxyAuthenticationDialog READ proxyAuthenticationDialog WRITE setProxyAuthenticationDialog NOTIFY proxyAuthenticationDialogChanged)
260     Q_PROPERTY(QDeclarativeComponent* certificateVerificationDialog READ certificateVerificationDialog WRITE setCertificateVerificationDialog NOTIFY certificateVerificationDialogChanged)
261     Q_PROPERTY(QDeclarativeComponent* itemSelector READ itemSelector WRITE setItemSelector NOTIFY itemSelectorChanged)
262     Q_PROPERTY(QDeclarativeComponent* filePicker READ filePicker WRITE setFilePicker NOTIFY filePickerChanged)
263     Q_PROPERTY(QDeclarativeComponent* databaseQuotaDialog READ databaseQuotaDialog WRITE setDatabaseQuotaDialog NOTIFY databaseQuotaDialogChanged)
264     Q_PROPERTY(QWebPreferences* preferences READ preferences CONSTANT FINAL)
265     Q_PROPERTY(QWebViewportInfo* viewportInfo READ viewportInfo CONSTANT FINAL)
266     Q_PROPERTY(QDeclarativeListProperty<QQuickUrlSchemeDelegate> urlSchemeDelegates READ schemeDelegates)
267     Q_PROPERTY(QString userAgent READ userAgent WRITE setUserAgent NOTIFY userAgentChanged)
268     Q_ENUMS(NavigationRequestActionExperimental)
269
270 public:
271     enum NavigationRequestActionExperimental {
272         DownloadRequest = QQuickWebView::IgnoreRequest - 1
273     };
274
275     QQuickWebViewExperimental(QQuickWebView* webView);
276     virtual ~QQuickWebViewExperimental();
277
278     QDeclarativeComponent* alertDialog() const;
279     void setAlertDialog(QDeclarativeComponent*);
280     QDeclarativeComponent* confirmDialog() const;
281     void setConfirmDialog(QDeclarativeComponent*);
282     QDeclarativeComponent* promptDialog() const;
283     void setPromptDialog(QDeclarativeComponent*);
284     QDeclarativeComponent* authenticationDialog() const;
285     void setAuthenticationDialog(QDeclarativeComponent*);
286     QDeclarativeComponent* certificateVerificationDialog() const;
287     void setCertificateVerificationDialog(QDeclarativeComponent*);
288     QDeclarativeComponent* itemSelector() const;
289     void setItemSelector(QDeclarativeComponent*);
290     QDeclarativeComponent* proxyAuthenticationDialog() const;
291     void setProxyAuthenticationDialog(QDeclarativeComponent*);
292     QDeclarativeComponent* filePicker() const;
293     void setFilePicker(QDeclarativeComponent*);
294     QDeclarativeComponent* databaseQuotaDialog() const;
295     void setDatabaseQuotaDialog(QDeclarativeComponent*);
296     QString userAgent() const;
297     void setUserAgent(const QString& userAgent);
298
299     QWebViewportInfo* viewportInfo();
300
301     QWebPreferences* preferences() const;
302     QWebNavigationHistory* navigationHistory() const;
303     QQuickWebPage* page();
304
305     static QQuickUrlSchemeDelegate* schemeDelegates_At(QDeclarativeListProperty<QQuickUrlSchemeDelegate>*, int index);
306     static void schemeDelegates_Append(QDeclarativeListProperty<QQuickUrlSchemeDelegate>*, QQuickUrlSchemeDelegate*);
307     static int schemeDelegates_Count(QDeclarativeListProperty<QQuickUrlSchemeDelegate>*);
308     static void schemeDelegates_Clear(QDeclarativeListProperty<QQuickUrlSchemeDelegate>*);
309     QDeclarativeListProperty<QQuickUrlSchemeDelegate> schemeDelegates();
310     void invokeApplicationSchemeHandler(WTF::PassRefPtr<WebKit::QtRefCountedNetworkRequestData>);
311     void sendApplicationSchemeReply(QQuickNetworkReply*);
312
313     bool transparentBackground() const;
314     void setTransparentBackground(bool);
315
316     bool useDefaultContentItemSize() const;
317     void setUseDefaultContentItemSize(bool enable);
318
319     int preferredMinimumContentsWidth() const;
320     void setPreferredMinimumContentsWidth(int);
321
322     // C++ only
323     bool renderToOffscreenBuffer() const;
324     void setRenderToOffscreenBuffer(bool enable);
325     static void setFlickableViewportEnabled(bool enable);
326     static bool flickableViewportEnabled();
327
328 public Q_SLOTS:
329     void goBackTo(int index);
330     void goForwardTo(int index);
331     void postMessage(const QString&);
332
333 Q_SIGNALS:
334     void alertDialogChanged();
335     void confirmDialogChanged();
336     void promptDialogChanged();
337     void authenticationDialogChanged();
338     void certificateVerificationDialogChanged();
339     void itemSelectorChanged();
340     void filePickerChanged();
341     void databaseQuotaDialogChanged();
342     void downloadRequested(QWebDownloadItem* downloadItem);
343     void permissionRequested(QWebPermissionRequest* permission);
344     void messageReceived(const QVariantMap& message);
345     void proxyAuthenticationDialogChanged();
346     void userAgentChanged();
347
348 private:
349     QQuickWebView* q_ptr;
350     QQuickWebViewPrivate* d_ptr;
351     QObject* schemeParent;
352     QWebViewportInfo* m_viewportInfo;
353
354     friend class WebKit::QtWebPageUIClient;
355
356     Q_DECLARE_PRIVATE(QQuickWebView)
357     Q_DECLARE_PUBLIC(QQuickWebView)
358 };
359
360 #endif // qquickwebview_p_h