CookieJar: added code to support the cookie filtering of Qt 5.11+.

Wed, 04 Jul 2018 19:39:32 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 04 Jul 2018 19:39:32 +0200
changeset 6403
9b708a019463
parent 6402
bdc273bb66c5
child 6404
1a0afc7f2837

CookieJar: added code to support the cookie filtering of Qt 5.11+.

WebBrowser/CookieJar/CookieJar.py file | annotate | diff | comparison | revisions
--- a/WebBrowser/CookieJar/CookieJar.py	Wed Jul 04 19:38:44 2018 +0200
+++ b/WebBrowser/CookieJar/CookieJar.py	Wed Jul 04 19:39:32 2018 +0200
@@ -21,7 +21,6 @@
 import Preferences
 
 
-# TODO: add code to support cookie filtering as of Qt 5.11
 class CookieJar(QNetworkCookieJar):
     """
     Class implementing a QNetworkCookieJar subclass with various accept
@@ -60,6 +59,11 @@
                                           "web_browser", "cookies.ini")
         
         self.__store = WebBrowserWindow.webProfile().cookieStore()
+        try:
+            self.__store.setCookieFilter(self.__cookieFilter)
+        except AttributeError:
+            # pre Qt 5.11
+            pass
         self.__store.cookieAdded.connect(self.__cookieAdded)
         self.__store.cookieRemoved.connect(self.__cookieRemoved)
         
@@ -188,6 +192,38 @@
         if self.deleteCookie(cookie):
             self.cookiesChanged.emit()
     
+    def __cookieFilter(self, request):
+        """
+        Private method to filter cookies.
+        
+        Note: This method is used for Qt 5.11+ only.
+        
+        @param request reference to the cookie filter request object
+        @type QWebEngineCookieStore.FilterRequest
+        @return flag indicating cookie access is allowed
+        @rtype bool
+        """
+        if not self.__loaded:
+            self.__load()
+        
+        if self.__acceptCookies == self.AcceptNever:
+            res = self.__isOnDomainList(self.__exceptionsAllow,
+                                        request.origin.host())
+            if not res:
+                return False
+        
+        if self.__acceptCookies == self.AcceptAlways:
+            res = self.__isOnDomainList(self.__exceptionsBlock,
+                                        request.origin.host())
+            if res:
+                return False
+        
+        if self.__acceptCookies == self.AcceptOnlyFromSitesNavigatedTo and \
+           request.thirdParty:
+            return False
+        
+        return True
+ 
     def __rejectCookie(self, cookie, cookieDomain):
         """
         Private method to test, if a cookie shall be rejected.

eric ide

mercurial