1/*
2 * Copyright (C) 2012 Digia Plc 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 library 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 library; 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#ifndef QWebPageAdapter_h
21#define QWebPageAdapter_h
22
23#include "PlatformExportMacros.h"
24#include "QWebPageClient.h"
25#include "ViewportArguments.h"
26#include "qwebhistory.h"
27
28#include <qbasictimer.h>
29#include <qevent.h>
30#include <qnetworkrequest.h>
31#include <qrect.h>
32#include <qscopedpointer.h>
33#include <qsharedpointer.h>
34#include <qstring.h>
35#include <qurl.h>
36#include <wtf/ExportMacros.h>
37
38QT_BEGIN_NAMESPACE
39class QBitArray;
40class QKeyEvent;
41class QMimeData;
42class QMouseEvent;
43class QNetworkAccessManager;
44class QWheelEvent;
45class QInputMethodEvent;
46QT_END_NAMESPACE
47
48namespace WebCore {
49class ChromeClientQt;
50class GeolocationClientQt;
51class Page;
52class UndoStep;
53}
54
55class QtPluginWidgetAdapter;
56class QWebFrameAdapter;
57class QWebHistoryItem;
58class QWebHitTestResultPrivate;
59class QWebPageClient;
60class QWebPluginFactory;
61class QWebSecurityOrigin;
62class QWebSelectMethod;
63class QWebSettings;
64class QWebFullScreenVideoHandler;
65class UndoStepQt;
66
67class WEBKIT_EXPORTDATA QWebPageAdapter {
68public:
69
70#define FOR_EACH_MAPPED_MENU_ACTION(F, SEPARATOR) \
71    F(OpenLink, WebCore::ContextMenuItemTagOpenLink) SEPARATOR \
72    F(OpenLinkInNewWindow, WebCore::ContextMenuItemTagOpenLinkInNewWindow) SEPARATOR \
73    F(OpenLinkInThisWindow, WebCore::ContextMenuItemTagOpenLinkInThisWindow) SEPARATOR \
74    F(DownloadLinkToDisk, WebCore::ContextMenuItemTagDownloadLinkToDisk) SEPARATOR \
75    F(CopyLinkToClipboard, WebCore::ContextMenuItemTagCopyLinkToClipboard) SEPARATOR \
76    F(OpenImageInNewWindow, WebCore::ContextMenuItemTagOpenImageInNewWindow) SEPARATOR \
77    F(DownloadImageToDisk, WebCore::ContextMenuItemTagDownloadImageToDisk) SEPARATOR \
78    F(CopyImageToClipboard, WebCore::ContextMenuItemTagCopyImageToClipboard) SEPARATOR \
79    F(CopyImageUrlToClipboard, WebCore::ContextMenuItemTagCopyImageUrlToClipboard) SEPARATOR \
80    F(OpenFrameInNewWindow, WebCore::ContextMenuItemTagOpenFrameInNewWindow) SEPARATOR \
81    F(Copy, WebCore::ContextMenuItemTagCopy) SEPARATOR \
82    F(Back, WebCore::ContextMenuItemTagGoBack) SEPARATOR \
83    F(Forward, WebCore::ContextMenuItemTagGoForward) SEPARATOR \
84    F(Stop, WebCore::ContextMenuItemTagStop) SEPARATOR \
85    F(Reload, WebCore::ContextMenuItemTagReload) SEPARATOR \
86    F(Cut, WebCore::ContextMenuItemTagCut) SEPARATOR \
87    F(Paste, WebCore::ContextMenuItemTagPaste) SEPARATOR \
88    F(SetTextDirectionDefault, WebCore::ContextMenuItemTagDefaultDirection) SEPARATOR \
89    F(SetTextDirectionLeftToRight, WebCore::ContextMenuItemTagLeftToRight) SEPARATOR \
90    F(SetTextDirectionRightToLeft, WebCore::ContextMenuItemTagRightToLeft) SEPARATOR \
91    F(ToggleBold, WebCore::ContextMenuItemTagBold) SEPARATOR \
92    F(ToggleItalic, WebCore::ContextMenuItemTagItalic) SEPARATOR \
93    F(ToggleUnderline, WebCore::ContextMenuItemTagUnderline) SEPARATOR \
94    F(SelectAll, WebCore::ContextMenuItemTagSelectAll)
95#define COMMA_SEPARATOR ,
96#define SEMICOLON_SEPARATOR ;
97#define DEFINE_ACTION(Name, Value) \
98    Name
99
100    enum MenuAction {
101        NoAction = - 1,
102        FOR_EACH_MAPPED_MENU_ACTION(DEFINE_ACTION, COMMA_SEPARATOR)
103#if ENABLE(INSPECTOR)
104        , InspectElement
105#endif
106        , ActionCount
107    };
108
109    // Duplicated from qwebpage.h
110    enum FindFlag {
111        FindBackward = 1,
112        FindCaseSensitively = 2,
113        FindWrapsAroundDocument = 4,
114        HighlightAllOccurrences = 8,
115        FindAtWordBeginningsOnly = 16,
116        TreatMedialCapitalAsWordBeginning = 32,
117        FindBeginsInSelection = 64
118    };
119
120    // valid values matching those from ScrollTypes.h
121    enum ScrollDirection {
122        InvalidScrollDirection = -1,
123        ScrollUp,
124        ScrollDown,
125        ScrollLeft,
126        ScrollRight
127    };
128    // same here
129    enum ScrollGranularity {
130        InvalidScrollGranularity = -1,
131        ScrollByLine,
132        ScrollByPage,
133        ScrollByDocument
134    };
135
136    QWebPageAdapter();
137    virtual ~QWebPageAdapter();
138
139    // Called manually from ~QWebPage destructor to ensure that
140    // the QWebPageAdapter and the QWebPagePrivate are intact when
141    // various destruction callbacks from WebCore::Page::~Page() hit us.
142    void deletePage();
143    // For similar reasons, we don't want to create the WebCore Page before
144    // we properly initialized the style factory callbacks.
145    void initializeWebCorePage();
146
147    virtual void show() = 0;
148    virtual void setFocus() = 0;
149    virtual void unfocus() = 0;
150    virtual void setWindowRect(const QRect&) = 0;
151    virtual QSize viewportSize() const = 0;
152    virtual QWebPageAdapter* createWindow(bool /*dialog*/) = 0;
153    virtual QObject* handle() = 0;
154    virtual void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID) = 0;
155    virtual void javaScriptAlert(QWebFrameAdapter*, const QString& msg) = 0;
156    virtual bool javaScriptConfirm(QWebFrameAdapter*, const QString& msg) = 0;
157    virtual bool javaScriptPrompt(QWebFrameAdapter*, const QString& msg, const QString& defaultValue, QString* result) = 0;
158    virtual bool shouldInterruptJavaScript() = 0;
159    virtual void printRequested(QWebFrameAdapter*) = 0;
160    virtual void databaseQuotaExceeded(QWebFrameAdapter*, const QString& databaseName) = 0;
161    virtual void applicationCacheQuotaExceeded(QWebSecurityOrigin*, quint64 defaultOriginQuota, quint64 totalSpaceNeeded) = 0;
162    virtual void setToolTip(const QString&) = 0;
163    virtual QStringList chooseFiles(QWebFrameAdapter*, bool allowMultiple, const QStringList& suggestedFileNames) = 0;
164    virtual QColor colorSelectionRequested(const QColor& selectedColor) = 0;
165    virtual QWebSelectMethod* createSelectPopup() = 0;
166    virtual QRect viewRectRelativeToWindow() = 0;
167
168#if USE(QT_MULTIMEDIA)
169    virtual QWebFullScreenVideoHandler* createFullScreenVideoHandler() = 0;
170#endif
171    virtual void geolocationPermissionRequested(QWebFrameAdapter*) = 0;
172    virtual void geolocationPermissionRequestCancelled(QWebFrameAdapter*) = 0;
173    virtual void notificationsPermissionRequested(QWebFrameAdapter*) = 0;
174    virtual void notificationsPermissionRequestCancelled(QWebFrameAdapter*) = 0;
175
176    virtual void respondToChangedContents() = 0;
177    virtual void respondToChangedSelection() = 0;
178    virtual void microFocusChanged() = 0;
179    virtual void triggerCopyAction() = 0;
180    virtual void triggerActionForKeyEvent(QKeyEvent*) = 0;
181    virtual void clearUndoStack() = 0;
182    virtual bool canUndo() const = 0;
183    virtual bool canRedo() const = 0;
184    virtual void undo() = 0;
185    virtual void redo() = 0;
186    virtual const char* editorCommandForKeyEvent(QKeyEvent*) = 0;
187    virtual void createUndoStep(QSharedPointer<UndoStepQt>) = 0;
188
189    virtual void updateNavigationActions() = 0;
190
191    virtual QWebFrameAdapter* mainFrameAdapter() = 0;
192
193    virtual QObject* inspectorHandle() = 0;
194    virtual void setInspectorFrontend(QObject*) = 0;
195    virtual void setInspectorWindowTitle(const QString&) = 0;
196    virtual void createWebInspector(QObject** inspectorView, QWebPageAdapter** inspectorPage) = 0;
197    virtual QStringList menuActionsAsText() = 0;
198    virtual void emitViewportChangeRequested() = 0;
199    virtual bool acceptNavigationRequest(QWebFrameAdapter*, const QNetworkRequest&, int type) = 0;
200    virtual void emitRestoreFrameStateRequested(QWebFrameAdapter *) = 0;
201    virtual void emitSaveFrameStateRequested(QWebFrameAdapter *, QWebHistoryItem*) = 0;
202    virtual void emitDownloadRequested(const QNetworkRequest&) = 0;
203    virtual void emitFrameCreated(QWebFrameAdapter*) = 0;
204    virtual QString userAgentForUrl(const QUrl&) const = 0;
205    virtual bool supportsErrorPageExtension() const = 0;
206    struct ErrorPageOption {
207        QUrl url;
208        QWebFrameAdapter* frame;
209        QString domain;
210        int error;
211        QString errorString;
212    };
213    struct ErrorPageReturn {
214        QString contentType;
215        QString encoding;
216        QUrl baseUrl;
217        QByteArray content;
218    };
219    virtual bool errorPageExtension(ErrorPageOption*, ErrorPageReturn*) = 0;
220    virtual QtPluginWidgetAdapter* createPlugin(const QString&, const QUrl&, const QStringList&, const QStringList&) = 0;
221    virtual QtPluginWidgetAdapter* adapterForWidget(QObject*) const = 0;
222    virtual bool requestSoftwareInputPanel() const = 0;
223    struct MenuItemDescription {
224        MenuItemDescription()
225            : type(NoType)
226            , action(NoAction)
227            , traits(None)
228        { }
229        enum Type {
230            NoType,
231            Action,
232            Separator,
233            SubMenu
234        } type;
235        MenuAction action;
236        enum Trait {
237            None = 0,
238            Enabled = 1,
239            Checkable = 2,
240            Checked = 4
241        };
242        Q_DECLARE_FLAGS(Traits, Trait);
243        Traits traits;
244        QList<MenuItemDescription> subMenu;
245        QString subMenuTitle;
246    };
247    virtual void createAndSetCurrentContextMenu(const QList<MenuItemDescription>&, QBitArray*) = 0;
248    virtual bool handleScrollbarContextMenuEvent(QContextMenuEvent*, bool, ScrollDirection*, ScrollGranularity*) = 0;
249
250    static QWebPageAdapter* kit(WebCore::Page*);
251    WebCore::ViewportArguments viewportArguments() const;
252    void registerUndoStep(WTF::PassRefPtr<WebCore::UndoStep>);
253
254    void setNetworkAccessManager(QNetworkAccessManager*);
255    QNetworkAccessManager* networkAccessManager();
256
257    bool hasSelection() const;
258    QString selectedText() const;
259    QString selectedHtml() const;
260
261    bool isContentEditable() const;
262    void setContentEditable(bool);
263
264    bool findText(const QString& subString, FindFlag options);
265
266    void adjustPointForClicking(QMouseEvent*);
267
268    void mouseMoveEvent(QMouseEvent*);
269    void mousePressEvent(QMouseEvent*);
270    void mouseDoubleClickEvent(QMouseEvent*);
271    void mouseTripleClickEvent(QMouseEvent*);
272    void mouseReleaseEvent(QMouseEvent*);
273    void handleSoftwareInputPanel(Qt::MouseButton, const QPoint&);
274#ifndef QT_NO_WHEELEVENT
275    void wheelEvent(QWheelEvent*, int wheelScrollLines);
276#endif
277#ifndef QT_NO_DRAGANDDROP
278    Qt::DropAction dragEntered(const QMimeData*, const QPoint&, Qt::DropActions);
279    void dragLeaveEvent();
280    Qt::DropAction dragUpdated(const QMimeData*, const QPoint&, Qt::DropActions);
281    bool performDrag(const QMimeData*, const QPoint&, Qt::DropActions);
282#endif
283    void inputMethodEvent(QInputMethodEvent*);
284    QVariant inputMethodQuery(Qt::InputMethodQuery property) const;
285    void dynamicPropertyChangeEvent(QObject*, QDynamicPropertyChangeEvent*);
286    bool handleKeyEvent(QKeyEvent*);
287    bool handleScrolling(QKeyEvent*);
288    void focusInEvent(QFocusEvent*);
289    void focusOutEvent(QFocusEvent*);
290    bool handleShortcutOverrideEvent(QKeyEvent*);
291    // Returns whether the default action was cancelled in the JS event handler
292    bool touchEvent(QTouchEvent*);
293    bool swallowContextMenuEvent(QContextMenuEvent *, QWebFrameAdapter*);
294
295    QWebHitTestResultPrivate* updatePositionDependentMenuActions(const QPoint&, QBitArray*);
296    void updateActionInternal(MenuAction, const char* commandName, bool* enabled, bool* checked);
297    void triggerAction(MenuAction, QWebHitTestResultPrivate*, const char* commandName, bool endToEndReload);
298    QString contextMenuItemTagForAction(MenuAction, bool* checkable) const;
299
300    QStringList supportedContentTypes() const;
301#if ENABLE(GEOLOCATION) && HAVE(QTLOCATION)
302    void setGeolocationEnabledForFrame(QWebFrameAdapter*, bool);
303#endif
304#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
305    void setNotificationsAllowedForFrame(QWebFrameAdapter*, bool allowed);
306    void addNotificationPresenterClient();
307#ifndef QT_NO_SYSTEMTRAYICON
308    bool hasSystemTrayIcon() const;
309    void setSystemTrayIcon(QObject*);
310#endif // QT_NO_SYSTEMTRAYICON
311#endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
312
313    // Called from QWebPage as private slots.
314    void _q_cleanupLeakMessages();
315    void _q_onLoadProgressChanged(int);
316
317    bool supportsContentType(const QString& mimeType) const;
318
319    void didShowInspector();
320    void didCloseInspector();
321
322    static QString defaultUserAgentString();
323    static bool treatSchemeAsLocal(const QString&);
324
325    QObject* currentFrame() const;
326    bool hasFocusedNode() const;
327    struct ViewportAttributes {
328        qreal initialScaleFactor;
329        qreal minimumScaleFactor;
330        qreal maximumScaleFactor;
331        qreal devicePixelRatio;
332        bool isUserScalable;
333        QSizeF size;
334    };
335
336    ViewportAttributes viewportAttributesForSize(const QSize& availableSize, const QSize& deviceSize) const;
337
338    QWebSettings *settings;
339
340    WebCore::Page *page;
341    QScopedPointer<QWebPageClient> client;
342
343    QWebPluginFactory *pluginFactory;
344
345    bool forwardUnsupportedContent;
346    bool insideOpenCall;
347    QPoint tripleClick;
348    QBasicTimer tripleClickTimer;
349
350    bool clickCausedFocus;
351    quint64 m_totalBytes;
352    quint64 m_bytesReceived;
353    QWebHistory history;
354
355private:
356    QNetworkAccessManager *networkManager;
357
358public:
359    static bool drtRun;
360
361    friend class WebCore::ChromeClientQt;
362    friend class WebCore::GeolocationClientQt;
363};
364
365#endif // QWebPageAdapter_h
366