From 38a426f21c0d6e47bdc05e5541b79c48cf967a0c Mon Sep 17 00:00:00 2001 From: Kai Koehne Date: Fri, 25 Sep 2015 13:27:58 +0200 Subject: Do not require to subclass/install QWebEngineCookieStoreClient The class has only setters and getters, except for the virtual acceptCookie method. By replacing this method with a setCookieFilter callback we can avoid the need of users to subclass the client. Change-Id: Id78c01fc103b8d9cc267594527239b598e8975f1 Reviewed-by: Allan Sandfeld Jensen --- src/core/api/qwebenginecallback_p.h | 6 +++ src/core/api/qwebenginecookiestoreclient.cpp | 59 +++++++++++++++++++------- src/core/api/qwebenginecookiestoreclient.h | 16 +++++-- src/core/api/qwebenginecookiestoreclient_p.h | 1 + src/core/browser_context_adapter.cpp | 9 +--- src/core/browser_context_adapter.h | 3 +- src/webengine/api/qquickwebengineprofile.cpp | 10 ++--- src/webengine/api/qquickwebengineprofile_p.h | 2 +- src/webenginewidgets/api/qwebengineprofile.cpp | 13 ------ src/webenginewidgets/api/qwebengineprofile.h | 1 - 10 files changed, 73 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/core/api/qwebenginecallback_p.h b/src/core/api/qwebenginecallback_p.h index 44d9ceb14..f0c25fe9e 100644 --- a/src/core/api/qwebenginecallback_p.h +++ b/src/core/api/qwebenginecallback_p.h @@ -98,6 +98,12 @@ public: FOR_EACH_TYPE(DEFINE_INVOKE_FOR_TYPE) #undef DEFINE_INVOKE_FOR_TYPE + template + void invokeDirectly(const QWebEngineCallback &callback, const A &argument) + { + return callback.d.data()->operator()(std::forward(argument)); + } + private: struct CallbackSharedDataPointerBase { virtual ~CallbackSharedDataPointerBase() { } diff --git a/src/core/api/qwebenginecookiestoreclient.cpp b/src/core/api/qwebenginecookiestoreclient.cpp index 3a003ff3e..167b3f68c 100644 --- a/src/core/api/qwebenginecookiestoreclient.cpp +++ b/src/core/api/qwebenginecookiestoreclient.cpp @@ -175,8 +175,16 @@ void QWebEngineCookieStoreClientPrivate::onCookieChanged(const QNetworkCookie &c bool QWebEngineCookieStoreClientPrivate::canSetCookie(const QUrl &firstPartyUrl, const QByteArray &cookieLine, const QUrl &url) { - Q_Q(QWebEngineCookieStoreClient); - return q->acceptCookie(firstPartyUrl, cookieLine, url); + if (filterCallback) { + QWebEngineCookieStoreClient::FilterRequest request; + request.accepted = true; + request.firstPartyUrl = firstPartyUrl; + request.cookieLine = cookieLine; + request.cookieSource = url; + callbackDirectory.invokeDirectly(filterCallback, request); + return request.accepted; + } + return true; } /*! @@ -185,8 +193,7 @@ bool QWebEngineCookieStoreClientPrivate::canSetCookie(const QUrl &firstPartyUrl, \since 5.6 \brief The QWebEngineCookieStoreClient class provides access to Chromium's cookies. - By subclassing the QWebEngineCookieStoreClient and installing it on the profile, the user can - access HTTP cookies of Chromium. + The class allows to access HTTP cookies of Chromium for a specific profile. It can be used to synchronize cookies of Chromium and the QNetworkAccessManager, as well as to set, delete, and intercept cookies during navigation. Because cookie operations are asynchronous, the user can choose to provide a callback function @@ -195,6 +202,27 @@ bool QWebEngineCookieStoreClientPrivate::canSetCookie(const QUrl &firstPartyUrl, because they might block the IO thread in case of a blocking connection. */ +/*! + \class QWebEngineCookieStoreClient::FilterRequest + \inmodule QtWebEngineCore + \since 5.6 + + The structure specifies properties of a cookie, and whether it should accepted or not. It is + used as an argument to a filter installed via setCookieFilter(). +*/ + +/*! + \variable QWebEngineCookieStoreClient::FilterRequest::accepted + Whether the cookie shall be accepted. The default is \c true. + \variable QWebEngineCookieStoreClient::FilterRequest::firstPartyUrl + URL of page that triggered the setting of the cookie. + \variable QWebEngineCookieStoreClient::FilterRequest::cookieLine + Content of the cookie. + \variable QWebEngineCookieStoreClient::FilterRequest::cookieSource + URL of site that sets the cookie. +*/ + + /*! \fn void QWebEngineCookieStoreClient::cookieAdded(const QNetworkCookie &cookie) @@ -355,20 +383,21 @@ void QWebEngineCookieStoreClient::deleteAllCookies() } /*! - This virtual function is called before a new cookie is added to the cookie store. - By overriding this function and returning \c false the user can decide to deny - from \a firstPartyUrl the cookie \a cookieLine with the source \a cookieSource. - The request's \a firstPartyUrl can be used to identify a third-party cookie. - This function should not be used to execute heavy tasks since it is running on the + \fn void QWebEngineCookieStoreClient::setCookieFilter(FunctorOrLambda filterCallback) + + Installs a cookie filter that can reject cookies before they are added to the cookie store. + The \a filterCallback must be a lambda or functor taking FilterRequest structure. If the + cookie is to be rejected, the filter can set FilterRequest::accepted to \c false. + + The callback should not be used to execute heavy tasks since it is running on the IO thread and therefore blocks the Chromium networking. -*/ -bool QWebEngineCookieStoreClient::acceptCookie(const QUrl &firstPartyUrl, const QByteArray &cookieLine, const QUrl &cookieSource) + \sa deleteAllCookiesWithCallback(), getAllCookies() +*/ +void QWebEngineCookieStoreClient::setCookieFilter(const QWebEngineCallback &filter) { - Q_UNUSED(firstPartyUrl); - Q_UNUSED(cookieLine); - Q_UNUSED(cookieSource); - return true; + Q_D(QWebEngineCookieStoreClient); + d->filterCallback = filter; } QT_END_NAMESPACE diff --git a/src/core/api/qwebenginecookiestoreclient.h b/src/core/api/qwebenginecookiestoreclient.h index 3a6f54ea7..8bdb988e2 100644 --- a/src/core/api/qwebenginecookiestoreclient.h +++ b/src/core/api/qwebenginecookiestoreclient.h @@ -46,6 +46,7 @@ #include namespace QtWebEngineCore { +class BrowserContextAdapter; class CookieMonsterDelegateQt; } @@ -54,8 +55,15 @@ QT_BEGIN_NAMESPACE class QWebEngineCookieStoreClientPrivate; class QWEBENGINE_EXPORT QWebEngineCookieStoreClient : public QObject { Q_OBJECT + public: - explicit QWebEngineCookieStoreClient(QObject *parent = 0); + struct FilterRequest { + bool accepted; + + QUrl firstPartyUrl; + QByteArray cookieLine; + QUrl cookieSource; + }; virtual ~QWebEngineCookieStoreClient(); #ifdef Q_QDOC @@ -63,24 +71,26 @@ public: void deleteSessionCookiesWithCallback(FunctorOrLambda resultCallback); void deleteAllCookiesWithCallback(FunctorOrLambda resultCallback); void getAllCookies(FunctorOrLambda resultCallback); + void setCookieFilter(FunctorOrLambda filterCallback); #else void setCookieWithCallback(const QNetworkCookie &cookie, const QWebEngineCallback &resultCallback, const QUrl &origin = QUrl()); void deleteSessionCookiesWithCallback(const QWebEngineCallback &resultCallback); void deleteAllCookiesWithCallback(const QWebEngineCallback &resultCallback); void getAllCookies(const QWebEngineCallback &resultCallback); + void setCookieFilter(const QWebEngineCallback &filterCallback); #endif void setCookie(const QNetworkCookie &cookie, const QUrl &origin = QUrl()); void deleteCookie(const QNetworkCookie &cookie, const QUrl &origin = QUrl()); void deleteSessionCookies(); void deleteAllCookies(); - virtual bool acceptCookie(const QUrl &firstPartyUrl, const QByteArray &cookieLine, const QUrl &cookieSource); - Q_SIGNALS: void cookieAdded(const QNetworkCookie &cookie); void cookieRemoved(const QNetworkCookie &cookie); private: + explicit QWebEngineCookieStoreClient(QObject *parent = 0); + friend class QtWebEngineCore::BrowserContextAdapter; friend class QtWebEngineCore::CookieMonsterDelegateQt; Q_DISABLE_COPY(QWebEngineCookieStoreClient) Q_DECLARE_PRIVATE(QWebEngineCookieStoreClient) diff --git a/src/core/api/qwebenginecookiestoreclient_p.h b/src/core/api/qwebenginecookiestoreclient_p.h index a5b0a7068..43652fba6 100644 --- a/src/core/api/qwebenginecookiestoreclient_p.h +++ b/src/core/api/qwebenginecookiestoreclient_p.h @@ -74,6 +74,7 @@ class QWEBENGINE_PRIVATE_EXPORT QWebEngineCookieStoreClientPrivate { public: Q_DECLARE_PUBLIC(QWebEngineCookieStoreClient) QtWebEngineCore::CallbackDirectory callbackDirectory; + QWebEngineCallback filterCallback; QList m_pendingUserCookies; quint64 m_nextCallbackId; bool m_deleteSessionCookiesPending; diff --git a/src/core/browser_context_adapter.cpp b/src/core/browser_context_adapter.cpp index fc5dc8c21..b5fdf2ce0 100644 --- a/src/core/browser_context_adapter.cpp +++ b/src/core/browser_context_adapter.cpp @@ -138,16 +138,11 @@ DownloadManagerDelegateQt *BrowserContextAdapter::downloadManagerDelegate() QWebEngineCookieStoreClient *BrowserContextAdapter::cookieStoreClient() { + if (!m_cookieStoreClient) + m_cookieStoreClient.reset(new QWebEngineCookieStoreClient); return m_cookieStoreClient.data(); } -void BrowserContextAdapter::setCookieStoreClient(QWebEngineCookieStoreClient *client) -{ - m_cookieStoreClient = client; - if (m_browserContext->url_request_getter_.get()) - m_browserContext->url_request_getter_->updateStorageSettings(); -} - QWebEngineUrlRequestInterceptor *BrowserContextAdapter::requestInterceptor() { return m_requestInterceptor.data(); diff --git a/src/core/browser_context_adapter.h b/src/core/browser_context_adapter.h index 6d42f0b06..818dfda3d 100644 --- a/src/core/browser_context_adapter.h +++ b/src/core/browser_context_adapter.h @@ -74,7 +74,6 @@ public: DownloadManagerDelegateQt *downloadManagerDelegate(); QWebEngineCookieStoreClient *cookieStoreClient(); - void setCookieStoreClient(QWebEngineCookieStoreClient *client); QWebEngineUrlRequestInterceptor* requestInterceptor(); void setRequestInterceptor(QWebEngineUrlRequestInterceptor *interceptor); @@ -165,7 +164,7 @@ private: QScopedPointer m_visitedLinksManager; QScopedPointer m_downloadManagerDelegate; QScopedPointer m_userScriptController; - QPointer m_cookieStoreClient; + QScopedPointer m_cookieStoreClient; QPointer m_requestInterceptor; QString m_dataPath; diff --git a/src/webengine/api/qquickwebengineprofile.cpp b/src/webengine/api/qquickwebengineprofile.cpp index b06345b83..a678fe5fa 100644 --- a/src/webengine/api/qquickwebengineprofile.cpp +++ b/src/webengine/api/qquickwebengineprofile.cpp @@ -419,16 +419,16 @@ QQuickWebEngineProfile *QQuickWebEngineProfile::defaultProfile() return profile; } -QQuickWebEngineSettings *QQuickWebEngineProfile::settings() const +QWebEngineCookieStoreClient *QQuickWebEngineProfile::cookieStoreClient() const { const Q_D(QQuickWebEngineProfile); - return d->settings(); + return d->browserContext()->cookieStoreClient(); } -void QQuickWebEngineProfile::setCookieStoreClient(QWebEngineCookieStoreClient* client) +QQuickWebEngineSettings *QQuickWebEngineProfile::settings() const { - Q_D(QQuickWebEngineProfile); - d->browserContext()->setCookieStoreClient(client); + const Q_D(QQuickWebEngineProfile); + return d->settings(); } QT_END_NAMESPACE diff --git a/src/webengine/api/qquickwebengineprofile_p.h b/src/webengine/api/qquickwebengineprofile_p.h index af5762032..2cd43caad 100644 --- a/src/webengine/api/qquickwebengineprofile_p.h +++ b/src/webengine/api/qquickwebengineprofile_p.h @@ -122,7 +122,7 @@ public: static QQuickWebEngineProfile *defaultProfile(); - Q_REVISION(1) Q_INVOKABLE void setCookieStoreClient(QWebEngineCookieStoreClient* client); + Q_REVISION(1) Q_INVOKABLE QWebEngineCookieStoreClient *cookieStoreClient() const; signals: void storageNameChanged(); diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp index 0d9e400ed..2edebdfeb 100644 --- a/src/webenginewidgets/api/qwebengineprofile.cpp +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -443,19 +443,6 @@ QWebEngineCookieStoreClient* QWebEngineProfile::cookieStoreClient() return d->browserContext()->cookieStoreClient(); } -/*! - Registers a cookie store client singleton \a client to access Chromium's cookies. - - The profile does not take ownership of the pointer. - - \sa QWebEngineCookieStoreClient -*/ - -void QWebEngineProfile::setCookieStoreClient(QWebEngineCookieStoreClient *client) -{ - Q_D(QWebEngineProfile); - d->browserContext()->setCookieStoreClient(client); -} /*! Registers a request interceptor singleton \a interceptor to intercept URL requests. diff --git a/src/webenginewidgets/api/qwebengineprofile.h b/src/webenginewidgets/api/qwebengineprofile.h index 4bed186fb..7e03c065c 100644 --- a/src/webenginewidgets/api/qwebengineprofile.h +++ b/src/webenginewidgets/api/qwebengineprofile.h @@ -100,7 +100,6 @@ public: void setHttpCacheMaximumSize(int maxSize); QWebEngineCookieStoreClient* cookieStoreClient(); - void setCookieStoreClient(QWebEngineCookieStoreClient *client); void setRequestInterceptor(QWebEngineUrlRequestInterceptor *interceptor); void clearAllVisitedLinks(); -- cgit v1.2.3