WebBrowser/SafeBrowsing/SafeBrowsingManager.py

changeset 5839
fe4d62e23908
parent 5831
536d97e3f1a1
child 5842
c3f41b959a65
diff -r 4c2cace2263a -r fe4d62e23908 WebBrowser/SafeBrowsing/SafeBrowsingManager.py
--- a/WebBrowser/SafeBrowsing/SafeBrowsingManager.py	Tue Aug 08 10:53:10 2017 +0200
+++ b/WebBrowser/SafeBrowsing/SafeBrowsingManager.py	Tue Aug 08 17:20:28 2017 +0200
@@ -19,7 +19,8 @@
 import os
 import base64
 
-from PyQt5.QtCore import pyqtSignal, QObject, QCoreApplication, QUrl
+from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QCoreApplication, \
+    QUrl, QDateTime, QTimer
 
 import Preferences
 import Utilities
@@ -64,6 +65,13 @@
         
         self.__gsbDialog = None
         self.__setPlatforms()
+        
+        self.__updatingThreatLists = False
+        self.__threatListsUpdateTimer = QTimer(self)
+        self.__threatListsUpdateTimer.setSingleShot(True)
+        self.__threatListsUpdateTimer.timeout.connect(
+            self.__threatListsUpdateTimerTimeout)
+        self.__setAutoUpdateThreatLists()
     
     def configurationChanged(self):
         """
@@ -84,6 +92,7 @@
             bool(self.__apiKey))
         
         self.__setPlatforms()
+        self.__setAutoUpdateThreatLists()
     
     def __setPlatforms(self):
         """
@@ -124,6 +133,69 @@
         """
         return self.__enabled and self.__apiClient.fairUseDelayExpired()
     
+    def __showStatusBarMessage(self, message):
+        """
+        Private method to show some message in the main window status bar.
+        
+        @param message message to be shown
+        @type str
+        """
+        from WebBrowser.WebBrowserWindow import WebBrowserWindow
+        WebBrowserWindow.mainWindow().statusBar().showMessage(message, 5000)
+    
+    def __setAutoUpdateThreatLists(self):
+        """
+        Private method to set auto update for the threat lists.
+        """
+        autoUpdateEnabled = Preferences.getWebBrowser("SafeBrowsingAutoUpdate")
+        if autoUpdateEnabled and self.__enabled:
+            nextUpdateDateTime = Preferences.getWebBrowser(
+                "SafeBrowsingUpdateDateTime")
+            if nextUpdateDateTime.isValid():
+                interval = \
+                    QDateTime.currentDateTime().secsTo(nextUpdateDateTime) + 2
+                # 2 seconds extra wait time; interval in milliseconds
+                
+                if interval < 5:
+                    interval = 5
+                    # minimum 5 seconds interval
+            else:
+                interval = 5
+                # just wait 5 seconds
+            self.__threatListsUpdateTimer.start(interval * 1000)
+        else:
+            if self.__threatListsUpdateTimer.isActive():
+                self.__threatListsUpdateTimer.stop()
+    
+    @pyqtSlot()
+    def __threatListsUpdateTimerTimeout(self):
+        """
+        Private slot to perform the auto update of the threat lists.
+        """
+        ok = False
+        if self.__enabled:
+            self.__showStatusBarMessage(self.tr("Updating threat lists..."))
+            ok = self.updateHashPrefixCache()[0]
+            if ok:
+                self.__showStatusBarMessage(
+                    self.tr("Updating threat lists done"))
+            else:
+                self.__showStatusBarMessage(
+                    self.tr("Updating threat lists failed"))
+        
+        if ok:
+            nextUpdateDateTime = \
+                self.__apiClient.getFairUseDelayExpirationDateTime()
+            Preferences.setWebBrowser("SafeBrowsingUpdateDateTime",
+                                      nextUpdateDateTime)
+            self.__threatListsUpdateTimer.start(
+                (QDateTime.currentDateTime().secsTo(nextUpdateDateTime) + 2) *
+                1000)
+            # 2 seconds extra wait time; interval in milliseconds
+        else:
+            Preferences.setWebBrowser("SafeBrowsingUpdateDateTime",
+                                      QDateTime())
+    
     def updateHashPrefixCache(self):
         """
         Public method to load or update the locally cached threat lists.
@@ -141,6 +213,10 @@
                     self.__apiClient.getFairUseDelayExpirationDateTime()
                     .toString("yyyy-MM-dd, HH:mm:ss"))
         
+        self.__updatingThreatLists = True
+        ok = True
+        errorMessage = ""
+        
         # step 1: remove expired hashes
         self.__cache.cleanupFullHashes()
         
@@ -209,12 +285,24 @@
                 self.__cache.updateThreatListClientState(
                     responseThreatList, response["newClientState"])
             else:
-                return False, \
-                    self.tr("Local cache checksum does not match the server."
-                            " Consider cleaning the cache. Threat update has"
-                            " been aborted.")
+                ok = False
+                errorMessage = self.tr(
+                    "Local cache checksum does not match the server. Consider"
+                    " cleaning the cache. Threat update has been aborted.")
+        
+        self.__updatingThreatLists = False
         
-        return True, ""
+        return ok, errorMessage
+    
+    def isUpdatingThreatLists(self):
+        """
+        Public method to check, if we are in the process of updating the
+        threat lists.
+        
+        @return flag indicating an update process is active
+        @rtype bool
+        """
+        return self.__updatingThreatLists
     
     def __verifyThreatListChecksum(self, threatList, remoteChecksum):
         """

eric ide

mercurial