From effa889ba7b7a10b3648a9ded55a5bcd4bae993a Mon Sep 17 00:00:00 2001 From: David Rosca <nowrep@gmail.com> Date: Thu, 8 Oct 2015 17:28:19 +0200 Subject: [PATCH] Add firstPartyUrl to QWebEngineUrlRequestInfo Add firstPartyUrl that can be used to identify third-party requests. Change-Id: I2b8e48ff0a1a4402af224c80f91d4e599a61a89c Reviewed-by: Leena Miettinen <riitta-leena.miettinen@theqtcompany.com> Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com> --- src/core/api/qwebengineurlrequestinfo.cpp | 14 ++++++++++++- src/core/api/qwebengineurlrequestinfo.h | 1 + src/core/api/qwebengineurlrequestinfo_p.h | 2 ++ src/core/network_delegate_qt.cpp | 1 + .../resources/firstparty.html | 5 +++++ .../tst_qwebengineurlrequestinterceptor.cpp | 20 +++++++++++++++++++ .../tst_qwebengineurlrequestinterceptor.qrc | 1 + 7 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/auto/core/qwebengineurlrequestinterceptor/resources/firstparty.html diff --git a/src/core/api/qwebengineurlrequestinfo.cpp b/src/core/api/qwebengineurlrequestinfo.cpp index b769081f8..e8ce65be3 100644 --- a/src/core/api/qwebengineurlrequestinfo.cpp +++ b/src/core/api/qwebengineurlrequestinfo.cpp @@ -119,11 +119,12 @@ ASSERT_ENUMS_MATCH(QtWebEngineCore::WebContentsAdapterClient::OtherNavigation, Q */ -QWebEngineUrlRequestInfoPrivate::QWebEngineUrlRequestInfoPrivate(QWebEngineUrlRequestInfo::ResourceType resource, QWebEngineUrlRequestInfo::NavigationType navigation, const QUrl &u, const QByteArray &m) +QWebEngineUrlRequestInfoPrivate::QWebEngineUrlRequestInfoPrivate(QWebEngineUrlRequestInfo::ResourceType resource, QWebEngineUrlRequestInfo::NavigationType navigation, const QUrl &u, const QUrl &fpu, const QByteArray &m) : resourceType(resource) , navigationType(navigation) , shouldBlockRequest(false) , url(u) + , firstPartyUrl(fpu) , method(m) { } @@ -218,6 +219,17 @@ QUrl QWebEngineUrlRequestInfo::requestUrl() const return d->url; } +/*! + Returns the first party URL of the request. + The first party URL is the URL of the page that issued the request. +*/ + +QUrl QWebEngineUrlRequestInfo::firstPartyUrl() const +{ + Q_D(const QWebEngineUrlRequestInfo); + return d->firstPartyUrl; +} + /*! Returns the HTTP method of the request (for example, GET or POST). diff --git a/src/core/api/qwebengineurlrequestinfo.h b/src/core/api/qwebengineurlrequestinfo.h index 7c016d20d..e6e225051 100644 --- a/src/core/api/qwebengineurlrequestinfo.h +++ b/src/core/api/qwebengineurlrequestinfo.h @@ -86,6 +86,7 @@ public: NavigationType navigationType() const; QUrl requestUrl() const; + QUrl firstPartyUrl() const; QByteArray requestMethod() const; void block(bool shouldBlock); diff --git a/src/core/api/qwebengineurlrequestinfo_p.h b/src/core/api/qwebengineurlrequestinfo_p.h index b6a304a03..1b1279d27 100644 --- a/src/core/api/qwebengineurlrequestinfo_p.h +++ b/src/core/api/qwebengineurlrequestinfo_p.h @@ -58,6 +58,7 @@ public: QWebEngineUrlRequestInfoPrivate(QWebEngineUrlRequestInfo::ResourceType resource , QWebEngineUrlRequestInfo::NavigationType navigation , const QUrl &u + , const QUrl &fpu , const QByteArray &m); QWebEngineUrlRequestInfo::ResourceType resourceType; @@ -65,6 +66,7 @@ public: bool shouldBlockRequest; QUrl url; + QUrl firstPartyUrl; const QByteArray method; QHash<QByteArray, QByteArray> extraHeaders; diff --git a/src/core/network_delegate_qt.cpp b/src/core/network_delegate_qt.cpp index b8f1b68d0..3f67e7c0d 100644 --- a/src/core/network_delegate_qt.cpp +++ b/src/core/network_delegate_qt.cpp @@ -106,6 +106,7 @@ int NetworkDelegateQt::OnBeforeURLRequest(net::URLRequest *request, const net::C QWebEngineUrlRequestInfoPrivate *infoPrivate = new QWebEngineUrlRequestInfoPrivate(static_cast<QWebEngineUrlRequestInfo::ResourceType>(resourceType) , static_cast<QWebEngineUrlRequestInfo::NavigationType>(navigationType) , qUrl + , toQt(request->first_party_for_cookies()) , QByteArray::fromStdString(request->method())); QWebEngineUrlRequestInfo requestInfo(infoPrivate); if (interceptor->interceptRequest(requestInfo)) { diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/resources/firstparty.html b/tests/auto/core/qwebengineurlrequestinterceptor/resources/firstparty.html new file mode 100644 index 000000000..8bc540c08 --- /dev/null +++ b/tests/auto/core/qwebengineurlrequestinterceptor/resources/firstparty.html @@ -0,0 +1,5 @@ +<html> +<body> +<iframe src="qrc:///resources/content.html"></iframe> +</body> +</html> diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp index a2f9e0c37..4891cafd0 100644 --- a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp +++ b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp @@ -66,6 +66,7 @@ private Q_SLOTS: void ipv6HostEncoding(); void requestedUrl(); void setUrlSameUrl(); + void firstPartyUrl(); }; tst_QWebEngineUrlRequestInterceptor::tst_QWebEngineUrlRequestInterceptor() @@ -96,6 +97,7 @@ class TestRequestInterceptor : public QWebEngineUrlRequestInterceptor { public: QList<QUrl> observedUrls; + QList<QUrl> firstPartyUrls; bool shouldIntercept; bool interceptRequest(QWebEngineUrlRequestInfo &info) override @@ -105,6 +107,7 @@ public: info.redirect(QUrl("qrc:///resources/content.html")); observedUrls.append(info.requestUrl()); + firstPartyUrls.append(info.firstPartyUrl()); return shouldIntercept; } TestRequestInterceptor(bool intercept) @@ -253,5 +256,22 @@ void tst_QWebEngineUrlRequestInterceptor::setUrlSameUrl() QCOMPARE(spy.count(), 4); } +void tst_QWebEngineUrlRequestInterceptor::firstPartyUrl() +{ + QWebEnginePage page; + TestRequestInterceptor interceptor(/* intercept */ false); + page.profile()->setRequestInterceptor(&interceptor); + + QSignalSpy spy(&page, SIGNAL(loadFinished(bool))); + + page.setUrl(QUrl("qrc:///resources/firstparty.html")); + waitForSignal(&page, SIGNAL(loadFinished(bool))); + QCOMPARE(interceptor.observedUrls.at(0), QUrl("qrc:///resources/firstparty.html")); + QCOMPARE(interceptor.observedUrls.at(1), QUrl("qrc:///resources/content.html")); + QCOMPARE(interceptor.firstPartyUrls.at(0), QUrl("qrc:///resources/firstparty.html")); + QCOMPARE(interceptor.firstPartyUrls.at(1), QUrl("qrc:///resources/firstparty.html")); + QCOMPARE(spy.count(), 1); +} + QTEST_MAIN(tst_QWebEngineUrlRequestInterceptor) #include "tst_qwebengineurlrequestinterceptor.moc" diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.qrc b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.qrc index afeae268b..ca045e7fc 100644 --- a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.qrc +++ b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.qrc @@ -2,5 +2,6 @@ <qresource> <file>resources/index.html</file> <file>resources/content.html</file> + <file>resources/firstparty.html</file> </qresource> </RCC> -- GitLab