WebBrowser/SafeBrowsing/SafeBrowsingManager.py

changeset 6234
fb1f9e681848
parent 6233
a64b986abb54
child 6645
ad476851d7e0
equal deleted inserted replaced
6233:a64b986abb54 6234:fb1f9e681848
258 258
259 # step 2: update threat lists 259 # step 2: update threat lists
260 threatListsForRemove = {} 260 threatListsForRemove = {}
261 for threatList, _clientState in self.__cache.getThreatLists(): 261 for threatList, _clientState in self.__cache.getThreatLists():
262 threatListsForRemove[repr(threatList)] = threatList 262 threatListsForRemove[repr(threatList)] = threatList
263 threatLists = self.__apiClient.getThreatLists() 263 threatLists, error = self.__apiClient.getThreatLists()
264 if error:
265 return False, error
266
264 maximum = len(threatLists) 267 maximum = len(threatLists)
265 current = 0 268 current = 0
266 self.progressMessage.emit(self.tr("Updating threat lists"), maximum) 269 self.progressMessage.emit(self.tr("Updating threat lists"), maximum)
267 for entry in threatLists: 270 for entry in threatLists:
268 current += 1 271 current += 1
290 # step 3: update threats 293 # step 3: update threats
291 threatLists = self.__cache.getThreatLists() 294 threatLists = self.__cache.getThreatLists()
292 clientStates = {} 295 clientStates = {}
293 for threatList, clientState in threatLists: 296 for threatList, clientState in threatLists:
294 clientStates[threatList.asTuple()] = clientState 297 clientStates[threatList.asTuple()] = clientState
295 threatsUpdateResponses = \ 298 threatsUpdateResponses, error = \
296 self.__apiClient.getThreatsUpdate(clientStates) 299 self.__apiClient.getThreatsUpdate(clientStates)
300 if error:
301 return False, error
302
297 maximum = len(threatsUpdateResponses) 303 maximum = len(threatsUpdateResponses)
298 current = 0 304 current = 0
299 self.progressMessage.emit(self.tr("Updating hash prefixes"), maximum) 305 self.progressMessage.emit(self.tr("Updating hash prefixes"), maximum)
300 for response in threatsUpdateResponses: 306 for response in threatsUpdateResponses:
301 current += 1 307 current += 1
377 """ 383 """
378 Public method to lookup an URL. 384 Public method to lookup an URL.
379 385
380 @param url URL to be checked 386 @param url URL to be checked
381 @type str or QUrl 387 @type str or QUrl
382 @return list of threat lists the URL was found in 388 @return tuple containing the list of threat lists the URL was found in
383 @rtype list of ThreatList 389 and an error message
390 @rtype tuple of (list of ThreatList, str)
384 @exception ValueError raised for an invalid URL 391 @exception ValueError raised for an invalid URL
385 """ 392 """
386 # TODO: extend to return error string in case of issues
387 if self.isEnabled(): 393 if self.isEnabled():
388 if self.__useLookupApi: 394 if self.__useLookupApi:
389 if isinstance(url, str): 395 if isinstance(url, str):
390 url = QUrl(url.strip()) 396 url = QUrl(url.strip())
391 397
392 if url.isEmpty(): 398 if url.isEmpty():
393 raise ValueError("Empty URL given.") 399 raise ValueError("Empty URL given.")
394 400
395 listNames = self.__apiClient.lookupUrl(url, self.__platforms) 401 listNames, error = \
396 if listNames: 402 self.__apiClient.lookupUrl(url, self.__platforms)
397 return listNames 403 return listNames, error
398 else: 404 else:
399 if isinstance(url, QUrl): 405 if isinstance(url, QUrl):
400 urlStr = url.toString().strip() 406 urlStr = url.toString().strip()
401 else: 407 else:
402 urlStr = url.strip() 408 urlStr = url.strip()
404 if not urlStr: 410 if not urlStr:
405 raise ValueError("Empty URL given.") 411 raise ValueError("Empty URL given.")
406 412
407 urlHashes = SafeBrowsingUrl(urlStr).hashes() 413 urlHashes = SafeBrowsingUrl(urlStr).hashes()
408 listNames = self.__lookupHashes(urlHashes) 414 listNames = self.__lookupHashes(urlHashes)
409 if listNames: 415
410 return listNames 416 return listNames, ""
411 417
412 return None 418 return None, ""
413 419
414 def __lookupHashes(self, fullHashes): 420 def __lookupHashes(self, fullHashes):
415 """ 421 """
416 Private method to lookup the given hashes. 422 Private method to lookup the given hashes.
417 423

eric ide

mercurial