1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2013 - 2018 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 * Copyright (C) 2015 - 2016 Jan Bajer aka bajasoft <jbajer@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 **************************************************************************/
20 
21 #ifndef OTTER_QTWEBKITWEBWIDGET_H
22 #define OTTER_QTWEBKITWEBWIDGET_H
23 
24 #include "../../../../ui/WebWidget.h"
25 
26 #include <QtCore/QQueue>
27 #include <QtNetwork/QNetworkReply>
28 #include <QtWebKitWidgets/QWebPage>
29 #include <QtWebKitWidgets/QWebView>
30 
31 namespace Otter
32 {
33 
34 class ContentsDialog;
35 class QtWebKitInspector;
36 class QtWebKitNetworkManager;
37 class QtWebKitWebBackend;
38 class QtWebKitPage;
39 class SourceViewerWebWidget;
40 
41 class QtWebKitWebWidget final : public WebWidget
42 {
43 	Q_OBJECT
44 
45 public:
46 	~QtWebKitWebWidget();
47 
48 	void search(const QString &query, const QString &searchEngine) override;
49 	void print(QPrinter *printer) override;
50 	WebWidget* clone(bool cloneHistory = true, bool isPrivate = false, const QStringList &excludedOptions = {}) const override;
51 	QWidget* getInspector() override;
52 	QWidget* getViewport() override;
53 	QString getTitle() const override;
54 	QString getDescription() const override;
55 	QString getActiveStyleSheet() const override;
56 	QString getSelectedText() const override;
57 	QVariant getPageInformation(PageInformation key) const override;
58 	QStringList getBlockedElements() const;
59 	QUrl getUrl() const override;
60 	QIcon getIcon() const override;
61 	QPixmap createThumbnail(const QSize &size = {}) override;
62 	QPoint getScrollPosition() const override;
63 	QRect getGeometry(bool excludeScrollBars = false) const override;
64 	LinkUrl getActiveFrame() const override;
65 	LinkUrl getActiveImage() const override;
66 	LinkUrl getActiveLink() const override;
67 	LinkUrl getActiveMedia() const override;
68 	SslInformation getSslInformation() const override;
69 	WindowHistoryInformation getHistory() const override;
70 	HitTestResult getHitTestResult(const QPoint &position) override;
71 	QStringList getStyleSheets() const override;
72 	QVector<LinkUrl> getFeeds() const override;
73 	QVector<LinkUrl> getLinks() const override;
74 	QVector<LinkUrl> getSearchEngines() const override;
75 	QVector<NetworkManager::ResourceInformation> getBlockedRequests() const override;
76 	QMap<QByteArray, QByteArray> getHeaders() const override;
77 	QMultiMap<QString, QString> getMetaData() const override;
78 	ContentStates getContentState() const override;
79 	LoadingState getLoadingState() const override;
80 	int getZoom() const override;
81 	int findInPage(const QString &text, FindFlags flags = NoFlagsFind) override;
82 	bool hasSelection() const override;
83 	bool hasWatchedChanges(ChangeWatcher watcher) const override;
84 	bool isAudible() const override;
85 	bool isAudioMuted() const override;
86 	bool isFullScreen() const override;
87 	bool isPrivate() const override;
88 	bool eventFilter(QObject *object, QEvent *event) override;
89 
90 public slots:
91 	void clearOptions() override;
92 	void fillPassword(const PasswordsManager::PasswordInformation &password) override;
93 	void triggerAction(int identifier, const QVariantMap &parameters = {}, ActionsManager::TriggerType trigger = ActionsManager::UnknownTrigger) override;
94 	void setActiveStyleSheet(const QString &styleSheet) override;
95 	void setPermission(FeaturePermission feature, const QUrl &url, PermissionPolicies policies) override;
96 	void setOption(int identifier, const QVariant &value) override;
97 	void setScrollPosition(const QPoint &position) override;
98 	void setHistory(const WindowHistoryInformation &history) override;
99 	void setZoom(int zoom) override;
100 	void setUrl(const QUrl &url, bool isTyped = true) override;
101 
102 protected:
103 	enum HistoryEntryData
104 	{
105 		IdentifierEntryData = 0,
106 		ZoomEntryData,
107 		PositionEntryData,
108 		VisitTimeEntryData
109 	};
110 
111 	explicit QtWebKitWebWidget(const QVariantMap &parameters, WebBackend *backend, QtWebKitNetworkManager *networkManager = nullptr, ContentsWidget *parent = nullptr);
112 
113 	void timerEvent(QTimerEvent *event) override;
114 	void showEvent(QShowEvent *event) override;
115 	void hideEvent(QHideEvent *event) override;
116 	void focusInEvent(QFocusEvent *event) override;
117 	void clearPluginToken();
118 	void resetSpellCheck(QWebElement element);
119 	void muteAudio(QWebFrame *frame, bool isMuted);
120 	void openRequest(const QNetworkRequest &request, QNetworkAccessManager::Operation operation, QIODevice *outgoingData);
121 	void openFormRequest(const QNetworkRequest &request, QNetworkAccessManager::Operation operation, QIODevice *outgoingData);
122 	void startDelayedTransfer(Transfer *transfer);
123 	void handleHistory();
124 	void handleNavigationRequest(const QUrl &url, QWebPage::NavigationType type);
125 	void setHistory(const QVariantMap &history);
126 	void setOptions(const QHash<int, QVariant> &options, const QStringList &excludedOptions = {}) override;
127 	QtWebKitPage* getPage() const;
128 	QString getMessageToken() const;
129 	QString getPluginToken() const;
130 	QUrl resolveUrl(QWebFrame *frame, const QUrl &url) const;
131 	QVector<LinkUrl> getLinks(const QString &query) const;
132 	int getAmountOfDeferredPlugins() const override;
133 	bool canLoadPlugins() const;
134 	bool canGoBack() const override;
135 	bool canGoForward() const override;
136 	bool canFastForward() const override;
137 	bool canInspect() const override;
138 	bool canTakeScreenshot() const override;
139 	bool canRedo() const override;
140 	bool canUndo() const override;
141 	bool canShowContextMenu(const QPoint &position) const override;
142 	bool canViewSource() const override;
143 	bool isInspecting() const override;
144 	bool isNavigating() const;
145 	bool isPopup() const override;
146 	bool isScrollBar(const QPoint &position) const override;
147 
148 protected slots:
149 	void saveState(QWebFrame *frame, QWebHistoryItem *item);
150 	void restoreState(QWebFrame *frame);
151 	void handleDownloadRequested(const QNetworkRequest &request);
152 	void handleUnsupportedContent(QNetworkReply *reply);
153 	void handleOptionChanged(int identifier, const QVariant &value);
154 	void handleLoadStarted();
155 	void handleLoadProgress(int progress);
156 	void handleLoadFinished(bool result);
157 	void handleViewSourceReplyFinished();
158 	void handlePrintRequest(QWebFrame *frame);
159 	void handleFullScreenRequest(QWebFullScreenRequest request);
160 	void handlePermissionRequest(QWebFrame *frame, QWebPage::Feature feature);
161 	void handlePermissionCancel(QWebFrame *frame, QWebPage::Feature feature);
162 	void notifyTitleChanged();
163 	void notifyUrlChanged(const QUrl &url);
164 	void notifyIconChanged();
165 	void notifyPermissionRequested(QWebFrame *frame, QWebPage::Feature nativeFeature, bool cancel);
166 	void notifySavePasswordRequested(const PasswordsManager::PasswordInformation &password, bool isUpdate);
167 	void updateAmountOfDeferredPlugins();
168 	void updateOptions(const QUrl &url);
169 
170 private:
171 	QWebView *m_webView;
172 	QtWebKitPage *m_page;
173 	QtWebKitInspector *m_inspector;
174 	QtWebKitNetworkManager *m_networkManager;
175 	QString m_messageToken;
176 	QString m_pluginToken;
177 	QPixmap m_thumbnail;
178 	QNetworkRequest m_formRequest;
179 	QByteArray m_formRequestBody;
180 	QQueue<Transfer*> m_transfers;
181 	QHash<QNetworkReply*, QPointer<SourceViewerWebWidget> > m_viewSourceReplies;
182 	QNetworkAccessManager::Operation m_formRequestOperation;
183 	LoadingState m_loadingState;
184 	int m_amountOfDeferredPlugins;
185 	int m_transfersTimer;
186 	bool m_canLoadPlugins;
187 	bool m_isAudioMuted;
188 	bool m_isFullScreen;
189 	bool m_isTyped;
190 	bool m_isNavigating;
191 
192 signals:
193 	void widgetActivated(WebWidget *widget);
194 
195 friend class QtWebKitFrame;
196 friend class QtWebKitNetworkManager;
197 friend class QtWebKitPluginFactory;
198 friend class QtWebKitWebBackend;
199 friend class QtWebKitPage;
200 };
201 
202 }
203 
204 #endif
205