summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndras Becsi <andras.becsi@theqtcompany.com>2015-05-13 21:24:48 +0200
committerAndras Becsi <becsi.andras@gmail.com>2015-08-28 08:40:18 +0000
commit1df4983268673e514d700cc4491310da7dd26a26 (patch)
tree7c10172bcb47a0f6956787fc35a5fe8f8437f60e
parentec2b89a03d884c5d767a92eddc264d7dbf702470 (diff)
Add invokable to QQuickWebEngineProfile to set cookie client
This makes it possible to set a cookie client in the C++ part of a QtQuick application to receive notifications about cookies. Add setting for blocking third party cookies to quicktestbrowser. Change-Id: I627eaab067e92a7be5b36ffed68794e54c7be0e8 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
-rw-r--r--src/core/api/qwebenginecookiestoreclient.h2
-rw-r--r--src/webengine/api/qquickwebengineprofile.cpp7
-rw-r--r--src/webengine/api/qquickwebengineprofile_p.h3
-rw-r--r--tests/quicktestbrowser/ApplicationRoot.qml2
-rw-r--r--tests/quicktestbrowser/BrowserWindow.qml8
-rw-r--r--tests/quicktestbrowser/main.cpp40
6 files changed, 61 insertions, 1 deletions
diff --git a/src/core/api/qwebenginecookiestoreclient.h b/src/core/api/qwebenginecookiestoreclient.h
index 3c01f927d..3a6f54ea7 100644
--- a/src/core/api/qwebenginecookiestoreclient.h
+++ b/src/core/api/qwebenginecookiestoreclient.h
@@ -89,4 +89,6 @@ private:
QT_END_NAMESPACE
+Q_DECLARE_METATYPE(QWebEngineCookieStoreClient*)
+
#endif // QWEBENGINECOOKIESTORECLIENT_H
diff --git a/src/webengine/api/qquickwebengineprofile.cpp b/src/webengine/api/qquickwebengineprofile.cpp
index 027ac3b22..0a1504e28 100644
--- a/src/webengine/api/qquickwebengineprofile.cpp
+++ b/src/webengine/api/qquickwebengineprofile.cpp
@@ -40,6 +40,7 @@
#include "qquickwebenginedownloaditem_p_p.h"
#include "qquickwebengineprofile_p_p.h"
#include "qquickwebenginesettings_p.h"
+#include "qwebenginecookiestoreclient.h"
#include <QQmlEngine>
@@ -435,4 +436,10 @@ QQuickWebEngineSettings *QQuickWebEngineProfile::settings() const
return d->settings();
}
+void QQuickWebEngineProfile::setCookieStoreClient(QWebEngineCookieStoreClient* client)
+{
+ Q_D(QQuickWebEngineProfile);
+ d->browserContext()->setCookieStoreClient(client);
+}
+
QT_END_NAMESPACE
diff --git a/src/webengine/api/qquickwebengineprofile_p.h b/src/webengine/api/qquickwebengineprofile_p.h
index 1f68879e3..4daae1aa7 100644
--- a/src/webengine/api/qquickwebengineprofile_p.h
+++ b/src/webengine/api/qquickwebengineprofile_p.h
@@ -63,6 +63,7 @@ QT_BEGIN_NAMESPACE
class QQuickWebEngineDownloadItem;
class QQuickWebEngineProfilePrivate;
class QQuickWebEngineSettings;
+class QWebEngineCookieStoreClient;
class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineProfile : public QObject {
Q_OBJECT
@@ -121,6 +122,8 @@ public:
static QQuickWebEngineProfile *defaultProfile();
+ Q_REVISION(1) Q_INVOKABLE void setCookieStoreClient(QWebEngineCookieStoreClient* client);
+
signals:
void storageNameChanged();
void offTheRecordChanged();
diff --git a/tests/quicktestbrowser/ApplicationRoot.qml b/tests/quicktestbrowser/ApplicationRoot.qml
index 71737694d..5641b89a3 100644
--- a/tests/quicktestbrowser/ApplicationRoot.qml
+++ b/tests/quicktestbrowser/ApplicationRoot.qml
@@ -44,6 +44,8 @@ import QtWebEngine 1.1
QtObject {
id: root
+ property bool thirdPartyCookiesEnabled: true
+
property QtObject testProfile: WebEngineProfile {
storageName: "Test"
}
diff --git a/tests/quicktestbrowser/BrowserWindow.qml b/tests/quicktestbrowser/BrowserWindow.qml
index 9f15d188e..ca0b6499b 100644
--- a/tests/quicktestbrowser/BrowserWindow.qml
+++ b/tests/quicktestbrowser/BrowserWindow.qml
@@ -77,6 +77,7 @@ ApplicationWindow {
property alias javaScriptEnabled: javaScriptEnabled.checked;
property alias errorPageEnabled: errorPageEnabled.checked;
property alias pluginsEnabled: pluginsEnabled.checked;
+ property alias thirdPartyCookiesEnabled: thirdPartyCookiesEnabled.checked;
}
// Make sure the Qt.WindowFullscreenButtonHint is set on OS X.
@@ -252,6 +253,13 @@ ApplicationWindow {
checked: true
}
MenuItem {
+ id: thirdPartyCookiesEnabled
+ text: "Third party cookies enabled"
+ checkable: true
+ checked: true
+ onToggled: applicationRoot.thirdPartyCookiesEnabled = checked
+ }
+ MenuItem {
id: offTheRecordEnabled
text: "Off The Record"
checkable: true
diff --git a/tests/quicktestbrowser/main.cpp b/tests/quicktestbrowser/main.cpp
index 7171baf77..166da4d5b 100644
--- a/tests/quicktestbrowser/main.cpp
+++ b/tests/quicktestbrowser/main.cpp
@@ -50,7 +50,9 @@ typedef QGuiApplication Application;
#endif
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>
+#include <QtQml/QQmlComponent>
#include <QtWebEngine/qtwebengineglobal.h>
+#include <QtWebEngineCore/qwebenginecookiestoreclient.h>
static QUrl startupUrl()
{
@@ -67,6 +69,24 @@ static QUrl startupUrl()
return QUrl(QStringLiteral("http://qt.io/"));
}
+class CookieClient: public QWebEngineCookieStoreClient
+{
+ QMetaProperty m_settingProperty;
+ const QObject *m_object;
+public:
+ CookieClient(const QObject *object)
+ : m_object(object)
+ {
+ const QMetaObject *rootMeta = object->metaObject();
+ int index = rootMeta->indexOfProperty("thirdPartyCookiesEnabled");
+ Q_ASSERT(index != -1);
+ m_settingProperty = rootMeta->property(index);
+ }
+ virtual bool acceptCookieFromUrl(const QByteArray &, const QUrl &) {
+ return m_settingProperty.read(m_object).toBool();
+ }
+};
+
int main(int argc, char **argv)
{
Application app(argc, argv);
@@ -80,7 +100,25 @@ int main(int argc, char **argv)
Utils utils;
appEngine.rootContext()->setContextProperty("utils", &utils);
appEngine.load(QUrl("qrc:/ApplicationRoot.qml"));
- QMetaObject::invokeMethod(appEngine.rootObjects().first(), "load", Q_ARG(QVariant, startupUrl()));
+ QObject *rootObject = appEngine.rootObjects().first();
+
+ QQmlComponent component(&appEngine);
+ component.setData(QByteArrayLiteral("import QtQuick 2.0\n"
+ "import QtWebEngine 1.1\n"
+ "WebEngineProfile {\n"
+ "storageName: \"Test\"\n"
+ "}")
+ , QUrl());
+ QObject *profile = component.create();
+ CookieClient client(rootObject);
+ QMetaObject::invokeMethod(profile, "setCookieStoreClient", Q_ARG(QWebEngineCookieStoreClient*, &client));
+ const QMetaObject *rootMeta = rootObject->metaObject();
+ int index = rootMeta->indexOfProperty("testProfile");
+ Q_ASSERT(index != -1);
+ QMetaProperty profileProperty = rootMeta->property(index);
+ profileProperty.write(rootObject, qVariantFromValue(profile));
+
+ QMetaObject::invokeMethod(rootObject, "load", Q_ARG(QVariant, startupUrl()));
return app.exec();
}