WebBrowser/SafeBrowsing/SafeBrowsingManager.py

changeset 6233
a64b986abb54
parent 6230
77a1b22c8a1f
child 6234
fb1f9e681848
equal deleted inserted replaced
6232:d2eac9616a92 6233:a64b986abb54
67 Utilities.getConfigDir(), "web_browser", "safe_browsing") 67 Utilities.getConfigDir(), "web_browser", "safe_browsing")
68 self.__cache = SafeBrowsingCache(gsbCachePath, self) 68 self.__cache = SafeBrowsingCache(gsbCachePath, self)
69 69
70 self.__gsbDialog = None 70 self.__gsbDialog = None
71 self.__setPlatforms() 71 self.__setPlatforms()
72 self.__setLookupMethod()
72 73
73 self.__updatingThreatLists = False 74 self.__updatingThreatLists = False
74 self.__threatListsUpdateTimer = QTimer(self) 75 self.__threatListsUpdateTimer = QTimer(self)
75 self.__threatListsUpdateTimer.setSingleShot(True) 76 self.__threatListsUpdateTimer.setSingleShot(True)
76 self.__threatListsUpdateTimer.timeout.connect( 77 self.__threatListsUpdateTimer.timeout.connect(
94 SafeBrowsingManager.enabled = ( 95 SafeBrowsingManager.enabled = (
95 Preferences.getWebBrowser("SafeBrowsingEnabled") and 96 Preferences.getWebBrowser("SafeBrowsingEnabled") and
96 bool(self.__apiKey)) 97 bool(self.__apiKey))
97 98
98 self.__setPlatforms() 99 self.__setPlatforms()
100 self.__setLookupMethod()
99 self.__setAutoUpdateThreatLists() 101 self.__setAutoUpdateThreatLists()
100 102
101 def __setPlatforms(self): 103 def __setPlatforms(self):
102 """ 104 """
103 Private method to set the platforms to be checked against. 105 Private method to set the platforms to be checked against.
110 platform = "macos" 112 platform = "macos"
111 else: 113 else:
112 # treat all other platforms like linux 114 # treat all other platforms like linux
113 platform = "linux" 115 platform = "linux"
114 self.__platforms = SafeBrowsingAPIClient.getPlatformTypes(platform) 116 self.__platforms = SafeBrowsingAPIClient.getPlatformTypes(platform)
117
118 def __setLookupMethod(self):
119 """
120 Private method to set the lookup method (Update API or Lookup API).
121 """
122 self.__useLookupApi = Preferences.getWebBrowser(
123 "SafeBrowsingUseLookupApi")
115 124
116 @classmethod 125 @classmethod
117 def isEnabled(cls): 126 def isEnabled(cls):
118 """ 127 """
119 Class method to check, if safe browsing is enabled. 128 Class method to check, if safe browsing is enabled.
168 177
169 def __setAutoUpdateThreatLists(self): 178 def __setAutoUpdateThreatLists(self):
170 """ 179 """
171 Private method to set auto update for the threat lists. 180 Private method to set auto update for the threat lists.
172 """ 181 """
173 autoUpdateEnabled = Preferences.getWebBrowser("SafeBrowsingAutoUpdate") 182 autoUpdateEnabled = \
183 Preferences.getWebBrowser("SafeBrowsingAutoUpdate") and \
184 not Preferences.getWebBrowser("SafeBrowsingUseLookupApi")
174 if autoUpdateEnabled and self.isEnabled(): 185 if autoUpdateEnabled and self.isEnabled():
175 nextUpdateDateTime = Preferences.getWebBrowser( 186 nextUpdateDateTime = Preferences.getWebBrowser(
176 "SafeBrowsingUpdateDateTime") 187 "SafeBrowsingUpdateDateTime")
177 if nextUpdateDateTime.isValid(): 188 if nextUpdateDateTime.isValid():
178 interval = \ 189 interval = \
370 @type str or QUrl 381 @type str or QUrl
371 @return list of threat lists the URL was found in 382 @return list of threat lists the URL was found in
372 @rtype list of ThreatList 383 @rtype list of ThreatList
373 @exception ValueError raised for an invalid URL 384 @exception ValueError raised for an invalid URL
374 """ 385 """
386 # TODO: extend to return error string in case of issues
375 if self.isEnabled(): 387 if self.isEnabled():
376 # TODO: add branch for the lookup API 388 if self.__useLookupApi:
377 if isinstance(url, QUrl): 389 if isinstance(url, str):
378 urlStr = url.toString().strip() 390 url = QUrl(url.strip())
391
392 if url.isEmpty():
393 raise ValueError("Empty URL given.")
394
395 listNames = self.__apiClient.lookupUrl(url, self.__platforms)
396 if listNames:
397 return listNames
379 else: 398 else:
380 urlStr = url.strip() 399 if isinstance(url, QUrl):
381 400 urlStr = url.toString().strip()
382 if not urlStr: 401 else:
383 raise ValueError("Empty URL given.") 402 urlStr = url.strip()
384 403
385 urlHashes = SafeBrowsingUrl(urlStr).hashes() 404 if not urlStr:
386 listNames = self.__lookupHashes(urlHashes) 405 raise ValueError("Empty URL given.")
387 if listNames: 406
388 return listNames 407 urlHashes = SafeBrowsingUrl(urlStr).hashes()
408 listNames = self.__lookupHashes(urlHashes)
409 if listNames:
410 return listNames
389 411
390 return None 412 return None
391 413
392 def __lookupHashes(self, fullHashes): 414 def __lookupHashes(self, fullHashes):
393 """ 415 """

eric ide

mercurial