summaryrefslogtreecommitdiffstats
path: root/src/core/api/qwebengineclienthints.cpp
blob: 4786b76394670c8652bd116e7b8cf418c77f39b3 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (C) 2023 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
// Qt-Security score:significant reason:default

#include "qwebengineclienthints.h"

#include "profile_adapter.h"

#include <QJsonObject>
#include <QVariantMap>

QT_BEGIN_NAMESPACE

/*!
    \class QWebEngineClientHints
    \brief The QWebEngineClientHints class provides an object to customize User-Agent Client Hints used by a profile.

    \since 6.8

    \inmodule QtWebEngineCore

    QWebEngineClientHints allows configuration of exposing browser and platform information via
    User-Agent response and request headers, and a JavaScript API.

    The information accessed via this API is split into two groups: low entropy and high entropy hints.
    Low entropy hints (\l{QWebEngineClientHints::platform}{platform} and \l{QWebEngineClientHints::mobile}{mobile})
    are those that do not give away much information; the API makes these accessible with every request and they can not
    be disabled by QWebEngineClientHints::setAllClientHintsEnabled.

    All the others are high entropy hints; they have the potential to give away more information, therefore they can be
    disabled by QWebEngineClientHints::setAllClientHintsEnabled.

    Each profile object has its own QWebEngineClientHints object, which configures the
    Client Hint settings for that browsing context. If a Client Hint is not configured for a web engine
    profile, its default value is deduced from the system.

    \sa QWebEngineProfile::clientHints(), QQuickWebEngineProfile::clientHints()
*/

/*! \internal
 */
QWebEngineClientHints::QWebEngineClientHints(QtWebEngineCore::ProfileAdapter *profileAdapter)
    : m_profileAdapter(profileAdapter)
{
}

/*! \internal
 */
QWebEngineClientHints::~QWebEngineClientHints()
{
}

/*!
    \property QWebEngineClientHints::arch
    The value of the \c{Sec-CH-UA-Arch} HTTP header and \c{architecture} member of NavigatorUAData in JavaScript.
*/
QString QWebEngineClientHints::arch() const
{
    if (!m_profileAdapter)
        return QString();
    return m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UAArchitecture).toString();
}

/*!
    \property QWebEngineClientHints::platform
    The value of the \c{Sec-CH-UA-Platform} HTTP header and \c{platform} member of NavigatorUAData in JavaScript.

    Can not be disabled.
*/
QString QWebEngineClientHints::platform() const
{
    if (!m_profileAdapter)
        return QString();
    return m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UAPlatform).toString();
}

/*!
    \property QWebEngineClientHints::model
    The value of the \c{Sec-CH-UA-Model} HTTP header and \c{model} member of NavigatorUAData in JavaScript.
*/
QString QWebEngineClientHints::model() const
{
    if (!m_profileAdapter)
        return QString();
    return m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UAModel).toString();
}

/*!
    \property QWebEngineClientHints::mobile
    The value of the \c{Sec-CH-UA-Mobile} HTTP header and \c{mobile} member of NavigatorUAData in JavaScript.

    Can not be disabled.
*/
bool QWebEngineClientHints::isMobile() const
{
    if (!m_profileAdapter)
        return false;
    return m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UAMobile).toBool();
}

/*!
    \property QWebEngineClientHints::fullVersion
    The value of the \c{Sec-CH-UA-Full-Version} HTTP header and \c{uaFullVersion} member of NavigatorUAData in JavaScript.
*/
QString QWebEngineClientHints::fullVersion() const
{
    if (!m_profileAdapter)
        return QString();
    return m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UAFullVersion).toString();
}

/*!
    \property QWebEngineClientHints::platformVersion
    The value of the \c{Sec-CH-UA-Platform-Version} HTTP header and \c{platformVersion} member of NavigatorUAData in JavaScript.
*/
QString QWebEngineClientHints::platformVersion() const
{
    if (!m_profileAdapter)
        return QString();
    return m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UAPlatformVersion).toString();
}

/*!
    \property QWebEngineClientHints::bitness
    The value of the \c{Sec-CH-UA-Bitness} HTTP header and \c{bitness} member of NavigatorUAData in JavaScript.
*/
QString QWebEngineClientHints::bitness() const
{
    if (!m_profileAdapter)
        return QString();
    return m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UABitness).toString();
}

