diff options
author | Andras Becsi <andras.becsi@theqtcompany.com> | 2015-05-13 21:24:48 +0200 |
---|---|---|
committer | Andras Becsi <becsi.andras@gmail.com> | 2015-08-28 08:40:18 +0000 |
commit | 1df4983268673e514d700cc4491310da7dd26a26 (patch) | |
tree | 7c10172bcb47a0f6956787fc35a5fe8f8437f60e /tests/quicktestbrowser | |
parent | ec2b89a03d884c5d767a92eddc264d7dbf702470 (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>
Diffstat (limited to 'tests/quicktestbrowser')
-rw-r--r-- | tests/quicktestbrowser/ApplicationRoot.qml | 2 | ||||
-rw-r--r-- | tests/quicktestbrowser/BrowserWindow.qml | 8 | ||||
-rw-r--r-- | tests/quicktestbrowser/main.cpp | 40 |
3 files changed, 49 insertions, 1 deletions
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(); } |