eric6/WebBrowser/FlashCookieManager/FlashCookieManager.py

changeset 8069
1176a936efa4
parent 8068
28457602b8f8
child 8070
6758ba4670e1
equal deleted inserted replaced
8068:28457602b8f8 8069:1176a936efa4
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the Flash cookie manager.
8 """
9
10 import shutil
11
12 from PyQt5.QtCore import QObject, QTimer, QDir, QFileInfo, QFile
13
14 from .FlashCookie import FlashCookie
15 from .FlashCookieReader import FlashCookieReader, FlashCookieReaderError
16
17 import WebBrowser.WebBrowserWindow
18
19 import Preferences
20
21
22 class FlashCookieManager(QObject):
23 """
24 Class implementing the Flash cookie manager object.
25 """
26 RefreshInterval = 60 * 1000
27
28 def __init__(self, parent=None):
29 """
30 Constructor
31
32 @param parent reference to the parent object
33 @type QObject
34 """
35 super(FlashCookieManager, self).__init__(parent)
36
37 self.__flashCookieManagerDialog = None
38 self.__flashCookies = [] # list of FlashCookie
39 self.__newCookiesList = [] # list of str
40
41 self.__timer = QTimer(self)
42 self.__timer.setInterval(FlashCookieManager.RefreshInterval)
43 self.__timer.timeout.connect(self.__autoRefresh)
44
45 # start the timer if needed
46 self.__startStopTimer()
47
48 if Preferences.getWebBrowser("FlashCookiesDeleteOnStartExit"):
49 self.__loadFlashCookies()
50 self.__removeAllButWhitelisted()
51
52 def shutdown(self):
53 """
54 Public method to perform shutdown actions.
55 """
56 if self.__flashCookieManagerDialog is not None:
57 self.__flashCookieManagerDialog.close()
58
59 if Preferences.getWebBrowser("FlashCookiesDeleteOnStartExit"):
60 self.__removeAllButWhitelisted()
61
62 def setFlashCookies(self, cookies):
63 """
64 Public method to set the list of cached Flash cookies.
65
66 @param cookies list of Flash cookies to store
67 @type list of FlashCookie
68 """
69 self.__flashCookies = cookies[:]
70
71 def flashCookies(self):
72 """
73 Public method to get the list of cached Flash cookies.
74
75 @return list of Flash cookies
76 @rtype list of FlashCookie
77 """
78 if not self.__flashCookies:
79 self.__loadFlashCookies()
80
81 return self.__flashCookies[:]
82
83 def newCookiesList(self):
84 """
85 Public method to get the list of newly detected Flash cookies.
86
87 @return list of newly detected Flash cookies
88 @rtype list of str
89 """
90 return self.__newCookiesList[:]
91
92 def clearNewOrigins(self):
93 """
94 Public method to clear the list of newly detected Flash cookies.
95 """
96 self.__newCookiesList = []
97
98 def clearCache(self):
99 """
100 Public method to clear the list of cached Flash cookies.
101 """
102 self.__flashCookies = []
103
104 def __isBlacklisted(self, cookie):
105 """
106 Private method to check for a blacklisted cookie.
107
108 @param cookie Flash cookie to be tested
109 @type FlashCookie
110 @return flag indicating a blacklisted cookie
111 @rtype bool
112 """
113 return (
114 cookie.origin in Preferences.getWebBrowser("FlashCookiesBlacklist")
115 )
116
117 def __isWhitelisted(self, cookie):
118 """
119 Private method to check for a whitelisted cookie.
120
121 @param cookie Flash cookie to be tested
122 @type FlashCookie
123 @return flag indicating a whitelisted cookie
124 @rtype bool
125 """
126 return (
127 cookie.origin in Preferences.getWebBrowser("FlashCookiesWhitelist")
128 )
129
130 def __removeAllButWhitelisted(self):
131 """
132 Private method to remove all non-whitelisted cookies.
133 """
134 for cookie in self.__flashCookies[:]:
135 if not self.__isWhitelisted(cookie):
136 self.removeCookie(cookie)
137
138 def removeAllCookies(self):
139 """
140 Public method to remove all flash cookies.
141 """
142 for cookie in self.__flashCookies[:]:
143 self.removeCookie(cookie)
144 self.clearNewOrigins()
145 self.clearCache()
146
147 def __sharedObjectDirName(self):
148 """
149 Private slot to determine the path of the shared data objects.
150
151 @return path of the shared data objects
152 @rtype str
153 """
154 if (
155 "macromedia" in self.flashPlayerDataPath().lower() or
156 "/.gnash" not in self.flashPlayerDataPath().lower()
157 ):
158 return "/#SharedObjects/"
159 else:
160 return "/SharedObjects/"
161
162 def flashPlayerDataPath(self):
163 """
164 Public method to get the Flash Player data path.
165
166 @return Flash Player data path
167 @rtype str
168 """
169 return Preferences.getWebBrowser("FlashCookiesDataPath")
170
171 def preferencesChanged(self):
172 """
173 Public slot to handle a change of preferences.
174 """
175 self.__startStopTimer()
176
177 def removeCookie(self, cookie):
178 """
179 Public method to remove a cookie of the list of cached cookies.
180
181 @param cookie Flash cookie to be removed
182 @type FlashCookie
183 """
184 if cookie in self.__flashCookies:
185 self.__flashCookies.remove(cookie)
186 shutil.rmtree(cookie.path, True)
187
188 def __autoRefresh(self):
189 """
190 Private slot to refresh the list of cookies.
191 """
192 if (
193 self.__flashCookieManagerDialog and
194 self.__flashCookieManagerDialog.isVisible()
195 ):
196 return
197
198 oldFlashCookies = self.__flashCookies[:]
199 self.__loadFlashCookies()
200 newCookieList = []
201
202 for cookie in self.__flashCookies[:]:
203 if self.__isBlacklisted(cookie):
204 self.removeCookie(cookie)
205 continue
206
207 if self.__isWhitelisted(cookie):
208 continue
209
210 newCookie = True
211 for oldCookie in oldFlashCookies:
212 if (oldCookie.path + oldCookie.name ==
213 cookie.path + cookie.name):
214 newCookie = False
215 break
216
217 if newCookie:
218 newCookieList.append(cookie.path + "/" + cookie.name)
219
220 if newCookieList and Preferences.getWebBrowser("FlashCookieNotify"):
221 self.__newCookiesList.extend(newCookieList)
222 win = WebBrowser.WebBrowserWindow.WebBrowserWindow.mainWindow()
223 if win is None:
224 return
225
226 view = win.currentBrowser()
227 if view is None:
228 return
229
230 from .FlashCookieNotification import FlashCookieNotification
231 notification = FlashCookieNotification(
232 view, self, len(newCookieList))
233 notification.show()
234
235 def showFlashCookieManagerDialog(self):
236 """
237 Public method to show the Flash cookies management dialog.
238 """
239 if self.__flashCookieManagerDialog is None:
240 from .FlashCookieManagerDialog import FlashCookieManagerDialog
241 self.__flashCookieManagerDialog = FlashCookieManagerDialog(self)
242
243 self.__flashCookieManagerDialog.refreshView()
244 self.__flashCookieManagerDialog.showPage(0)
245 self.__flashCookieManagerDialog.show()
246 self.__flashCookieManagerDialog.raise_()
247
248 def __startStopTimer(self):
249 """
250 Private slot to start or stop the auto refresh timer.
251 """
252 if Preferences.getWebBrowser("FlashCookieAutoRefresh"):
253 if not self.__timer.isActive():
254 if not bool(self.__flashCookies):
255 self.__loadFlashCookies()
256
257 self.__timer.start()
258 else:
259 self.__timer.stop()
260
261 def __loadFlashCookies(self):
262 """
263 Private slot to load the Flash cookies to be cached.
264 """
265 self.__flashCookies = []
266 self.__loadFlashCookiesFromPath(self.flashPlayerDataPath())
267
268 def __loadFlashCookiesFromPath(self, path):
269 """
270 Private slot to load the Flash cookies from a path.
271
272 @param path Flash cookies path
273 @type str
274 """
275 if path.endswith("#AppContainer"):
276 # specific to IE and Windows
277 return
278
279 path = path.replace("\\", "/")
280 solDir = QDir(path)
281 entryList = solDir.entryList()
282 for entry in entryList:
283 if entry == "." or entry == "..":
284 continue
285 entryInfo = QFileInfo(path + "/" + entry)
286 if entryInfo.isDir():
287 self.__loadFlashCookiesFromPath(entryInfo.filePath())
288 else:
289 self.__insertFlashCookie(entryInfo.filePath())
290
291 def __insertFlashCookie(self, path):
292 """
293 Private method to insert a Flash cookie into the cache.
294
295 @param path Flash cookies path
296 @type str
297 """
298 solFile = QFile(path)
299 if not solFile.open(QFile.ReadOnly):
300 return
301
302 dataStr = ""
303 data = bytes(solFile.readAll())
304 if data:
305 try:
306 reader = FlashCookieReader()
307 reader.setBytes(data)
308 reader.parse()
309 dataStr = reader.toString()
310 except FlashCookieReaderError as err:
311 dataStr = err.msg
312
313 solFileInfo = QFileInfo(solFile)
314
315 cookie = FlashCookie()
316 cookie.contents = dataStr
317 cookie.name = solFileInfo.fileName()
318 cookie.path = solFileInfo.canonicalPath()
319 cookie.size = int(solFile.size())
320 cookie.lastModified = solFileInfo.lastModified()
321 cookie.origin = self.__extractOriginFrom(path)
322
323 self.__flashCookies.append(cookie)
324
325 def __extractOriginFrom(self, path):
326 """
327 Private method to extract the cookie origin given its file name.
328
329 @param path file name of the cookie file
330 @type str
331 @return cookie origin
332 @rtype str
333 """
334 origin = path
335 if path.startswith(
336 self.flashPlayerDataPath() + self.__sharedObjectDirName()):
337 origin = origin.replace(
338 self.flashPlayerDataPath() + self.__sharedObjectDirName(), "")
339 if "/" in origin:
340 origin = origin.split("/", 1)[1]
341 elif path.startswith(
342 self.flashPlayerDataPath() +
343 "/macromedia.com/support/flashplayer/sys/"):
344 origin = origin.replace(
345 self.flashPlayerDataPath() +
346 "/macromedia.com/support/flashplayer/sys/", "")
347 if origin == "settings.sol":
348 return self.tr("!default")
349 elif origin.startswith("#"):
350 origin = origin[1:]
351 else:
352 origin = ""
353
354 index = origin.find("/")
355 if index == -1:
356 return self.tr("!other")
357
358 origin = origin[:index]
359 if origin in ["localhost", "local"]:
360 origin = "!localhost"
361
362 return origin

eric ide

mercurial