aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/webengineprofile.cpp
blob: 0e6a6bd27d3a8438dff5b40296a6a149358d4e82 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: git://neueland.iserlohn-fortress.net/smolbote.git
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "webengineprofile.h"
#include <QFileInfo>
#include <QSettings>
#include <QWebEngineCookieStore>
#include <QWebEngineSettings>

WebEngineProfile::WebEngineProfile(QObject *parent)
    : QWebEngineProfile(parent)
{
    m_name = tr("Off-the-record");

#ifdef QT_DEBUG
    qDebug("Creating off-the-record profile");
#endif

    // Off-the-record profiles have no persistent path
}

WebEngineProfile::WebEngineProfile(const QString &name, const QString &path, QObject *parent)
    : QWebEngineProfile(name, parent)
{
    m_name = name;

#ifdef QT_DEBUG
    qDebug("Creating profile %s", qUtf8Printable(m_name));
#endif

    setPersistentStoragePath(path + "/storage");
    setCachePath(path + "/cache");
}

WebEngineProfile::~WebEngineProfile()
{
    if(shouldSaveProfile) {
        saveProfile();
    }
}

QString WebEngineProfile::name() const
{
    return m_name;
}

QUrl WebEngineProfile::homepage() const
{
    return m_homepage;
}

void WebEngineProfile::setHomepage(const QUrl &url)
{
    m_homepage = url;
}

QUrl WebEngineProfile::newtab() const
{
    return m_newtab;
}

void WebEngineProfile::setNewtab(const QUrl &url)
{
    m_newtab = url;
}

