summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java43
-rw-r--r--src/plugins/android/qandroidwebview.cpp27
-rw-r--r--src/plugins/android/qandroidwebview_p.h3
-rw-r--r--src/plugins/darwin/qdarwinwebview.mm68
-rw-r--r--src/plugins/darwin/qdarwinwebview_p.h3
-rw-r--r--src/plugins/webengine/qwebenginewebview.cpp58
-rw-r--r--src/plugins/webengine/qwebenginewebview_p.h22
-rw-r--r--src/quick/qquickwebview.cpp54
-rw-r--r--src/quick/qquickwebview_p.h5
-rw-r--r--src/webview/qabstractwebview_p.h2
-rw-r--r--src/webview/qwebview.cpp17
-rw-r--r--src/webview/qwebview_p.h6
-rw-r--r--src/webview/qwebviewfactory.cpp5
-rw-r--r--src/webview/qwebviewinterface_p.h3
-rw-r--r--tests/auto/qml/qquickwebview/CMakeLists.txt1
-rw-r--r--tests/auto/qml/qquickwebview/html/cookies.html9
-rw-r--r--tests/auto/qml/qquickwebview/tst_qquickwebview.cpp45
-rw-r--r--tests/auto/webview/qwebview/tst_qwebview.cpp36
18 files changed, 405 insertions, 2 deletions
diff --git a/src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java b/src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java
index 5994d65..65ab0c4 100644
--- a/src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java
+++ b/src/jar/src/org/qtproject/qt/android/view/QtAndroidWebViewController.java
@@ -44,6 +44,7 @@ import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
+import android.webkit.CookieManager;
import java.lang.Runnable;
import android.app.Activity;
import android.content.Intent;
@@ -90,6 +91,8 @@ public class QtAndroidWebViewController
private native void c_onReceivedTitle(long id, String title);
private native void c_onRunJavaScriptResult(long id, long callbackId, String result);
private native void c_onReceivedError(long id, int errorCode, String description, String url);
+ private native void c_onCookieAdded(long id, boolean result, String domain, String name);
+ private native void c_onCookiesRemoved(long id, boolean result);
// We need to block the UI thread in some cases, if it takes to long we should timeout before
// ANR kicks in... Usually the hard limit is set to 10s and if exceed that then we're in trouble.
@@ -516,4 +519,44 @@ public class QtAndroidWebViewController
}
});
}
+
+ public void setCookie(final String url, final String cookieString)
+ {
+ CookieManager cookieManager = CookieManager.getInstance();
+ cookieManager.setAcceptCookie(true);
+
+ try {
+ cookieManager.setCookie(url, cookieString, new ValueCallback<Boolean>() {
+ @Override
+ public void onReceiveValue(Boolean value) {
+ try {
+ c_onCookieAdded(m_id, value, url, cookieString.split("=")[0]);
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void removeCookies() {
+ try {
+ CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
+ @Override
+ public void onReceiveValue(Boolean value) {
+ try {
+ c_onCookiesRemoved(m_id, value);
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
}
diff --git a/src/plugins/android/qandroidwebview.cpp b/src/plugins/android/qandroidwebview.cpp
index 9c7d6f4..cd4f9d8 100644
--- a/src/plugins/android/qandroidwebview.cpp
+++ b/src/plugins/android/qandroidwebview.cpp
@@ -209,6 +209,33 @@ void QAndroidWebViewPrivate::runJavaScriptPrivate(const QString &script,
callbackId);
}
+void QAndroidWebViewPrivate::setCookie(const QString &domain, const QString &name, const QString &value)
+{
+ QNativeInterface::QAndroidApplication::runOnAndroidMainThread([=]() {
+ m_viewController.callMethod<void>("setCookie",
+ "(Ljava/lang/String;Ljava/lang/String;)V",
+ static_cast<jstring>(QJniObject::fromString(domain).object()),
+ static_cast<jstring>(QJniObject::fromString(name + "=" + value).object()));
+ });
+}
+
+void QAndroidWebViewPrivate::deleteCookie(const QString &domain, const QString &name)
+{
+ QNativeInterface::QAndroidApplication::runOnAndroidMainThread([=]() {
+ m_viewController.callMethod<void>("setCookie",
+ "(Ljava/lang/String;Ljava/lang/String;)V",
+ static_cast<jstring>(QJniObject::fromString(domain).object()),
+ static_cast<jstring>(QJniObject::fromString(name + "=" + "").object()));
+ });
+}
+
+void QAndroidWebViewPrivate::deleteAllCookies()
+{
+ QNativeInterface::QAndroidApplication::runOnAndroidMainThread([=]() {
+ m_viewController.callMethod<void>("removeCookies");
+ });
+}
+
void QAndroidWebViewPrivate::setVisible(bool visible)
{
m_window->setVisible(visible);
diff --git a/src/plugins/android/qandroidwebview_p.h b/src/plugins/android/qandroidwebview_p.h
index ebffef7..4a9fcfb 100644
--- a/src/plugins/android/qandroidwebview_p.h
+++ b/src/plugins/android/qandroidwebview_p.h
@@ -86,6 +86,9 @@ public Q_SLOTS:
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;
protected:
void runJavaScriptPrivate(const QString& script,
diff --git a/src/plugins/darwin/qdarwinwebview.mm b/src/plugins/darwin/qdarwinwebview.mm
index a9c711c..6a97081 100644
--- a/src/plugins/darwin/qdarwinwebview.mm
+++ b/src/plugins/darwin/qdarwinwebview.mm
@@ -467,6 +467,74 @@ void QDarwinWebViewPrivate::runJavaScriptPrivate(const QString &script, int call
}];
}
+void QDarwinWebViewPrivate::setCookie(const QString &domain, const QString &name, const QString &value)
+{
+ NSString *cookieDomain = domain.toNSString();
+ NSString *cookieName = name.toNSString();
+ NSString *cookieValue = value.toNSString();
+
+ WKHTTPCookieStore *cookieStore = wkWebView.configuration.websiteDataStore.httpCookieStore;
+
+ if (cookieStore == nullptr) {
+ return;
+ }
+
+ NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
+ [cookieProperties setObject:cookieName forKey:NSHTTPCookieName];
+ [cookieProperties setObject:cookieValue forKey:NSHTTPCookieValue];
+ [cookieProperties setObject:cookieDomain forKey:NSHTTPCookieDomain];
+ [cookieProperties setObject:cookieDomain forKey:NSHTTPCookieOriginURL];
+ [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
+ [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
+
+ NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
+
+ if (cookie == nullptr) {
+ return;
+ }
+
+ [cookieStore setCookie:cookie completionHandler:^{
+ Q_EMIT cookieAdded(QString::fromNSString(cookie.domain), QString::fromNSString(cookie.name));
+ }];
+}
+
+void QDarwinWebViewPrivate::deleteCookie(const QString &domain, const QString &name)
+{
+ NSString *cookieDomain = domain.toNSString();
+ NSString *cookieName = name.toNSString();
+
+ WKHTTPCookieStore *cookieStore = wkWebView.configuration.websiteDataStore.httpCookieStore;
+
+ if (cookieStore == nullptr) {
+ return;
+ }
+
+ [cookieStore getAllCookies:^(NSArray *cookies) {
+ NSHTTPCookie *cookie;
+ for (cookie in cookies) {
+ if (cookie.domain == cookieDomain && cookie.name == cookieName) {
+ [cookieStore deleteCookie:cookie completionHandler:^{
+ Q_EMIT cookieRemoved(QString::fromNSString(cookie.domain), QString::fromNSString(cookie.name));
+ }];
+ }
+ }
+ }];
+}
+
+void QDarwinWebViewPrivate::deleteAllCookies()
+{
+ WKHTTPCookieStore *cookieStore = wkWebView.configuration.websiteDataStore.httpCookieStore;
+
+ [cookieStore getAllCookies:^(NSArray *cookies) {
+ NSHTTPCookie *cookie;
+ for (cookie in cookies) {
+ [cookieStore deleteCookie:cookie completionHandler:^{
+ Q_EMIT cookieRemoved(QString::fromNSString(cookie.domain), QString::fromNSString(cookie.name));
+ }];
+ }
+ }];
+}
+
QString QDarwinWebViewPrivate::httpUserAgent() const
{
return QString::fromNSString(wkWebView.customUserAgent);
diff --git a/src/plugins/darwin/qdarwinwebview_p.h b/src/plugins/darwin/qdarwinwebview_p.h
index 603bf6a..87b5923 100644
--- a/src/plugins/darwin/qdarwinwebview_p.h
+++ b/src/plugins/darwin/qdarwinwebview_p.h
@@ -105,6 +105,9 @@ public Q_SLOTS:
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;
protected:
void runJavaScriptPrivate(const QString& script,
diff --git a/src/plugins/webengine/qwebenginewebview.cpp b/src/plugins/webengine/qwebenginewebview.cpp
index be25967..f7ce1ae 100644
--- a/src/plugins/webengine/qwebenginewebview.cpp
+++ b/src/plugins/webengine/qwebenginewebview.cpp
@@ -55,6 +55,9 @@
#include <QtWebEngineQuick/private/qquickwebenginesettings_p.h>
#include <QtWebEngineCore/qwebengineloadinginfo.h>
+#include <QWebEngineCookieStore>
+#include <QNetworkCookie>
+
QT_BEGIN_NAMESPACE
static QByteArray qmlSource()
@@ -68,6 +71,7 @@ QWebEngineWebViewPrivate::QWebEngineWebViewPrivate(QObject *p)
: QAbstractWebView(p), m_profile(nullptr)
{
m_webEngineView.m_parent = this;
+ m_cookieStore.m_webEngineViewPtr = &m_webEngineView;
}
QWebEngineWebViewPrivate::~QWebEngineWebViewPrivate()
@@ -149,6 +153,32 @@ void QWebEngineWebViewPrivate::runJavaScriptPrivate(const QString &script,
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();
+}
+
void QWebEngineWebViewPrivate::setVisible(bool visible)
{
m_webEngineView->setVisible(visible);
@@ -233,6 +263,16 @@ void QWebEngineWebViewPrivate::q_httpUserAgentChanged()
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);
@@ -274,8 +314,26 @@ void QWebEngineWebViewPrivate::QQuickWebEngineViewPtr::init() const
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);
+ }
}
QT_END_NAMESPACE
diff --git a/src/plugins/webengine/qwebenginewebview_p.h b/src/plugins/webengine/qwebenginewebview_p.h
index a114507..0092ef8 100644
--- a/src/plugins/webengine/qwebenginewebview_p.h
+++ b/src/plugins/webengine/qwebenginewebview_p.h
@@ -57,11 +57,11 @@
#include <private/qabstractwebview_p.h>
#include <QtWebEngineQuick/QQuickWebEngineProfile>
-
QT_BEGIN_NAMESPACE
class QQuickWebEngineView;
class QWebEngineLoadingInfo;
+class QNetworkCookie;
class QWebEngineWebViewPrivate : public QAbstractWebView
{
@@ -93,6 +93,10 @@ public Q_SLOTS:
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:
void q_urlChanged();
@@ -101,11 +105,12 @@ private Q_SLOTS:
void q_loadingChanged(const QWebEngineLoadingInfo &loadRequest);
void q_profileChanged();
void q_httpUserAgentChanged();
+ void q_cookieAdded(const QNetworkCookie &cookie);
+ void q_cookieRemoved(const QNetworkCookie &cookie);
protected:
void runJavaScriptPrivate(const QString& script,
int callbackId) override;
-
private:
QQuickWebEngineProfile *m_profile;
QString m_httpUserAgent;
@@ -122,6 +127,19 @@ private:
QWebEngineWebViewPrivate *m_parent;
mutable QScopedPointer<QQuickWebEngineView> m_webEngineView;
} m_webEngineView;
+ struct QWebEngineCookieStorePtr
+ {
+ inline QWebEngineCookieStore *operator->() const
+ {
+ if (!m_cookieStore)
+ init();
+ return m_cookieStore;
+ }
+ void init() const;
+
+ QQuickWebEngineViewPtr *m_webEngineViewPtr = nullptr;
+ mutable QWebEngineCookieStore *m_cookieStore = nullptr;
+ } m_cookieStore;
};
QT_END_NAMESPACE
diff --git a/src/quick/qquickwebview.cpp b/src/quick/qquickwebview.cpp
index dcb2fb2..9188cc3 100644
--- a/src/quick/qquickwebview.cpp
+++ b/src/quick/qquickwebview.cpp
@@ -101,6 +101,8 @@ QQuickWebView::QQuickWebView(QQuickItem *parent)
connect(m_webView, &QWebView::requestFocus, this, &QQuickWebView::onFocusRequest);
connect(m_webView, &QWebView::javaScriptResult, this, &QQuickWebView::onRunJavaScriptResult);
connect(m_webView, &QWebView::httpUserAgentChanged, this, &QQuickWebView::httpUserAgentChanged);
+ connect(m_webView, &QWebView::cookieAdded, this, &QQuickWebView::cookieAdded);
+ connect(m_webView, &QWebView::cookieRemoved, this, &QQuickWebView::cookieRemoved);
}
QQuickWebView::~QQuickWebView() { }
@@ -306,6 +308,58 @@ void QQuickWebView::runJavaScriptPrivate(const QString &script, int callbackId)
m_webView->runJavaScriptPrivate(script, callbackId);
}
+/*!
+ \qmlmethod void QtWebView::WebView::setCookie(string domain, string name, string value)
+ \since QtWebView 6.3
+ Adds a cookie with the specified \a domain, \a name and \a value.
+
+ The \l cookieAdded signal will be emitted when the cookie is added.
+*/
+/*!
+ \qmlsignal QtWebView::WebView::cookieAdded(string domain, string name)
+
+ This signal is emitted when a cookie is added.
+
+ The parameters provide information about the \a domain and the \a name of the added cookie.
+
+ \note When Qt WebEngine module is used as backend, cookieAdded signal will be emitted for any
+ cookie added to the underlying QWebEngineCookieStore, including those added by websites.
+ In other cases cookieAdded signal is only emitted for cookies explicitly added with \l setCookie().
+*/
+void QQuickWebView::setCookie(const QString &domain, const QString &name, const QString &value)
+{
+ m_webView->setCookie(domain, name, value);
+}
+
+/*!
+ \qmlmethod void QtWebView::WebView::deleteCookie(string domain, string name)
+ \since QtWebView 6.3
+ Deletes a cookie with the specified \a domain and \a name.
+
+ The \l cookieRemoved signal will be emitted when the cookie is deleted.
+*/
+/*!
+ \qmlsignal QtWebView::WebView::cookieRemoved(string domain, string name)
+
+ This signal is emitted when a cookie is deleted.
+
+ The parameters provide information about the \a domain and the \a name of the deleted cookie.
+*/
+void QQuickWebView::deleteCookie(const QString &domain, const QString &name)
+{
+ m_webView->deleteCookie(domain, name);
+}
+
+/*!
+ \qmlmethod void QtWebView::WebView::deleteAllCookies()
+ \since QtWebView 6.3
+ Deletes all the cookies.
+*/
+void QQuickWebView::deleteAllCookies()
+{
+ m_webView->deleteAllCookies();
+}
+
void QQuickWebView::itemChange(ItemChange change, const ItemChangeData &value)
{
if (change == QQuickItem::ItemActiveFocusHasChanged) {
diff --git a/src/quick/qquickwebview_p.h b/src/quick/qquickwebview_p.h
index 2775fa7..19abdc2 100644
--- a/src/quick/qquickwebview_p.h
+++ b/src/quick/qquickwebview_p.h
@@ -106,6 +106,9 @@ public Q_SLOTS:
Q_REVISION(1, 1) void loadHtml(const QString &html, const QUrl &baseUrl = QUrl()) override;
Q_REVISION(1, 1)
void runJavaScript(const QString &script, const QJSValue &callback = QJSValue());
+ Q_REVISION(6, 3) void setCookie(const QString &domain, const QString &name, const QString &value) override;
+ Q_REVISION(6, 3) void deleteCookie(const QString &domain, const QString &name) override;
+ Q_REVISION(6, 3) void deleteAllCookies() override;
Q_SIGNALS:
void titleChanged();
@@ -113,6 +116,8 @@ Q_SIGNALS:
Q_REVISION(1, 1) void loadingChanged(QQuickWebViewLoadRequest *loadRequest);
void loadProgressChanged();
Q_REVISION(1, 14) void httpUserAgentChanged();
+ Q_REVISION(6, 3) void cookieAdded(const QString &domain, const QString &name);
+ Q_REVISION(6, 3) void cookieRemoved(const QString &domain, const QString &name);
protected:
void itemChange(ItemChange change, const ItemChangeData &value) override;
diff --git a/src/webview/qabstractwebview_p.h b/src/webview/qabstractwebview_p.h
index 9f57b5a..5f612e3 100644
--- a/src/webview/qabstractwebview_p.h
+++ b/src/webview/qabstractwebview_p.h
@@ -71,6 +71,8 @@ Q_SIGNALS:
void javaScriptResult(int id, const QVariant &result);
void requestFocus(bool focus);
void httpUserAgentChanged(const QString &httpUserAgent);
+ void cookieAdded(const QString &domain, const QString &name);
+ void cookieRemoved(const QString &domain, const QString &name);
protected:
explicit QAbstractWebView(QObject *p = nullptr) : QObject(p) { }
diff --git a/src/webview/qwebview.cpp b/src/webview/qwebview.cpp
index 943218b..85e8522 100644
--- a/src/webview/qwebview.cpp
+++ b/src/webview/qwebview.cpp
@@ -58,6 +58,8 @@ QWebView::QWebView(QObject *p)
connect(d, &QAbstractWebView::requestFocus, this, &QWebView::requestFocus);
connect(d, &QAbstractWebView::javaScriptResult,
this, &QWebView::javaScriptResult);
+ connect(d, &QAbstractWebView::cookieAdded, this, &QWebView::cookieAdded);
+ connect(d, &QAbstractWebView::cookieRemoved, this, &QWebView::cookieRemoved);
}
QWebView::~QWebView()
@@ -173,6 +175,21 @@ void QWebView::runJavaScriptPrivate(const QString &script,
d->runJavaScriptPrivate(script, callbackId);
}
+void QWebView::setCookie(const QString &domain, const QString &name, const QString &value)
+{
+ d->setCookie(domain, name, value);
+}
+
+void QWebView::deleteCookie(const QString &domain, const QString &name)
+{
+ d->deleteCookie(domain, name);
+}
+
+void QWebView::deleteAllCookies()
+{
+ d->deleteAllCookies();
+}
+
void QWebView::onTitleChanged(const QString &title)
{
if (m_title == title)
diff --git a/src/webview/qwebview_p.h b/src/webview/qwebview_p.h
index 46754f8..1472c90 100644
--- a/src/webview/qwebview_p.h
+++ b/src/webview/qwebview_p.h
@@ -101,6 +101,10 @@ public Q_SLOTS:
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;
Q_SIGNALS:
void titleChanged();
@@ -110,6 +114,8 @@ Q_SIGNALS:
void javaScriptResult(int id, const QVariant &result);
void requestFocus(bool focus);
void httpUserAgentChanged();
+ void cookieAdded(const QString &domain, const QString &name);
+ void cookieRemoved(const QString &domain, const QString &name);
protected:
void init() override;
diff --git a/src/webview/qwebviewfactory.cpp b/src/webview/qwebviewfactory.cpp
index fdf57bf..3ac3a60 100644
--- a/src/webview/qwebviewfactory.cpp
+++ b/src/webview/qwebviewfactory.cpp
@@ -81,6 +81,11 @@ public:
{ Q_UNUSED(html); Q_UNUSED(baseUrl); }
void runJavaScriptPrivate(const QString &script, int callbackId) override
{ Q_UNUSED(script); Q_UNUSED(callbackId); }
+ void setCookie(const QString &domain, const QString &name, const QString &value) override
+ { Q_UNUSED(domain); Q_UNUSED(name); Q_UNUSED(value); }
+ void deleteCookie(const QString &domain, const QString &name) override
+ { Q_UNUSED(domain); Q_UNUSED(name); }
+ void deleteAllCookies() override {}
};
QAbstractWebView *QWebViewFactory::createWebView()
diff --git a/src/webview/qwebviewinterface_p.h b/src/webview/qwebviewinterface_p.h
index d9e566c..35c5f97 100644
--- a/src/webview/qwebviewinterface_p.h
+++ b/src/webview/qwebviewinterface_p.h
@@ -82,6 +82,9 @@ public:
virtual void runJavaScriptPrivate(const QString &script,
int callbackId) = 0;
+ virtual void setCookie(const QString &domain, const QString &name, const QString &value) = 0;
+ virtual void deleteCookie(const QString &domain, const QString &name) = 0;
+ virtual void deleteAllCookies() = 0;
};
QT_END_NAMESPACE
diff --git a/tests/auto/qml/qquickwebview/CMakeLists.txt b/tests/auto/qml/qquickwebview/CMakeLists.txt
index ca80585..dee784e 100644
--- a/tests/auto/qml/qquickwebview/CMakeLists.txt
+++ b/tests/auto/qml/qquickwebview/CMakeLists.txt
@@ -18,6 +18,7 @@ set(testdata_resource_files
"html/direct-image-compositing.html"
"html/inputmethod.html"
"html/scroll.html"
+ "html/cookies.html"
)
qt_internal_add_resource(tst_qquickwebview "testdata"
diff --git a/tests/auto/qml/qquickwebview/html/cookies.html b/tests/auto/qml/qquickwebview/html/cookies.html
new file mode 100644
index 0000000..a61783b
--- /dev/null
+++ b/tests/auto/qml/qquickwebview/html/cookies.html
@@ -0,0 +1,9 @@
+<html>
+ <head>
+ <script>
+ document.cookie = "TestCookie1=testValue1";
+ document.cookie = "TestCookie2=testValue2";
+</script>
+ </head>
+ </body>
+</html>
diff --git a/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp b/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp
index a9366c4..fa41f99 100644
--- a/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp
+++ b/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp
@@ -69,6 +69,7 @@ private Q_SLOTS:
void multipleWebViews();
void titleUpdate();
void changeUserAgent();
+ void setAndDeleteCookies();
private:
inline QQuickWebView *newWebView();
@@ -353,5 +354,49 @@ void tst_QQuickWebView::changeUserAgent()
QCOMPARE(webView->httpUserAgent(), "dummy");
}
+void tst_QQuickWebView::setAndDeleteCookies()
+{
+ QSignalSpy cookieAddedSpy(webView(), SIGNAL(cookieAdded(const QString &, const QString &)));
+ QSignalSpy cookieRemovedSpy(webView(), SIGNAL(cookieRemoved(const QString &, const QString &)));
+
+#ifdef QT_WEBVIEW_WEBENGINE_BACKEND
+ webView()->setUrl(QUrl("qrc:///cookies.html"));
+ QVERIFY(waitForLoadSucceeded(webView()));
+
+ QTRY_COMPARE(cookieAddedSpy.count(), 2);
+
+ webView()->deleteAllCookies();
+ QTRY_COMPARE(cookieRemovedSpy.count(), 2);
+
+ cookieAddedSpy.clear();
+ cookieRemovedSpy.clear();
+#endif
+
+ webView()->setCookie(".example.com", "TestCookie", "testValue");
+ webView()->setCookie(".example2.com", "TestCookie2", "testValue2");
+ webView()->setCookie(".example3.com", "TestCookie3", "testValue3");
+ QTRY_COMPARE(cookieAddedSpy.count(), 3);
+ QList<QVariant> arguments = cookieAddedSpy.first();
+ QCOMPARE(arguments.at(0), ".example.com");
+ QCOMPARE(arguments.at(1), "TestCookie");
+
+ webView()->deleteCookie(".example.com", "TestCookie");
+ QTRY_COMPARE(cookieRemovedSpy.count(), 1);
+ arguments = cookieRemovedSpy.first();
+ QCOMPARE(arguments.at(0), ".example.com");
+ QCOMPARE(arguments.at(1), "TestCookie");
+
+ // deleting a cookie using a name that has not been set
+ webView()->deleteCookie(".example.com", "NewCookieName");
+ QTRY_COMPARE(cookieRemovedSpy.count(), 1);
+
+ // deleting a cookie using a domain that has not been set
+ webView()->deleteCookie(".new.domain.com", "TestCookie2");
+ QTRY_COMPARE(cookieRemovedSpy.count(), 1);
+
+ webView()->deleteAllCookies();
+ QTRY_COMPARE(cookieRemovedSpy.count(), 3);
+}
+
QTEST_MAIN(tst_QQuickWebView)
#include "tst_qquickwebview.moc"
diff --git a/tests/auto/webview/qwebview/tst_qwebview.cpp b/tests/auto/webview/qwebview/tst_qwebview.cpp
index 14f2dfa..23ef5ca 100644
--- a/tests/auto/webview/qwebview/tst_qwebview.cpp
+++ b/tests/auto/webview/qwebview/tst_qwebview.cpp
@@ -72,6 +72,7 @@ private slots:
void runJavaScript();
void loadHtml();
void loadRequest();
+ void setAndDeleteCookie();
private:
const QString m_cacheLocation;
@@ -237,6 +238,41 @@ void tst_QWebView::loadRequest()
}
}
+void tst_QWebView::setAndDeleteCookie()
+{
+#ifdef QT_WEBVIEW_WEBENGINE_BACKEND
+ QQmlEngine engine;
+ QQmlContext * rootContext = engine.rootContext();
+ QQuickWebView qview;
+ QQmlEngine::setContextForObject(&qview, rootContext);
+ QWebView & view = qview.webView();
+#else
+ QWebView view;
+#endif
+
+ QSignalSpy cookieAddedSpy(&view, SIGNAL(cookieAdded(const QString &, const QString &)));
+ QSignalSpy cookieRemovedSpy(&view, SIGNAL(cookieRemoved(const QString &, const QString &)));
+
+ view.setCookie(".example.com", "TestCookie", "testValue");
+ view.setCookie(".example2.com", "TestCookie2", "testValue2");
+ view.setCookie(".example3.com", "TestCookie3", "testValue3");
+ QTRY_COMPARE(cookieAddedSpy.count(), 3);
+
+ view.deleteCookie(".example.com", "TestCookie");
+ QTRY_COMPARE(cookieRemovedSpy.count(), 1);
+
+ // deleting a cookie using a name that has not been set
+ view.deleteCookie(".example.com", "NewCookieName");
+ QTRY_COMPARE(cookieRemovedSpy.count(), 1);
+
+ // deleting a cookie using a domain that has not been set
+ view.deleteCookie(".new.domain.com", "TestCookie2");
+ QTRY_COMPARE(cookieRemovedSpy.count(), 1);
+
+ view.deleteAllCookies();
+ QTRY_COMPARE(cookieRemovedSpy.count(), 3);
+}
+
QTEST_MAIN(tst_QWebView)
#include "tst_qwebview.moc"