WebBrowser/SafeBrowsing/SafeBrowsingManager.py

branch
safe_browsing
changeset 5821
6c7766cde4c1
parent 5820
b610cb5b501a
child 5829
d3448873ced3
equal deleted inserted replaced
5820:b610cb5b501a 5821:6c7766cde4c1
17 from __future__ import unicode_literals 17 from __future__ import unicode_literals
18 18
19 import os 19 import os
20 import base64 20 import base64
21 21
22 from PyQt5.QtCore import QObject 22 from PyQt5.QtCore import pyqtSignal, QObject, QCoreApplication
23 23
24 import Preferences 24 import Preferences
25 import Utilities 25 import Utilities
26 26
27 from .SafeBrowsingAPIClient import SafeBrowsingAPIClient 27 from .SafeBrowsingAPIClient import SafeBrowsingAPIClient
29 29
30 30
31 class SafeBrowsingManager(QObject): 31 class SafeBrowsingManager(QObject):
32 """ 32 """
33 Class implementing the interface for Google Safe Browsing. 33 Class implementing the interface for Google Safe Browsing.
34
35 @signal progressMessage(message,maximum) emitted to give a message for the
36 action about to be performed and the maximum value
37 @signal progress(current) emitted to signal the current progress
34 """ 38 """
39 progressMessage = pyqtSignal(str, int)
40 progress = pyqtSignal(int)
41
35 def __init__(self): 42 def __init__(self):
36 """ 43 """
37 Constructor 44 Constructor
38 """ 45 """
39 super(SafeBrowsingManager, self).__init__() 46 super(SafeBrowsingManager, self).__init__()
126 # step 2: update threat lists 133 # step 2: update threat lists
127 threatListsForRemove = {} 134 threatListsForRemove = {}
128 for threatList, clientState in self.__cache.getThreatLists(): 135 for threatList, clientState in self.__cache.getThreatLists():
129 threatListsForRemove[repr(threatList)] = threatList 136 threatListsForRemove[repr(threatList)] = threatList
130 threatLists = self.__apiClient.getThreatLists() 137 threatLists = self.__apiClient.getThreatLists()
138 maximum = len(threatLists)
139 current = 0
140 self.progressMessage.emit(self.tr("Updating threat lists"), maximum)
131 for entry in threatLists: 141 for entry in threatLists:
142 current += 1
143 self.progress.emit(current)
144 QCoreApplication.processEvents()
132 threatList = ThreatList.fromApiEntry(entry) 145 threatList = ThreatList.fromApiEntry(entry)
133 if self.__platforms is None or \ 146 if self.__platforms is None or \
134 threatList.platformType in self.__platforms: 147 threatList.platformType in self.__platforms:
135 self.__cache.addThreatList(threatList) 148 self.__cache.addThreatList(threatList)
136 key = repr(threatList) 149 key = repr(threatList)
137 if key in threatListsForRemove: 150 if key in threatListsForRemove:
138 del threatListsForRemove[key] 151 del threatListsForRemove[key]
152 maximum = len(threatListsForRemove.values())
153 current = 0
154 self.progressMessage.emit(self.tr("Deleting obsolete threat lists"),
155 maximum)
139 for threatList in threatListsForRemove.values(): 156 for threatList in threatListsForRemove.values():
157 current += 1
158 self.progress.emit(current)
159 QCoreApplication.processEvents()
140 self.__cache.deleteHashPrefixList(threatList) 160 self.__cache.deleteHashPrefixList(threatList)
141 self.__cache.deleteThreatList(threatList) 161 self.__cache.deleteThreatList(threatList)
142 del threatListsForRemove 162 del threatListsForRemove
143 163
144 # step 3: update threats 164 # step 3: update threats
146 clientStates = {} 166 clientStates = {}
147 for threatList, clientState in threatLists: 167 for threatList, clientState in threatLists:
148 clientStates[threatList.asTuple()] = clientState 168 clientStates[threatList.asTuple()] = clientState
149 threatsUpdateResponses = \ 169 threatsUpdateResponses = \
150 self.__apiClient.getThreatsUpdate(clientStates) 170 self.__apiClient.getThreatsUpdate(clientStates)
171 maximum = len(threatsUpdateResponses)
172 current = 0
173 self.progressMessage.emit(self.tr("Updating hash prefixes"), maximum)
151 for response in threatsUpdateResponses: 174 for response in threatsUpdateResponses:
175 current += 1
176 self.progress.emit(current)
177 QCoreApplication.processEvents()
152 responseThreatList = ThreatList.fromApiEntry(response) 178 responseThreatList = ThreatList.fromApiEntry(response)
153 if response["responseType"] == "FULL_UPDATE": 179 if response["responseType"] == "FULL_UPDATE":
154 self.__cache.deleteHashPrefixList(responseThreatList) 180 self.__cache.deleteHashPrefixList(responseThreatList)
155 for removal in response.get("removals", []): 181 for removal in response.get("removals", []):
156 self.__cache.removeHashPrefixIndices( 182 self.__cache.removeHashPrefixIndices(
157 responseThreatList, removal["rawIndices"]["indices"]) 183 responseThreatList, removal["rawIndices"]["indices"])
184 QCoreApplication.processEvents()
158 for addition in response.get("additions", []): 185 for addition in response.get("additions", []):
159 hashPrefixList = HashPrefixList( 186 hashPrefixList = HashPrefixList(
160 addition["rawHashes"]["prefixSize"], 187 addition["rawHashes"]["prefixSize"],
161 base64.b64decode(addition["rawHashes"]["rawHashes"])) 188 base64.b64decode(addition["rawHashes"]["rawHashes"]))
162 self.__cache.populateHashPrefixList(responseThreatList, 189 self.__cache.populateHashPrefixList(responseThreatList,
163 hashPrefixList) 190 hashPrefixList)
191 QCoreApplication.processEvents()
164 expectedChecksum = base64.b64decode(response["checksum"]["sha256"]) 192 expectedChecksum = base64.b64decode(response["checksum"]["sha256"])
165 if self.__verifyThreatListChecksum(responseThreatList, 193 if self.__verifyThreatListChecksum(responseThreatList,
166 expectedChecksum): 194 expectedChecksum):
167 self.__cache.updateThreatListClientState( 195 self.__cache.updateThreatListClientState(
168 responseThreatList, response["newClientState"]) 196 responseThreatList, response["newClientState"])

eric ide

mercurial