From e2e61521969932c8b9f1afe13979f21ecf2a5399 Mon Sep 17 00:00:00 2001
From: Michael Bruning <michael.bruning@theqtcompany.com>
Date: Tue, 24 Nov 2015 17:41:17 +0100
Subject: [PATCH] Change QWebEngineUrlRequestInterceptor::interceptRequest to
 void.

Now uses a flag in QWebEngineUrlRequestInfoPrivate to store if
the interceptor actually changed the request.

Change-Id: Idccbd1c15696e577ee69248e53b75ba6ec1c571c
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
---
 src/core/api/qwebengineurlrequestinfo.cpp              | 10 ++++++++++
 src/core/api/qwebengineurlrequestinfo.h                |  1 +
 src/core/api/qwebengineurlrequestinfo_p.h              |  1 +
 src/core/api/qwebengineurlrequestinterceptor.h         |  2 +-
 src/core/network_delegate_qt.cpp                       |  3 ++-
 .../tst_qwebengineurlrequestinterceptor.cpp            |  8 +++-----
 6 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/core/api/qwebengineurlrequestinfo.cpp b/src/core/api/qwebengineurlrequestinfo.cpp
index e8ce65be3..0aab4aa1b 100644
--- a/src/core/api/qwebengineurlrequestinfo.cpp
+++ b/src/core/api/qwebengineurlrequestinfo.cpp
@@ -126,6 +126,7 @@ QWebEngineUrlRequestInfoPrivate::QWebEngineUrlRequestInfoPrivate(QWebEngineUrlRe
     , url(u)
     , firstPartyUrl(fpu)
     , method(m)
+    , changed(false)
 {
 }
 
@@ -241,6 +242,12 @@ QByteArray QWebEngineUrlRequestInfo::requestMethod() const
     return d->method;
 }
 
+bool QWebEngineUrlRequestInfo::changed() const
+{
+    Q_D(const QWebEngineUrlRequestInfo);
+    return d->changed;
+}
+
 /*!
     Redirects this request to \a url.
     It is only possible to redirect requests that do not have payload data, such as GET requests.
@@ -249,6 +256,7 @@ QByteArray QWebEngineUrlRequestInfo::requestMethod() const
 void QWebEngineUrlRequestInfo::redirect(const QUrl &url)
 {
     Q_D(QWebEngineUrlRequestInfo);
+    d->changed = true;
     d->url = url;
 }
 
@@ -261,6 +269,7 @@ void QWebEngineUrlRequestInfo::redirect(const QUrl &url)
 void QWebEngineUrlRequestInfo::block(bool shouldBlock)
 {
     Q_D(QWebEngineUrlRequestInfo);
+    d->changed = true;
     d->shouldBlockRequest = shouldBlock;
 }
 
@@ -271,6 +280,7 @@ void QWebEngineUrlRequestInfo::block(bool shouldBlock)
 void QWebEngineUrlRequestInfo::setExtraHeader(const QByteArray &name, const QByteArray &value)
 {
     Q_D(QWebEngineUrlRequestInfo);
+    d->changed = true;
     d->extraHeaders.insert(name, value);
 }
 
diff --git a/src/core/api/qwebengineurlrequestinfo.h b/src/core/api/qwebengineurlrequestinfo.h
index e6e225051..1443665af 100644
--- a/src/core/api/qwebengineurlrequestinfo.h
+++ b/src/core/api/qwebengineurlrequestinfo.h
@@ -88,6 +88,7 @@ public:
     QUrl requestUrl() const;
     QUrl firstPartyUrl() const;
     QByteArray requestMethod() const;
+    bool changed() const;
 
     void block(bool shouldBlock);
     void redirect(const QUrl &url);
diff --git a/src/core/api/qwebengineurlrequestinfo_p.h b/src/core/api/qwebengineurlrequestinfo_p.h
index 1b1279d27..df5f18d6e 100644
--- a/src/core/api/qwebengineurlrequestinfo_p.h
+++ b/src/core/api/qwebengineurlrequestinfo_p.h
@@ -68,6 +68,7 @@ public:
     QUrl url;
     QUrl firstPartyUrl;
     const QByteArray method;
+    bool changed;
     QHash<QByteArray, QByteArray> extraHeaders;
 
     QWebEngineUrlRequestInfo *q_ptr;
diff --git a/src/core/api/qwebengineurlrequestinterceptor.h b/src/core/api/qwebengineurlrequestinterceptor.h
index 9a632c3e0..372ee9066 100644
--- a/src/core/api/qwebengineurlrequestinterceptor.h
+++ b/src/core/api/qwebengineurlrequestinterceptor.h
@@ -57,7 +57,7 @@ public:
     {
     }
 
-    virtual bool interceptRequest(QWebEngineUrlRequestInfo &info) = 0;
+    virtual void interceptRequest(QWebEngineUrlRequestInfo &info) = 0;
 };
 
 QT_END_NAMESPACE
diff --git a/src/core/network_delegate_qt.cpp b/src/core/network_delegate_qt.cpp
index 3f67e7c0d..6fdabcd4c 100644
--- a/src/core/network_delegate_qt.cpp
+++ b/src/core/network_delegate_qt.cpp
@@ -109,7 +109,8 @@ int NetworkDelegateQt::OnBeforeURLRequest(net::URLRequest *request, const net::C
                                                                                            , toQt(request->first_party_for_cookies())
                                                                                            , QByteArray::fromStdString(request->method()));
         QWebEngineUrlRequestInfo requestInfo(infoPrivate);
-        if (interceptor->interceptRequest(requestInfo)) {
+        interceptor->interceptRequest(requestInfo);
+        if (requestInfo.changed()) {
             int result = infoPrivate->shouldBlockRequest ? net::ERR_ABORTED : net::OK;
 
             if (qUrl != infoPrivate->url)
diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp
index 4a261c46b..c4461f1c1 100644
--- a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp
+++ b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp
@@ -100,15 +100,14 @@ public:
     QList<QUrl> firstPartyUrls;
     bool shouldIntercept;
 
-    bool interceptRequest(QWebEngineUrlRequestInfo &info) override
+    void interceptRequest(QWebEngineUrlRequestInfo &info) override
     {
         info.block(info.requestMethod() != QByteArrayLiteral("GET"));
-        if (info.requestUrl().toString().endsWith(QLatin1String("__placeholder__")))
+        if (shouldIntercept && info.requestUrl().toString().endsWith(QLatin1String("__placeholder__")))
             info.redirect(QUrl("qrc:///resources/content.html"));
 
         observedUrls.append(info.requestUrl());
         firstPartyUrls.append(info.firstPartyUrl());
-        return shouldIntercept;
     }
     TestRequestInterceptor(bool intercept)
         : shouldIntercept(intercept)
@@ -162,11 +161,10 @@ class LocalhostContentProvider : public QWebEngineUrlRequestInterceptor
 public:
     LocalhostContentProvider() { }
 
-    bool interceptRequest(QWebEngineUrlRequestInfo &info) override
+    void interceptRequest(QWebEngineUrlRequestInfo &info) override
     {
         requestedUrls.append(info.requestUrl());
         info.redirect(QUrl("data:text/html,<p>hello"));
-        return true;
     }
 
     QList<QUrl> requestedUrls;
-- 
GitLab