summaryrefslogtreecommitdiffstats
path: root/src/plugins/windows/qwebview2webview_p.h
blob: 89442b37e8cc656ae0928d1b966432594bab60a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QWEBVIEW2WEBVIEW_P_H
#define QWEBVIEW2WEBVIEW_P_H

#include <private/qabstractwebview_p.h>

#include <QMap>
#include <QPointer>
#include <webview2.h>
#include <wrl.h>
#include <wrl/client.h>

QT_BEGIN_NAMESPACE

using namespace Microsoft::WRL;

class QWebview2WebViewSettingsPrivate final : public QAbstractWebViewSettings
{
    Q_OBJECT
public:
    explicit QWebview2WebViewSettingsPrivate(QObject *p = nullptr);

    void init(ICoreWebView2Controller* viewController);

    bool localStorageEnabled() const final;
    bool javaScriptEnabled() const final;
    bool localContentCanAccessFileUrls() const final;
    bool allowFileAccess() const final;

public Q_SLOTS:
    void setLocalContentCanAccessFileUrls(bool enabled) final;
    void setJavaScriptEnabled(bool enabled) final;
    void setLocalStorageEnabled(bool enabled) final;
    void setAllowFileAccess(bool enabled) final;

private:
    ComPtr<ICoreWebView2Controller> m_webviewController;
    ComPtr<ICoreWebView2> m_webview;
    bool m_allowFileAccess = false;
    bool m_localContentCanAccessFileUrls = false;
};

// This is used to store informations before webview2 is initialized
// Because WebView2 initialization is async
struct QWebViewInitData{
    QString m_html;
    struct CookieData{
        QString domain;
        QString name;
        QString value;
    };
    QMap<QString, CookieData > m_cookies;
};

class QWebView2WebViewPrivate : public QAbstractWebView
{
    Q_OBJECT
public:
    explicit QWebView2WebViewPrivate(QObject *parent = nullptr);
    ~QWebView2WebViewPrivate() override;

    QString httpUserAgent() const override;
    void setHttpUserAgent(const QString &userAgent) override;
    void setUrl(const QUrl &url) override;
    bool canGoBack() const override;
    bool canGoForward() const override;
    QString title() const override;
    int loadProgress() const override;
    bool isLoading() const override;

    QWindow* nativeWindow() const override;

public Q_SLOTS:
    void goBack() override;
    void goForward() override;
    void reload() override;
    void stop() override;
    void loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) override;
    void setCookie(const QString &domain, const QString &name, const QString &value) override;
    void deleteCookie(const QString &domain, const QString &name) override;
    void deleteAllCookies() override;

private Q_SLOTS:
    HRESULT onNavigationStarting(ICoreWebView2* webview, ICoreWebView2NavigationStartingEventArgs* args);
    HRESULT onNavigationCompleted(ICoreWebView2* webview, ICoreWebView2NavigationCompletedEventArgs* args);
    HRESULT onWebResourceRequested(ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args);
    HRESULT onContentLoading(ICoreWebView2* webview, ICoreWebView2ContentLoadingEventArgs* args);
    void updateWindowGeometry();

protected:
    void runJavaScriptPrivate(const QString &script, int callbackId) override;
    QAbstractWebViewSettings *getSettings() const override;

private:
    ComPtr<ICoreWebView2Controller> m_webviewController;
    ComPtr<ICoreWebView2> m_webview;
    ComPtr<ICoreWebView2CookieManager> m_cookieManager;
    QWebview2WebViewSettingsPrivate *m_settings;
    QPointer<QWindow> m_window;
    QPointer<QWindow> m_webViewWindow;
    bool m_isLoading;
    QUrl m_url;
    std::unique_ptr<QWebViewInitData> m_initData = nullptr;
};

QT_END_NAMESPACE

#endif // QWEBVIEW2WEBVIEW_P_H