eric6/Helpviewer/AdBlock/AdBlockNetwork.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 7226
babe80d84a3e
parent 7285
1ff497f33f31
child 7287
1c17f2191bdd
equal deleted inserted replaced
7226:babe80d84a3e 7286:7eb04391adf7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the network block class.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import QObject, QUrl
13 from PyQt5.QtNetwork import QNetworkRequest
14
15 from .AdBlockBlockedNetworkReply import AdBlockBlockedNetworkReply
16
17
18 class AdBlockNetwork(QObject):
19 """
20 Class implementing a network block.
21 """
22 def block(self, request):
23 """
24 Public method to check for a network block.
25
26 @param request reference to the request object (QNetworkRequest)
27 @return reply object (QNetworkReply) or None
28 """
29 url = request.url()
30 urlString = bytes(url.toEncoded()).decode()
31 urlDomain = url.host()
32 urlScheme = url.scheme()
33 refererHost = QUrl.fromEncoded(request.rawHeader(b"Referer")).host()
34
35 import Helpviewer.HelpWindow
36 manager = Helpviewer.HelpWindow.HelpWindow.adBlockManager()
37 if not manager.isEnabled() or \
38 not self.canRunOnScheme(urlScheme) or \
39 manager.isHostExcepted(urlDomain) or \
40 manager.isHostExcepted(refererHost):
41 return None
42
43 for subscription in manager.subscriptions():
44 if subscription.isEnabled():
45 blockedRule = subscription.match(request, urlDomain, urlString)
46 if blockedRule:
47 webPage = request.attribute(QNetworkRequest.User + 100)
48 if webPage is not None:
49 if not self.__canBeBlocked(webPage.url()):
50 return None
51
52 webPage.addAdBlockRule(blockedRule, url)
53
54 reply = AdBlockBlockedNetworkReply(
55 request, subscription, blockedRule, self)
56 return reply
57
58 return None
59
60 def canRunOnScheme(self, scheme):
61 """
62 Public method to check, if AdBlock can be performed on the scheme.
63
64 @param scheme scheme to check (string)
65 @return flag indicating, that AdBlock can be performed (boolean)
66 """
67 return scheme not in ["data", "eric", "qthelp", "qrc", "file", "abp"]
68
69 def __canBeBlocked(self, url):
70 """
71 Private method to check, if an URL can be blocked.
72
73 @param url URL to be checked (QUrl)
74 @return flag indicating, that the URL can be blocked (boolean)
75 """
76 import Helpviewer.HelpWindow
77 manager = Helpviewer.HelpWindow.HelpWindow.adBlockManager()
78 if manager.isHostExcepted(url.host()):
79 return False
80 for subscription in manager.subscriptions():
81 if subscription.isEnabled() and \
82 subscription.adBlockDisabledForUrl(url):
83 return False
84
85 return True

eric ide

mercurial