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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
// Copyright (C) 2016 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
#include "qwebenginewebview_p.h"
#include <QtWebView/private/qwebview_p.h>
#include <QtWebView/private/qwebviewloadrequest_p.h>
#include <QtWebViewQuick/private/qquickwebview_p.h>
#include <QtCore/qmap.h>
#include <QtGui/qguiapplication.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qurl.h>
#include <QtQml/qqml.h>
#include <QtQuick/qquickwindow.h>
#include <QtQuick/qquickview.h>
#include <QtQuick/qquickitem.h>
#include <QtWebEngineQuick/private/qquickwebengineview_p.h>
#include <QtWebEngineCore/qwebengineloadinginfo.h>
#include <QWebEngineCookieStore>
#include <QNetworkCookie>
QT_BEGIN_NAMESPACE
static QByteArray qmlSource()
{
return QByteArrayLiteral("import QtWebEngine 1.1\n"
" WebEngineView {\n"
" anchors.fill: parent"
"}\n");
}
QWebEngineWebViewPrivate::QWebEngineWebViewPrivate(QObject *p)
: QAbstractWebView(p), m_profile(nullptr)
{
m_settings = new QWebEngineWebViewSettingsPrivate(this);
m_webEngineView.m_parent = this;
m_cookieStore.m_webEngineViewPtr = &m_webEngineView;
}
QWebEngineWebViewPrivate::~QWebEngineWebViewPrivate()
{
}
QString QWebEngineWebViewPrivate::httpUserAgent() const
{
return m_httpUserAgent;
}
void QWebEngineWebViewPrivate::setHttpUserAgent(const QString &userAgent)
{
m_httpUserAgent = userAgent;
if (m_profile) {
m_profile->setHttpUserAgent(userAgent);
Q_EMIT httpUserAgentChanged(userAgent);
}
}
void QWebEngineWebViewPrivate::setUrl(const QUrl &url)
{
m_webEngineView->setUrl(url);
}
void QWebEngineWebViewPrivate::loadHtml(const QString &html, const QUrl &baseUrl)
{
m_webEngineView->loadHtml(html, baseUrl);
}
bool QWebEngineWebViewPrivate::canGoBack() const
{
return m_webEngineView->canGoBack();
}
void QWebEngineWebViewPrivate::goBack()
{
m_webEngineView->goBack();
}
bool QWebEngineWebViewPrivate::canGoForward() const
{
return m_webEngineView->canGoForward();
}
void QWebEngineWebViewPrivate::goForward()
{
m_webEngineView->goForward();
}
void QWebEngineWebViewPrivate::reload()
{
m_webEngineView->reload();
}
QString QWebEngineWebViewPrivate::title() const
{
return m_webEngineView->title();
}
void QWebEngineWebViewPrivate::runJavaScriptPrivate(const QString &script,
int callbackId)
{
m_webEngineView->runJavaScript(script, QQuickWebView::takeCallback(callbackId));
}
void QWebEngineWebViewPrivate::setCookie(const QString &domain, const QString &name, const QString &value)
{
QNetworkCookie cookie;
cookie.setDomain(domain);
cookie.setName(QByteArray(name.toUtf8()));
cookie.setValue(QByteArray(value.toUtf8()));
cookie.setPath("/");
m_cookieStore->setCookie(cookie);
}
void QWebEngineWebViewPrivate::deleteCookie(const QString &domain, const QString &name)
{
QNetworkCookie cookie;
cookie.setDomain(domain);
cookie.setName(QByteArray(name.toUtf8()));
cookie.setPath("/");
m_cookieStore->deleteCookie(cookie);
}
void QWebEngineWebViewPrivate::deleteAllCookies()
{
m_cookieStore->deleteAllCookies();
}
QAbstractWebViewSettings *QWebEngineWebViewPrivate::getSettings() const
{
return m_settings;
}
int QWebEngineWebViewPrivate::loadProgress() const
{
return m_webEngineView->loadProgress();
}
bool QWebEngineWebViewPrivate::isLoading() const
{
return m_webEngineView->isLoading();
}
void QWebEngineWebViewPrivate::stop()
{
m_webEngineView->stop();
}
void QWebEngineWebViewPrivate::q_urlChanged()
{
Q_EMIT urlChanged(m_webEngineView->url());
}
void QWebEngineWebViewPrivate::q_loadProgressChanged()
{
Q_EMIT loadProgressChanged(m_webEngineView->loadProgress());
}
void QWebEngineWebViewPrivate::q_titleChanged()
{
Q_EMIT titleChanged(m_webEngineView->title());
}
void QWebEngineWebViewPrivate::q_loadingChanged(const QWebEngineLoadingInfo &loadRequest)
{
QWebViewLoadRequestPrivate lr(loadRequest.url(),
static_cast<QWebView::LoadStatus>(loadRequest.status()), // These "should" match...
loadRequest.errorString());
Q_EMIT loadingChanged(lr);
}
void QWebEngineWebViewPrivate::q_profileChanged()
{
auto profile = m_webEngineView->profile();
if (profile == m_profile)
return;
m_profile = profile;
auto userAgent = m_profile->httpUserAgent();
if (m_httpUserAgent == userAgent)
return;
m_httpUserAgent = userAgent;
QObject::connect(m_profile, &QQuickWebEngineProfile::httpUserAgentChanged, this, &QWebEngineWebViewPrivate::q_httpUserAgentChanged);
Q_EMIT httpUserAgentChanged(userAgent);
}
void QWebEngineWebViewPrivate::q_httpUserAgentChanged()
{
QString httpUserAgent = m_profile->httpUserAgent();
if (m_httpUserAgent == httpUserAgent)
return;
m_httpUserAgent = httpUserAgent;
Q_EMIT httpUserAgentChanged(m_httpUserAgent);
}
void QWebEngineWebViewPrivate::q_cookieAdded(const QNetworkCookie &cookie)
{
Q_EMIT cookieAdded(cookie.domain(), cookie.name());
}
void QWebEngineWebViewPrivate::q_cookieRemoved(const QNetworkCookie &cookie)
{
Q_EMIT cookieRemoved(cookie.domain(), cookie.name());
}
void QWebEngineWebViewPrivate::QQuickWebEngineViewPtr::init() const
{
Q_ASSERT(!m_webEngineView);
QObject *p = qobject_cast<QObject *>(m_parent);
QQuickItem *parentItem = nullptr;
while (p) {
p = p->parent();
parentItem = qobject_cast<QQuickWebView *>(p);
if (parentItem)
break;
}
if (!parentItem) {
qWarning("Could not find QQuickWebView");
return;
}
QQmlEngine *engine = qmlEngine(parentItem);
if (!engine) {
qWarning("Could not initialize qmlEngine");
return;
}
QQmlComponent *component = new QQmlComponent(engine);
component->setData(qmlSource(), QUrl::fromLocalFile(QLatin1String("")));
QQuickWebEngineView *webEngineView = qobject_cast<QQuickWebEngineView *>(component->create());
Q_ASSERT(webEngineView);
QQuickWebEngineProfile *profile = webEngineView->profile();
Q_ASSERT(profile);
QQuickWebEngineSettings *settings = webEngineView->settings();
Q_ASSERT(settings);
m_parent->m_profile = profile;
if (!m_parent->m_settings)
m_parent->m_settings = new QWebEngineWebViewSettingsPrivate(m_parent);
m_parent->m_settings->init(settings);
webEngineView->settings()->setErrorPageEnabled(false);
// When the httpUserAgent is set as a property then it will be set before
// init() is called
if (m_parent->m_httpUserAgent.isEmpty())
m_parent->m_httpUserAgent = profile->httpUserAgent();
else
profile->setHttpUserAgent(m_parent->m_httpUserAgent);
QObject::connect(webEngineView, &QQuickWebEngineView::urlChanged, m_parent, &QWebEngineWebViewPrivate::q_urlChanged);
QObject::connect(webEngineView, &QQuickWebEngineView::loadProgressChanged, m_parent, &QWebEngineWebViewPrivate::q_loadProgressChanged);
QObject::connect(webEngineView, &QQuickWebEngineView::loadingChanged, m_parent, &QWebEngineWebViewPrivate::q_loadingChanged);
QObject::connect(webEngineView, &QQuickWebEngineView::titleChanged, m_parent, &QWebEngineWebViewPrivate::q_titleChanged);
QObject::connect(webEngineView, &QQuickWebEngineView::profileChanged,m_parent, &QWebEngineWebViewPrivate::q_profileChanged);
QObject::connect(profile, &QQuickWebEngineProfile::httpUserAgentChanged, m_parent, &QWebEngineWebViewPrivate::q_httpUserAgentChanged);
webEngineView->setParentItem(parentItem);
m_webEngineView.reset(webEngineView);
if (!m_parent->m_cookieStore.m_cookieStore)
m_parent->m_cookieStore.init();
}
void QWebEngineWebViewPrivate::QWebEngineCookieStorePtr::init() const
{
if (!m_webEngineViewPtr->m_webEngineView)
m_webEngineViewPtr->init();
else {
QWebEngineWebViewPrivate * parent = m_webEngineViewPtr->m_parent;
QWebEngineCookieStore *cookieStore = parent->m_profile->cookieStore();
m_cookieStore = cookieStore;
QObject::connect(cookieStore, &QWebEngineCookieStore::cookieAdded, parent, &QWebEngineWebViewPrivate::q_cookieAdded);
QObject::connect(cookieStore, &QWebEngineCookieStore::cookieRemoved, parent, &QWebEngineWebViewPrivate::q_cookieRemoved);
}
}
QWebEngineWebViewSettingsPrivate::QWebEngineWebViewSettingsPrivate(QWebEngineWebViewPrivate *p)
: QAbstractWebViewSettings(p)
{
}
bool QWebEngineWebViewSettingsPrivate::localStorageEnabled() const
{
return m_settings ? m_settings->localStorageEnabled() : m_localStorageEnabled;
}
bool QWebEngineWebViewSettingsPrivate::javaScriptEnabled() const
{
return m_settings ? m_settings->javascriptEnabled() : m_javaScriptEnabled;
}
bool QWebEngineWebViewSettingsPrivate::localContentCanAccessFileUrls() const
{
return m_settings ? m_settings->localContentCanAccessFileUrls() : m_localContentCanAccessFileUrlsEnabled;
}
bool QWebEngineWebViewSettingsPrivate::allowFileAccess() const
{
return m_allowFileAccessEnabled;
}
void QWebEngineWebViewSettingsPrivate::setLocalContentCanAccessFileUrls(bool enabled)
{
if (m_settings)
m_settings->setLocalContentCanAccessFileUrls(enabled);
m_localContentCanAccessFileUrlsEnabled = enabled;
}
void QWebEngineWebViewSettingsPrivate::setJavaScriptEnabled(bool enabled)
{
if (m_settings)
m_settings->setJavascriptEnabled(enabled);
m_javaScriptEnabled = enabled;
}
void QWebEngineWebViewSettingsPrivate::setLocalStorageEnabled(bool enabled)
{
// This separation is a bit different on the mobile platforms, so for now
// we'll interpret this property to also affect the "off the record" profile setting.
if (auto webview = qobject_cast<QWebEngineWebViewPrivate *>(parent())) {
if (webview->m_profile)
webview->m_profile->setOffTheRecord(enabled);
}
if (m_settings)
m_settings->setLocalStorageEnabled(enabled);
m_localStorageEnabled = enabled;
}
void QWebEngineWebViewSettingsPrivate::setAllowFileAccess(bool enabled)
{
Q_UNUSED(enabled);
}
void QWebEngineWebViewSettingsPrivate::init(QQuickWebEngineSettings *settings)
{
m_settings = settings;
if (m_settings) {
// Sync any values already set.
setLocalContentCanAccessFileUrls(m_localContentCanAccessFileUrlsEnabled);
setJavaScriptEnabled(m_javaScriptEnabled);
setLocalStorageEnabled(m_localStorageEnabled);
}
}
QT_END_NAMESPACE
|