void WebEngineProfile::saveProfile()
{
    QSettings config(persistentStoragePath() + "/profile.ini", QSettings::IniFormat);

    config.setValue("homepage", homepage().toString());
    config.setValue("newtab", newtab().toString());

    config.beginGroup("http");
    config.setValue("userAgent", httpUserAgent());
    config.setValue("accept-lang", httpAcceptLanguage());
    switch(httpCacheType()) {
    case MemoryHttpCache:
        config.setValue("cacheType", "memory");
        break;
    case DiskHttpCache:
        config.setValue("cacheType", "disk");
        break;
    case NoCache:
        config.setValue("cacheType", "disabled");
        break;
    }
    config.setValue("cacheSize", httpCacheMaximumSize());
    config.endGroup(); // http

    config.beginGroup("policy");
    switch(persistentCookiesPolicy()) {
    case NoPersistentCookies:
        config.setValue("cookies", "disabled");
        break;
    case AllowPersistentCookies:
        config.setValue("cookies", "allow");
        break;
    case ForcePersistentCookies:
        config.setValue("cookies", "force");
        break;
    }
    config.endGroup(); // policy

    QWebEngineSettings *s = settings();
    config.beginGroup("attributes");
    config.setValue("autoLoadImages", s->testAttribute(QWebEngineSettings::AutoLoadImages));
    config.setValue("javascriptEnabled", s->testAttribute(QWebEngineSettings::JavascriptEnabled));
    config.setValue("javascriptCanOpenWindows", s->testAttribute(QWebEngineSettings::JavascriptCanOpenWindows));
    config.setValue("javascriptCanAccessClipboard", s->testAttribute(QWebEngineSettings::JavascriptCanAccessClipboard));
    config.setValue("linksIncludedInFocusChain", s->testAttribute(QWebEngineSettings::LinksIncludedInFocusChain));
    config.setValue("localStorageEnabled", s->testAttribute(QWebEngineSettings::LocalStorageEnabled));
    config.setValue("localContentCanAccessRemoteUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls));
    config.setValue("xssAuditingEnabled", s->testAttribute(QWebEngineSettings::XSSAuditingEnabled));
    config.setValue("spatialNavigationEnabled", s->testAttribute(QWebEngineSettings::SpatialNavigationEnabled));
    config.setValue("localContentCanAccessFileUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls));
    config.setValue("hyperlinkAuditingEnabled", s->testAttribute(QWebEngineSettings::HyperlinkAuditingEnabled));
    config.setValue("scrollAnimatorEnabled", s->testAttribute(QWebEngineSettings::ScrollAnimatorEnabled));
    config.setValue("errorPageEnabled", s->testAttribute(QWebEngineSettings::ErrorPageEnabled));
    config.setValue("pluginsEnabled", s->testAttribute(QWebEngineSettings::PluginsEnabled));
    config.setValue("fullscreenSupportEnabled", s->testAttribute(QWebEngineSettings::FullScreenSupportEnabled));
    config.setValue("screenCaptureEnabled", s->testAttribute(QWebEngineSettings::ScreenCaptureEnabled));
    config.setValue("webglEnabled", s->testAttribute(QWebEngineSettings::WebGLEnabled));
    config.setValue("accelerated2dCanvasEnabled", s->testAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled));
    config.setValue("autoLoadIconsForPage", s->testAttribute(QWebEngineSettings::AutoLoadIconsForPage));
    config.setValue("touchIconsEnabled", s->testAttribute(QWebEngineSettings::TouchIconsEnabled));
#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
    config.setValue("focusOnNavigationEnabled", s->testAttribute(QWebEngineSettings::FocusOnNavigationEnabled));
    config.setValue("printElementBackgrounds", s->testAttribute(QWebEngineSettings::PrintElementBackgrounds));
    config.setValue("allowRunningInsecureContent", s->testAttribute(QWebEngineSettings::AllowRunningInsecureContent));
#endif
    config.endGroup(); // attributes

    config.sync();
}

void WebEngineProfile::setCookieInterceptor(CookieInterceptor *interceptor)
{
    connect(cookieStore(), &QWebEngineCookieStore::cookieAdded, interceptor, &CookieInterceptor::judgeCookie);
    cookieStore()->loadAllCookies();
}

WebEngineProfile *createProfile(const QString &name, const QString &path, QObject *parent)
{
    WebEngineProfile *profile;
    if(name.isEmpty()) {
        profile = new WebEngineProfile(parent);
    } else {
        profile = new WebEngineProfile(name, path, parent);
    }

    // Read profile settings
    const QString profileIniPath = path + "/profile.ini";
    if(QFileInfo::exists(profileIniPath)) {
        qDebug("Reading profile from [%s]", qUtf8Printable(profileIniPath));
        QSettings config(profileIniPath, QSettings::IniFormat);

        profile->setHomepage(config.value("homepage", profile->homepage()).toUrl());
        profile->setNewtab(config.value("newtab", profile->newtab()).toUrl());

        config.beginGroup("http");
        profile->setHttpUserAgent(config.value("userAgent").toString());
        profile->setHttpAcceptLanguage(config.value("accept-lang").toString());
        {
            QString cacheType = config.value("cacheType").toString();
            if(cacheType == "memory") {
                profile->setHttpCacheType(QWebEngineProfile::MemoryHttpCache);
            } else if(cacheType == "disk") {
                profile->setHttpCacheType(QWebEngineProfile::DiskHttpCache);
            } else if(cacheType == "disabled") {
                profile->setHttpCacheType(QWebEngineProfile::NoCache);
            }
        }
        profile->setHttpCacheMaximumSize(config.value("cacheSize").toInt());
        config.endGroup(); // http

        config.beginGroup("policy");
        {
            QString cookies = config.value("cookies").toString();
            if(cookies == "disabled") {
                profile->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
            } else if(cookies == "allow") {
                profile->setPersistentCookiesPolicy(QWebEngineProfile::AllowPersistentCookies);
            } else if(cookies == "force") {
                profile->setPersistentCookiesPolicy(QWebEngineProfile::ForcePersistentCookies);
            }
        }
        config.endGroup(); // policy

        config.beginGroup("attributes");
        QWebEngineSettings *s = profile->settings();
        s->setAttribute(QWebEngineSettings::AutoLoadImages,
            config.value("autoLoadImages", s->testAttribute(QWebEngineSettings::AutoLoadImages)).toBool());
        s->setAttribute(QWebEngineSettings::JavascriptEnabled,
            config.value("javascriptEnabled", s->testAttribute(QWebEngineSettings::JavascriptEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows,
            config.value("javascriptCanOpenWindows", s->testAttribute(QWebEngineSettings::JavascriptCanOpenWindows)).toBool());
        s->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard,
            config.value("javascriptCanAccessClipboard", s->testAttribute(QWebEngineSettings::JavascriptCanAccessClipboard)).toBool());
        s->setAttribute(QWebEngineSettings::LinksIncludedInFocusChain,
            config.value("linksIncludedInFocusChain", s->testAttribute(QWebEngineSettings::LinksIncludedInFocusChain)).toBool());
        s->setAttribute(QWebEngineSettings::LocalStorageEnabled,
            config.value("localStorageEnabled", s->testAttribute(QWebEngineSettings::LocalStorageEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls,
            config.value("localContentCanAccessRemoteUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls)).toBool());
        s->setAttribute(QWebEngineSettings::XSSAuditingEnabled,
            config.value("xssAuditingEnabled", s->testAttribute(QWebEngineSettings::XSSAuditingEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::SpatialNavigationEnabled,
            config.value("spatialNavigationEnabled", s->testAttribute(QWebEngineSettings::SpatialNavigationEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls,
            config.value("localContentCanAccessFileUrls", s->testAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls)).toBool());
        s->setAttribute(QWebEngineSettings::HyperlinkAuditingEnabled,
            config.value("hyperlinkAuditingEnabled", s->testAttribute(QWebEngineSettings::HyperlinkAuditingEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled,
            config.value("scrollAnimatorEnabled", s->testAttribute(QWebEngineSettings::ScrollAnimatorEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::ErrorPageEnabled,
            config.value("errorPageEnabled", s->testAttribute(QWebEngineSettings::ErrorPageEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::PluginsEnabled,
            config.value("pluginsEnabled", s->testAttribute(QWebEngineSettings::PluginsEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::FullScreenSupportEnabled,
            config.value("fullscreenSupportEnabled", s->testAttribute(QWebEngineSettings::FullScreenSupportEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::ScreenCaptureEnabled,
            config.value("screenCaptureEnabled", s->testAttribute(QWebEngineSettings::ScreenCaptureEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::WebGLEnabled,
            config.value("webglEnabled", s->testAttribute(QWebEngineSettings::WebGLEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled,
            config.value("accelerated2dCanvasEnabled", s->testAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::AutoLoadIconsForPage,
            config.value("autoLoadIconsForPage", s->testAttribute(QWebEngineSettings::AutoLoadIconsForPage)).toBool());
        s->setAttribute(QWebEngineSettings::TouchIconsEnabled,
            config.value("touchIconsEnabled", s->testAttribute(QWebEngineSettings::TouchIconsEnabled)).toBool());
#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
        s->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled,
            config.value("focusOnNavigationEnabled", s->testAttribute(QWebEngineSettings::FocusOnNavigationEnabled)).toBool());
        s->setAttribute(QWebEngineSettings::PrintElementBackgrounds,
            config.value("printElementBackgrounds", s->testAttribute(QWebEngineSettings::PrintElementBackgrounds)).toBool());
        s->setAttribute(QWebEngineSettings::AllowRunningInsecureContent,
            config.value("allowRunningInsecureContent", s->testAttribute(QWebEngineSettings::AllowRunningInsecureContent)).toBool());
#endif
        config.endGroup(); // attributes
    }
#ifdef QT_DEBUG
    else {
        qDebug("No config for profile %s: %s", qUtf8Printable(name), qUtf8Printable(profileIniPath));
    }
#endif

    return profile;
}