/*!
    \property QWebEngineClientHints::fullVersionList
    The value of the \c{Sec-CH-UA-Full-Version-List} HTTP header and \c{fullVersionList} member of
   NavigatorUAData in JavaScript.

    The value of Sec-CH-UA header will also be generated from this by truncating the version
   numbers.

    It holds brand name and version number pairs in a QVariantMap. The provided values will be
   automatically extended by the currently used version of Chromium and a semi-random brand.
*/
QVariantMap QWebEngineClientHints::fullVersionList() const
{
    QVariantMap ret;
    if (!m_profileAdapter)
        return ret;
    QJsonObject fullVersionList = m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UAFullVersionList).toJsonObject();
    for (const QString &key : fullVersionList.keys())
        ret.insert(key, fullVersionList.value(key).toVariant());
    return ret;
}

/*!
    \property QWebEngineClientHints::wow64
    The value of the \c{Sec-CH-UA-Wow64} HTTP header and \c{wow64} member of NavigatorUAData in JavaScript.
*/
bool QWebEngineClientHints::isWow64() const
{
    if (!m_profileAdapter)
        return false;
    return m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UAWOW64).toBool();
}

/*!
    \property QWebEngineClientHints::formFactors
    The value of the \c{Sec-CH-UA-Form-Factors} HTTP request header.

    It gives a server information about the user agent's form-factors. It is a structured header
    whose value must be a list. The header's values are case-sensitive. For more information refer
    \l{https://wicg.github.io/ua-client-hints/#sec-ch-ua-form-factors}
*/
QStringList QWebEngineClientHints::formFactors() const
{
    if (!m_profileAdapter)
        return QStringList();

    return m_profileAdapter->clientHint(QtWebEngineCore::ProfileAdapter::UAFormFactors).toStringList();
}

void QWebEngineClientHints::setArch(const QString &arch)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UAArchitecture, QVariant(arch));
}

void QWebEngineClientHints::setPlatform(const QString &platform)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UAPlatform, QVariant(platform));
}

void QWebEngineClientHints::setModel(const QString &model)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UAModel, QVariant(model));
}

void QWebEngineClientHints::setIsMobile(bool mobile)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UAMobile, QVariant(mobile));
}

void QWebEngineClientHints::setFullVersion(const QString &fullVersion)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UAFullVersion, QVariant(fullVersion));
}

void QWebEngineClientHints::setPlatformVersion(const QString &platformVersion)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UAPlatformVersion, QVariant(platformVersion));
}

void QWebEngineClientHints::setBitness(const QString &bitness)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UABitness, QVariant(bitness));
}

void QWebEngineClientHints::setFullVersionList(const QVariantMap &fullVersionList)
{
    if (!m_profileAdapter)
        return;
    QJsonObject jsonObject;
    for (auto i = fullVersionList.cbegin(), end = fullVersionList.cend(); i != end; ++i)
        jsonObject.insert(i.key(), QJsonValue(i.value().toString()));
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UAFullVersionList, QVariant(jsonObject));
}

void QWebEngineClientHints::setIsWow64(bool wow64)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UAWOW64, QVariant(wow64));
}

void QWebEngineClientHints::setFormFactors(const QStringList &formFactors)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHint(QtWebEngineCore::ProfileAdapter::UAFormFactors,
                                    QVariant(formFactors));
}

/*!
    \property QWebEngineClientHints::isAllClientHintsEnabled
    This property controls whether the Client Hints HTTP headers are sent by WebEngine or not.

    Enabled by default.
*/
bool QWebEngineClientHints::isAllClientHintsEnabled()
{
    if (!m_profileAdapter)
        return true;
    return m_profileAdapter->clientHintsEnabled();
}

void QWebEngineClientHints::setAllClientHintsEnabled(bool enabled)
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->setClientHintsEnabled(enabled);
}

/*!
    Resets all Client Hints settings to their default values.
*/
void QWebEngineClientHints::resetAll()
{
    if (!m_profileAdapter)
        return;
    m_profileAdapter->resetClientHints();
}

QT_END_NAMESPACE