| 1 | // Copyright (C) 2015 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QWEBVIEW_P_H |
| 5 | #define QWEBVIEW_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include "qabstractwebview_p.h" |
| 19 | #include "qwebviewinterface_p.h" |
| 20 | #include <QtCore/qobject.h> |
| 21 | #include <QtCore/qurl.h> |
| 22 | #include <QtCore/qvariant.h> |
| 23 | #include <QtGui/qimage.h> |
| 24 | |
| 25 | #include <QtCore/qpointer.h> |
| 26 | |
| 27 | class tst_QWebView; |
| 28 | |
| 29 | QT_BEGIN_NAMESPACE |
| 30 | |
| 31 | class QWebViewLoadRequestPrivate; |
| 32 | |
| 33 | class QWindow; |
| 34 | |
| 35 | class Q_WEBVIEW_EXPORT QWebViewSettings : public QAbstractWebViewSettings |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | Q_PROPERTY(bool localStorageEnabled READ localStorageEnabled WRITE setLocalStorageEnabled NOTIFY localStorageEnabledChanged) |
| 39 | Q_PROPERTY(bool javaScriptEnabled READ javaScriptEnabled WRITE setJavaScriptEnabled NOTIFY javaScriptEnabledChanged) |
| 40 | Q_PROPERTY(bool allowFileAccess READ allowFileAccess WRITE setAllowFileAccess NOTIFY allowFileAccessChanged) |
| 41 | Q_PROPERTY(bool localContentCanAccessFileUrls READ localContentCanAccessFileUrls WRITE setLocalContentCanAccessFileUrls NOTIFY localContentCanAccessFileUrlsChanged) |
| 42 | |
| 43 | public: |
| 44 | explicit QWebViewSettings(QAbstractWebViewSettings *webview); |
| 45 | ~QWebViewSettings() override; |
| 46 | |
| 47 | bool localStorageEnabled() const override; |
| 48 | bool javaScriptEnabled() const override; |
| 49 | bool allowFileAccess() const override; |
| 50 | bool localContentCanAccessFileUrls() const override; |
| 51 | |
| 52 | public Q_SLOTS: |
| 53 | void setLocalStorageEnabled(bool enabled) override; |
| 54 | void setJavaScriptEnabled(bool enabled) override; |
| 55 | void setAllowFileAccess(bool enabled) override; |
| 56 | void setLocalContentCanAccessFileUrls(bool enabled) override; |
| 57 | |
| 58 | signals: |
| 59 | void localStorageEnabledChanged(); |
| 60 | void javaScriptEnabledChanged(); |
| 61 | void allowFileAccessChanged(); |
| 62 | void localContentCanAccessFileUrlsChanged(); |
| 63 | void nativeWindowChanged(QWindow *window); |
| 64 | |
| 65 | private: |
| 66 | QPointer<QAbstractWebViewSettings> d; |
| 67 | }; |
| 68 | |
| 69 | class Q_WEBVIEW_EXPORT QWebView |
| 70 | : public QAbstractWebView |
| 71 | { |
| 72 | Q_OBJECT |
| 73 | public: |
| 74 | enum LoadStatus { // Changes here needs to be done in QQuickWebView as well |
| 75 | LoadStartedStatus, |
| 76 | LoadStoppedStatus, |
| 77 | LoadSucceededStatus, |
| 78 | LoadFailedStatus |
| 79 | }; |
| 80 | |
| 81 | explicit QWebView(QObject *p = nullptr); |
| 82 | ~QWebView() override; |
| 83 | |
| 84 | QString httpUserAgent() const override; |
| 85 | void setHttpUserAgent(const QString &httpUserAgent) override; |
| 86 | QUrl url() const; |
| 87 | void setUrl(const QUrl &url) override; |
| 88 | bool canGoBack() const override; |
| 89 | bool canGoForward() const override; |
| 90 | QString title() const override; |
| 91 | int loadProgress() const override; |
| 92 | bool isLoading() const override; |
| 93 | |
| 94 | QWebViewSettings *getSettings() const override; |
| 95 | QWindow *nativeWindow() const override; |
| 96 | |
| 97 | // NOTE: This is a temporary solution for WASM and should |
| 98 | // be removed once window containers are supported. |
| 99 | static QAbstractWebView *get(QWebView &q) { return q.d; } |
| 100 | |
| 101 | public Q_SLOTS: |
| 102 | void goBack() override; |
| 103 | void goForward() override; |
| 104 | void reload() override; |
| 105 | void stop() override; |
| 106 | void loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) override; |
| 107 | void setCookie(const QString &domain, const QString &name, |
| 108 | const QString &value) override; |
| 109 | void deleteCookie(const QString &domain, const QString &name) override; |
| 110 | void deleteAllCookies() override; |
| 111 | |
| 112 | Q_SIGNALS: |
| 113 | void titleChanged(); |
| 114 | void urlChanged(); |
| 115 | void loadingChanged(const QWebViewLoadRequestPrivate &loadRequest); |
| 116 | void loadProgressChanged(); |
| 117 | void javaScriptResult(int id, const QVariant &result); |
| 118 | void httpUserAgentChanged(); |
| 119 | void cookieAdded(const QString &domain, const QString &name); |
| 120 | void cookieRemoved(const QString &domain, const QString &name); |
| 121 | |
| 122 | protected: |
| 123 | void runJavaScriptPrivate(const QString &script, |
| 124 | int callbackId) override; |
| 125 | |
| 126 | private Q_SLOTS: |
| 127 | void onTitleChanged(const QString &title); |
| 128 | void onUrlChanged(const QUrl &url); |
| 129 | void onLoadProgressChanged(int progress); |
| 130 | void onLoadingChanged(const QWebViewLoadRequestPrivate &loadRequest); |
| 131 | void onHttpUserAgentChanged(const QString &httpUserAgent); |
| 132 | |
| 133 | private: |
| 134 | friend class QQuickWebView; |
| 135 | friend class ::tst_QWebView; |
| 136 | |
| 137 | QAbstractWebView *d = nullptr; |
| 138 | QWebViewSettings *m_settings = nullptr; |
| 139 | |
| 140 | // provisional data |
| 141 | int m_progress; |
| 142 | QString m_title; |
| 143 | QUrl m_url; |
| 144 | mutable QString m_httpUserAgent; |
| 145 | }; |
| 146 | |
| 147 | QT_END_NAMESPACE |
| 148 | |
| 149 | #endif // QWEBVIEW_P_H |
| 150 | |