--- a/WebBrowser/SafeBrowsing/SafeBrowsingManager.py Tue Apr 10 19:43:45 2018 +0200 +++ b/WebBrowser/SafeBrowsing/SafeBrowsingManager.py Wed Apr 11 19:57:23 2018 +0200 @@ -69,6 +69,7 @@ self.__gsbDialog = None self.__setPlatforms() + self.__setLookupMethod() self.__updatingThreatLists = False self.__threatListsUpdateTimer = QTimer(self) @@ -96,6 +97,7 @@ bool(self.__apiKey)) self.__setPlatforms() + self.__setLookupMethod() self.__setAutoUpdateThreatLists() def __setPlatforms(self): @@ -113,6 +115,13 @@ platform = "linux" self.__platforms = SafeBrowsingAPIClient.getPlatformTypes(platform) + def __setLookupMethod(self): + """ + Private method to set the lookup method (Update API or Lookup API). + """ + self.__useLookupApi = Preferences.getWebBrowser( + "SafeBrowsingUseLookupApi") + @classmethod def isEnabled(cls): """ @@ -170,7 +179,9 @@ """ Private method to set auto update for the threat lists. """ - autoUpdateEnabled = Preferences.getWebBrowser("SafeBrowsingAutoUpdate") + autoUpdateEnabled = \ + Preferences.getWebBrowser("SafeBrowsingAutoUpdate") and \ + not Preferences.getWebBrowser("SafeBrowsingUseLookupApi") if autoUpdateEnabled and self.isEnabled(): nextUpdateDateTime = Preferences.getWebBrowser( "SafeBrowsingUpdateDateTime") @@ -372,20 +383,31 @@ @rtype list of ThreatList @exception ValueError raised for an invalid URL """ + # TODO: extend to return error string in case of issues if self.isEnabled(): - # TODO: add branch for the lookup API - if isinstance(url, QUrl): - urlStr = url.toString().strip() + if self.__useLookupApi: + if isinstance(url, str): + url = QUrl(url.strip()) + + if url.isEmpty(): + raise ValueError("Empty URL given.") + + listNames = self.__apiClient.lookupUrl(url, self.__platforms) + if listNames: + return listNames else: - urlStr = url.strip() - - if not urlStr: - raise ValueError("Empty URL given.") - - urlHashes = SafeBrowsingUrl(urlStr).hashes() - listNames = self.__lookupHashes(urlHashes) - if listNames: - return listNames + if isinstance(url, QUrl): + urlStr = url.toString().strip() + else: + urlStr = url.strip() + + if not urlStr: + raise ValueError("Empty URL given.") + + urlHashes = SafeBrowsingUrl(urlStr).hashes() + listNames = self.__lookupHashes(urlHashes) + if listNames: + return listNames return None