WebBrowser/SafeBrowsing/SafeBrowsingCache.py

branch
safe_browsing
changeset 5829
d3448873ced3
parent 5820
b610cb5b501a
child 5847
fd1262c3fa79
equal deleted inserted replaced
5821:6c7766cde4c1 5829:d3448873ced3
16 16
17 from __future__ import unicode_literals, division 17 from __future__ import unicode_literals, division
18 18
19 import os 19 import os
20 20
21 from PyQt5.QtCore import QObject, QByteArray, QCryptographicHash 21 from PyQt5.QtCore import QObject, QByteArray, QCryptographicHash, \
22 QCoreApplication, QEventLoop
22 from PyQt5.QtSql import QSql, QSqlDatabase, QSqlQuery 23 from PyQt5.QtSql import QSql, QSqlDatabase, QSqlQuery
23 24
24 from .SafeBrowsingUtilities import toHex 25 from .SafeBrowsingUtilities import toHex
25 26
26 27
281 if db.isOpen(): 282 if db.isOpen():
282 db.transaction() 283 db.transaction()
283 try: 284 try:
284 query = QSqlQuery(db) 285 query = QSqlQuery(db)
285 query.prepare( 286 query.prepare(
286 queryStr.format(",".join(["?" * len(hashValues)]))) 287 queryStr.format(",".join(["?"] * len(hashValues))))
287 for hashValue in hashValues: 288 for hashValue in hashValues:
288 query.addBindValue(QByteArray(hashValue), 289 query.addBindValue(QByteArray(hashValue),
289 QSql.In | QSql.Binary) 290 QSql.In | QSql.Binary)
290 291
291 query.exec_() 292 query.exec_()
292 293
293 while query.next(): 294 while query.next():
294 threatType = query.value(0) 295 threatType = query.value(0)
295 platformType = query.value(1) 296 platformType = query.value(1)
296 threatEntryType = query.value(2) 297 threatEntryType = query.value(2)
297 hasExpired = query.value(3) # TODO: check if bool 298 hasExpired = bool(query.value(3))
298 threatList = ThreatList(threatType, platformType, 299 threatList = ThreatList(threatType, platformType,
299 threatEntryType) 300 threatEntryType)
300 output.append((threatList, hasExpired)) 301 output.append((threatList, hasExpired))
301 del query 302 del query
302 finally: 303 finally:
325 if db.isOpen(): 326 if db.isOpen():
326 db.transaction() 327 db.transaction()
327 try: 328 try:
328 query = QSqlQuery(db) 329 query = QSqlQuery(db)
329 query.prepare( 330 query.prepare(
330 queryStr.format(",".join(["?" * len(prefixes)]))) 331 queryStr.format(",".join(["?"] * len(prefixes))))
331 for prefix in prefixes: 332 for prefix in prefixes:
332 query.addBindValue(prefix) 333 query.addBindValue(prefix)
333 334
334 query.exec_() 335 query.exec_()
335 336
336 while query.next(): 337 while query.next():
337 fullHash = bytes(query.value(0)) 338 fullHash = bytes(query.value(0))
338 threatType = query.value(1) 339 threatType = query.value(1)
339 platformType = query.value(2) 340 platformType = query.value(2)
340 threatEntryType = query.value(3) 341 threatEntryType = query.value(3)
341 negativeCacheExpired = query.value(4) # TODO: check if bool 342 negativeCacheExpired = bool(query.value(4))
342 threatList = ThreatList(threatType, platformType, 343 threatList = ThreatList(threatType, platformType,
343 threatEntryType) 344 threatEntryType)
344 output.append((threatList, fullHash, negativeCacheExpired)) 345 output.append((threatList, fullHash, negativeCacheExpired))
345 del query 346 del query
346 finally: 347 finally:
625 checksum = None 626 checksum = None
626 627
627 db = QSqlDatabase.database(self.__connectionName) 628 db = QSqlDatabase.database(self.__connectionName)
628 if db.isOpen(): 629 if db.isOpen():
629 db.transaction() 630 db.transaction()
630 hash = QCryptographicHash(QCryptographicHash.Sha256) 631 sha256Hash = QCryptographicHash(QCryptographicHash.Sha256)
631 try: 632 try:
632 query = QSqlQuery(db) 633 query = QSqlQuery(db)
633 query.prepare(queryStr) 634 query.prepare(queryStr)
634 query.addBindValue(threatList.threatType) 635 query.addBindValue(threatList.threatType)
635 query.addBindValue(threatList.platformType) 636 query.addBindValue(threatList.platformType)
636 query.addBindValue(threatList.threatEntryType) 637 query.addBindValue(threatList.threatEntryType)
637 638
638 query.exec_() 639 query.exec_()
639 640
640 while query.next(): 641 while query.next():
641 hash.addData(query.value(0)) 642 sha256Hash.addData(query.value(0))
643 QCoreApplication.processEvents(QEventLoop.AllEvents, 200)
642 del query 644 del query
643 finally: 645 finally:
644 db.commit() 646 db.commit()
645 647
646 checksum = bytes(hash.result()) 648 checksum = bytes(sha256Hash.result())
647 649
648 return checksum 650 return checksum
649 651
650 def populateHashPrefixList(self, threatList, prefixes): 652 def populateHashPrefixList(self, threatList, prefixes):
651 """ 653 """

eric ide

mercurial