qwebenginepage.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtWebEngine module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QWEBENGINEPAGE_H
  40. #define QWEBENGINEPAGE_H
  41. #include <QtWebEngineWidgets/qtwebenginewidgetsglobal.h>
  42. #include <QtWebEngineWidgets/qwebenginecertificateerror.h>
  43. #include <QtWebEngineCore/qwebenginecallback.h>
  44. #include <QtCore/qobject.h>
  45. #include <QtCore/qurl.h>
  46. #include <QtCore/qvariant.h>
  47. #include <QtGui/qpagelayout.h>
  48. #include <QtNetwork/qnetworkaccessmanager.h>
  49. #include <QtWidgets/qwidget.h>
  50. QT_BEGIN_NAMESPACE
  51. class QMenu;
  52. class QWebChannel;
  53. class QWebEngineContextMenuData;
  54. class QWebEngineFullScreenRequest;
  55. class QWebEngineHistory;
  56. class QWebEnginePage;
  57. class QWebEnginePagePrivate;
  58. class QWebEngineProfile;
  59. class QWebEngineScriptCollection;
  60. class QWebEngineSettings;
  61. class QWEBENGINEWIDGETS_EXPORT QWebEnginePage : public QObject {
  62. Q_OBJECT
  63. Q_PROPERTY(QString selectedText READ selectedText)
  64. Q_PROPERTY(bool hasSelection READ hasSelection)
  65. // Ex-QWebFrame properties
  66. Q_PROPERTY(QUrl requestedUrl READ requestedUrl)
  67. Q_PROPERTY(qreal zoomFactor READ zoomFactor WRITE setZoomFactor)
  68. Q_PROPERTY(QString title READ title)
  69. Q_PROPERTY(QUrl url READ url WRITE setUrl)
  70. Q_PROPERTY(QUrl iconUrl READ iconUrl NOTIFY iconUrlChanged)
  71. Q_PROPERTY(QIcon icon READ icon NOTIFY iconChanged)
  72. Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
  73. Q_PROPERTY(QSizeF contentsSize READ contentsSize NOTIFY contentsSizeChanged)
  74. Q_PROPERTY(QPointF scrollPosition READ scrollPosition NOTIFY scrollPositionChanged)
  75. Q_PROPERTY(bool audioMuted READ isAudioMuted WRITE setAudioMuted NOTIFY audioMutedChanged)
  76. Q_PROPERTY(bool recentlyAudible READ recentlyAudible NOTIFY recentlyAudibleChanged)
  77. public:
  78. enum WebAction {
  79. NoWebAction = - 1,
  80. Back,
  81. Forward,
  82. Stop,
  83. Reload,
  84. Cut,
  85. Copy,
  86. Paste,
  87. Undo,
  88. Redo,
  89. SelectAll,
  90. ReloadAndBypassCache,
  91. PasteAndMatchStyle,
  92. OpenLinkInThisWindow,
  93. OpenLinkInNewWindow,
  94. OpenLinkInNewTab,
  95. CopyLinkToClipboard,
  96. DownloadLinkToDisk,
  97. CopyImageToClipboard,
  98. CopyImageUrlToClipboard,
  99. DownloadImageToDisk,
  100. CopyMediaUrlToClipboard,
  101. ToggleMediaControls,
  102. ToggleMediaLoop,
  103. ToggleMediaPlayPause,
  104. ToggleMediaMute,
  105. DownloadMediaToDisk,
  106. InspectElement,
  107. ExitFullScreen,
  108. RequestClose,
  109. Unselect,
  110. SavePage,
  111. OpenLinkInNewBackgroundTab,
  112. WebActionCount
  113. };
  114. enum FindFlag {
  115. FindBackward = 1,
  116. FindCaseSensitively = 2,
  117. };
  118. Q_DECLARE_FLAGS(FindFlags, FindFlag)
  119. enum WebWindowType {
  120. WebBrowserWindow,
  121. WebBrowserTab,
  122. WebDialog,
  123. WebBrowserBackgroundTab
  124. };
  125. enum PermissionPolicy {
  126. PermissionUnknown,
  127. PermissionGrantedByUser,
  128. PermissionDeniedByUser
  129. };
  130. // must match WebContentsAdapterClient::NavigationType
  131. enum NavigationType {
  132. NavigationTypeLinkClicked,
  133. NavigationTypeTyped,
  134. NavigationTypeFormSubmitted,
  135. NavigationTypeBackForward,
  136. NavigationTypeReload,
  137. NavigationTypeOther
  138. };
  139. enum Feature {
  140. #ifndef Q_QDOC
  141. Notifications = 0,
  142. #endif
  143. Geolocation = 1,
  144. MediaAudioCapture = 2,
  145. MediaVideoCapture,
  146. MediaAudioVideoCapture,
  147. MouseLock
  148. };
  149. // Ex-QWebFrame enum
  150. enum FileSelectionMode {
  151. FileSelectOpen,
  152. FileSelectOpenMultiple,
  153. };
  154. // must match WebContentsAdapterClient::JavaScriptConsoleMessageLevel
  155. enum JavaScriptConsoleMessageLevel {
  156. InfoMessageLevel = 0,
  157. WarningMessageLevel,
  158. ErrorMessageLevel
  159. };
  160. // must match WebContentsAdapterClient::RenderProcessTerminationStatus
  161. enum RenderProcessTerminationStatus {
  162. NormalTerminationStatus = 0,
  163. AbnormalTerminationStatus,
  164. CrashedTerminationStatus,
  165. KilledTerminationStatus
  166. };
  167. explicit QWebEnginePage(QObject *parent = Q_NULLPTR);
  168. QWebEnginePage(QWebEngineProfile *profile, QObject *parent = Q_NULLPTR);
  169. ~QWebEnginePage();
  170. QWebEngineHistory *history() const;
  171. void setView(QWidget *view);
  172. QWidget *view() const;
  173. bool hasSelection() const;
  174. QString selectedText() const;
  175. QWebEngineProfile *profile() const;
  176. #ifndef QT_NO_ACTION
  177. QAction *action(WebAction action) const;
  178. #endif
  179. virtual void triggerAction(WebAction action, bool checked = false);
  180. virtual bool event(QEvent*);
  181. #ifdef Q_QDOC
  182. void findText(const QString &subString, FindFlags options = FindFlags());
  183. void findText(const QString &subString, FindFlags options, FunctorOrLambda resultCallback);
  184. #else
  185. void findText(const QString &subString, FindFlags options = FindFlags(), const QWebEngineCallback<bool> &resultCallback = QWebEngineCallback<bool>());
  186. #endif
  187. QMenu *createStandardContextMenu();
  188. void setFeaturePermission(const QUrl &securityOrigin, Feature feature, PermissionPolicy policy);
  189. // Ex-QWebFrame methods
  190. void load(const QUrl &url);
  191. void setHtml(const QString &html, const QUrl &baseUrl = QUrl());
  192. void setContent(const QByteArray &data, const QString &mimeType = QString(), const QUrl &baseUrl = QUrl());
  193. #ifdef Q_QDOC
  194. void toHtml(FunctorOrLambda resultCallback) const;
  195. void toPlainText(FunctorOrLambda resultCallback) const;
  196. #else
  197. void toHtml(const QWebEngineCallback<const QString &> &resultCallback) const;
  198. void toPlainText(const QWebEngineCallback<const QString &> &resultCallback) const;
  199. #endif
  200. QString title() const;
  201. void setUrl(const QUrl &url);
  202. QUrl url() const;
  203. QUrl requestedUrl() const;
  204. QUrl iconUrl() const;
  205. QIcon icon() const;
  206. qreal zoomFactor() const;
  207. void setZoomFactor(qreal factor);
  208. QPointF scrollPosition() const;
  209. QSizeF contentsSize() const;
  210. void runJavaScript(const QString& scriptSource);
  211. void runJavaScript(const QString& scriptSource, quint32 worldId);
  212. #ifdef Q_QDOC
  213. void runJavaScript(const QString& scriptSource, FunctorOrLambda resultCallback);
  214. void runJavaScript(const QString& scriptSource, quint32 worldId, FunctorOrLambda resultCallback);
  215. #else
  216. void runJavaScript(const QString& scriptSource, const QWebEngineCallback<const QVariant &> &resultCallback);
  217. void runJavaScript(const QString& scriptSource, quint32 worldId, const QWebEngineCallback<const QVariant &> &resultCallback);
  218. #endif
  219. QWebEngineScriptCollection &scripts();
  220. QWebEngineSettings *settings() const;
  221. QWebChannel *webChannel() const;
  222. void setWebChannel(QWebChannel *);
  223. void setWebChannel(QWebChannel *, uint worldId);
  224. QColor backgroundColor() const;
  225. void setBackgroundColor(const QColor &color);
  226. bool isAudioMuted() const;
  227. void setAudioMuted(bool muted);
  228. bool recentlyAudible() const;
  229. void printToPdf(const QString &filePath, const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF()));
  230. #ifdef Q_QDOC
  231. void printToPdf(FunctorOrLambda resultCallback, const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF()));
  232. #else
  233. void printToPdf(const QWebEngineCallback<const QByteArray&> &resultCallback, const QPageLayout &layout = QPageLayout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMarginsF()));
  234. #endif
  235. const QWebEngineContextMenuData &contextMenuData() const;
  236. Q_SIGNALS:
  237. void loadStarted();
  238. void loadProgress(int progress);
  239. void loadFinished(bool ok);
  240. void linkHovered(const QString &url);
  241. void selectionChanged();
  242. void geometryChangeRequested(const QRect& geom);
  243. void windowCloseRequested();
  244. void featurePermissionRequested(const QUrl &securityOrigin, QWebEnginePage::Feature feature);
  245. void featurePermissionRequestCanceled(const QUrl &securityOrigin, QWebEnginePage::Feature feature);
  246. void fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest);
  247. void authenticationRequired(const QUrl &requestUrl, QAuthenticator *authenticator);
  248. void proxyAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *authenticator, const QString &proxyHost);
  249. void renderProcessTerminated(RenderProcessTerminationStatus terminationStatus, int exitCode);
  250. // Ex-QWebFrame signals
  251. void titleChanged(const QString &title);
  252. void urlChanged(const QUrl &url);
  253. void iconUrlChanged(const QUrl &url);
  254. void iconChanged(const QIcon &icon);
  255. void scrollPositionChanged(const QPointF &position);
  256. void contentsSizeChanged(const QSizeF &size);
  257. void audioMutedChanged(bool muted);
  258. void recentlyAudibleChanged(bool recentlyAudible);
  259. protected:
  260. virtual QWebEnginePage *createWindow(WebWindowType type);
  261. virtual QStringList chooseFiles(FileSelectionMode mode, const QStringList &oldFiles, const QStringList &acceptedMimeTypes);
  262. virtual void javaScriptAlert(const QUrl &securityOrigin, const QString& msg);
  263. virtual bool javaScriptConfirm(const QUrl &securityOrigin, const QString& msg);
  264. virtual bool javaScriptPrompt(const QUrl &securityOrigin, const QString& msg, const QString& defaultValue, QString* result);
  265. virtual void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString& message, int lineNumber, const QString& sourceID);
  266. virtual bool certificateError(const QWebEngineCertificateError &certificateError);
  267. virtual bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame);
  268. private:
  269. Q_DISABLE_COPY(QWebEnginePage)
  270. Q_DECLARE_PRIVATE(QWebEnginePage)
  271. QScopedPointer<QWebEnginePagePrivate> d_ptr;
  272. #ifndef QT_NO_ACTION
  273. Q_PRIVATE_SLOT(d_func(), void _q_webActionTriggered(bool checked))
  274. #endif
  275. friend class QWebEngineFullScreenRequest;
  276. friend class QWebEngineView;
  277. friend class QWebEngineViewPrivate;
  278. #ifndef QT_NO_ACCESSIBILITY
  279. friend class QWebEngineViewAccessible;
  280. #endif // QT_NO_ACCESSIBILITY
  281. };
  282. QT_END_NAMESPACE
  283. #endif // QWEBENGINEPAGE_H