WebBrowser/SafeBrowsing/SafeBrowsingAPIClient.py

branch
safe_browsing
changeset 5829
d3448873ced3
parent 5820
b610cb5b501a
child 5831
536d97e3f1a1
--- a/WebBrowser/SafeBrowsing/SafeBrowsingAPIClient.py	Sun Jul 30 19:56:04 2017 +0200
+++ b/WebBrowser/SafeBrowsing/SafeBrowsingAPIClient.py	Fri Aug 04 18:38:45 2017 +0200
@@ -174,7 +174,7 @@
         
         for (threatType, platformType, threatEntryType), currentState in \
                 clientState.items():
-            requestBody["clientStates"].append(clientState)
+            requestBody["clientStates"].append(currentState)
             if threatType not in requestBody["threatInfo"]["threatTypes"]:
                 requestBody["threatInfo"]["threatTypes"].append(threatType)
             if platformType not in \
@@ -229,7 +229,7 @@
         if not self.__fairUse or minimumWaitDuration is None:
             self.__nextRequestNoSoonerThan = QDateTime()
         else:
-            waitDuration = int(minimumWaitDuration.rstrip("s"))
+            waitDuration = int(float(minimumWaitDuration.rstrip("s")))
             self.__nextRequestNoSoonerThan = \
                 QDateTime.currentDateTime().addSecs(waitDuration)
     
@@ -244,3 +244,169 @@
             self.__fairUse and
             QDateTime.currentDateTime() >= self.__nextRequestNoSoonerThan
         ) or not self.__fairUse
+    
+    def getFairUseDelayExpirationDateTime(self):
+        """
+        Public method to get the date and time the fair use delay will expire.
+        
+        @return fair use delay expiration date and time
+        @rtype QDateTime
+        """
+        return self.__nextRequestNoSoonerThan
+    
+    @classmethod
+    def getThreatMessage(cls, threatType):
+        """
+        Class method to get a warning message for the given threat type.
+        
+        @param threatType threat type to get the message for
+        @type str
+        @return threat message
+        @rtype str
+        """
+        threatType = threatType.lower()
+        if threatType == "malware":
+            msg = QCoreApplication.translate(
+                "SafeBrowsingAPI",
+                "<h3>Malware Warning</h3>"
+                "<p>The web site you are about to visit may try to install"
+                " harmful programs on your computer in order to steal or"
+                " destroy your data.</p>")
+        elif threatType == "social_engineering":
+            msg = QCoreApplication.translate(
+                "SafeBrowsingAPI",
+                "<h3>Phishing Warning</h3>"
+                "<p>The web site you are about to visit may try to trick you"
+                " into doing something dangerous online, such as revealing"
+                " passwords or personal information, usually through a fake"
+                " website.</p>")
+        elif threatType == "unwanted_software":
+            msg = QCoreApplication.translate(
+                "SafeBrowsingAPI",
+                "<h3>Unwanted Software Warning</h3>"
+                "<p>The software you are about to download may negatively"
+                " affect your browsing or computing experience.</p>")
+        elif threatType == "potentially_harmful_application":
+            msg = QCoreApplication.translate(
+                "SafeBrowsingAPI",
+                "<h3>Potentially Harmful Application</h3>"
+                "<p>The web site you are about to visit may try to trick you"
+                " into installing applications, that may negatively affect"
+                " your browsing experience.</p>")
+        else:
+            # unknow threat
+            msg = QCoreApplication.translate(
+                "SafeBrowsingAPI",
+                "<h3>Unknown Threat Warning</h3>"
+                "<p>The web site you are about to visit was found in the Safe"
+                " Browsing Database but was not classified yet.</p>")
+        
+        return msg
+    
+    @classmethod
+    def getThreatType(cls, threatType):
+        """
+        Class method to get a display string for a given threat type.
+        
+        @param threatType threat type to get display string for
+        @type str
+        @return display string
+        @rtype str
+        """
+        threatType = threatType.lower()
+        if threatType == "malware":
+            displayString = QCoreApplication.translate(
+                "SafeBrowsingAPI", "Malware")
+        elif threatType == "social_engineering":
+            displayString = QCoreApplication.translate(
+                "SafeBrowsingAPI", "Phishing")
+        elif threatType == "unwanted_software":
+            displayString = QCoreApplication.translate(
+                "SafeBrowsingAPI", "Unwanted Software")
+        elif threatType == "potentially_harmful_application":
+            displayString = QCoreApplication.translate(
+                "SafeBrowsingAPI", "Harmful Application")
+        elif threatType == "malcious_binary":
+            displayString = QCoreApplication.translate(
+                "SafeBrowsingAPI", "Malicious Binary")
+        else:
+            displayString = QCoreApplication.translate(
+                "SafeBrowsingAPI", "Unknown Threat")
+        
+        return displayString
+    
+    @classmethod
+    def getPlatformString(cls, platformType):
+        """
+        Class method to get the platform string for a given platform type.
+        
+        @param platformType platform type as defined in the v4 API
+        @type str
+        @return platform string
+        @rtype str
+        """
+        platformStrings = {
+            "WINDOWS": "Windows",
+            "LINUX": "Linux",
+            "ANDROID": "Android",
+            "OSX": "macOS",
+            "IOS": "iOS",
+            "CHROME": "Chrome OS",
+        }
+        if platformType in platformStrings:
+            return platformStrings[platformType]
+        
+        if platformType == "ANY_PLATFORM":
+            return QCoreApplication.translate(
+                "SafeBrowsingAPI", "any defined platform")
+        elif platformType == "ALL_PLATFORMS":
+            return QCoreApplication.translate(
+                "SafeBrowsingAPI", "all defined platforms")
+        else:
+            return QCoreApplication.translate(
+                "SafeBrowsingAPI", "unknown platform")
+    
+    @classmethod
+    def getThreatEntryString(cls, threatEntry):
+        """
+        Class method to get the threat entry string.
+        
+        @param threatEntry threat entry type as defined in the v4 API
+        @type str
+        @return threat entry string
+        @rtype str
+        """
+        if threatEntry == "URL":
+            return "URL"
+        elif threatEntry == "EXECUTABLE":
+            return QCoreApplication.translate(
+                "SafeBrowsingAPI", "executable program")
+        else:
+            return QCoreApplication.translate(
+                "SafeBrowsingAPI", "unknown type")
+    
+    @classmethod
+    def getPlatformTypes(cls, platform):
+        """
+        Class method to get the platform types for a given platform.
+        
+        @param platform platform string
+        @type str (one of 'linux', 'windows', 'macos')
+        @return list of platform types as defined in the v4 API for the
+            given platform
+        @rtype list of str
+        @exception ValueError raised to indicate an invalid platform string
+        """
+        platform = platform.lower()
+        
+        platformTypes = ["ANY_PLATFORM", "ALL_PLATFORMS"]
+        if platform == "linux":
+            platformTypes.append("LINUX")
+        elif platform == "windows":
+            platformTypes.append("WINDOWS")
+        elif platform == "macos":
+            platformTypes.append("OSX")
+        else:
+            raise ValueError("Unsupported platform")
+        
+        return platformTypes

eric ide

mercurial