9 |
9 |
10 |
10 |
11 import os |
11 import os |
12 |
12 |
13 from PyQt5.QtCore import ( |
13 from PyQt5.QtCore import ( |
14 pyqtSignal, QObject, QUrl, QUrlQuery, QFile, QByteArray, QMutex, |
14 pyqtSignal, QObject, QUrl, QUrlQuery, QFile, QByteArray, QMutex |
15 QMutexLocker |
|
16 ) |
15 ) |
17 from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInfo |
16 from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInfo |
18 |
17 |
19 from E5Gui import E5MessageBox |
18 from E5Gui import E5MessageBox |
|
19 |
|
20 from E5Utilities.E5MutexLocker import E5MutexLocker |
20 |
21 |
21 from .AdBlockSubscription import AdBlockSubscription |
22 from .AdBlockSubscription import AdBlockSubscription |
22 from .AdBlockUrlInterceptor import AdBlockUrlInterceptor |
23 from .AdBlockUrlInterceptor import AdBlockUrlInterceptor |
23 from .AdBlockMatcher import AdBlockMatcher |
24 from .AdBlockMatcher import AdBlockMatcher |
24 |
25 |
146 @param info request info object |
147 @param info request info object |
147 @type QWebEngineUrlRequestInfo |
148 @type QWebEngineUrlRequestInfo |
148 @return flag indicating to block the request |
149 @return flag indicating to block the request |
149 @rtype bool |
150 @rtype bool |
150 """ |
151 """ |
151 locker = QMutexLocker(self.__mutex) # __IGNORE_WARNING__ |
152 with E5MutexLocker(self.__mutex): |
152 |
153 if not self.isEnabled(): |
153 if not self.isEnabled(): |
154 return False |
154 return False |
155 |
155 |
156 urlString = bytes(info.requestUrl().toEncoded()).decode().lower() |
156 urlString = bytes(info.requestUrl().toEncoded()).decode().lower() |
157 urlDomain = info.requestUrl().host().lower() |
157 urlDomain = info.requestUrl().host().lower() |
158 urlScheme = info.requestUrl().scheme().lower() |
158 urlScheme = info.requestUrl().scheme().lower() |
159 |
159 |
|
160 if ( |
|
161 not self.canRunOnScheme(urlScheme) or |
|
162 not self.__canBeBlocked(info.firstPartyUrl()) |
|
163 ): |
|
164 return False |
|
165 |
|
166 res = False |
|
167 blockedRule = self.__matcher.match(info, urlDomain, urlString) |
|
168 |
|
169 if blockedRule: |
|
170 res = True |
|
171 if ( |
160 if ( |
172 info.resourceType() == |
161 not self.canRunOnScheme(urlScheme) or |
173 QWebEngineUrlRequestInfo.ResourceTypeMainFrame |
162 not self.__canBeBlocked(info.firstPartyUrl()) |
174 ): |
163 ): |
175 url = QUrl("eric:adblock") |
164 return False |
176 query = QUrlQuery() |
165 |
177 query.addQueryItem("rule", blockedRule.filter()) |
166 res = False |
178 query.addQueryItem( |
167 blockedRule = self.__matcher.match(info, urlDomain, urlString) |
179 "subscription", blockedRule.subscription().title()) |
168 |
180 url.setQuery(query) |
169 if blockedRule: |
181 info.redirect(url) |
170 res = True |
182 else: |
171 if ( |
183 info.block(True) |
172 info.resourceType() == |
184 |
173 QWebEngineUrlRequestInfo.ResourceTypeMainFrame |
185 return res |
174 ): |
|
175 url = QUrl("eric:adblock") |
|
176 query = QUrlQuery() |
|
177 query.addQueryItem("rule", blockedRule.filter()) |
|
178 query.addQueryItem( |
|
179 "subscription", blockedRule.subscription().title()) |
|
180 url.setQuery(query) |
|
181 info.redirect(url) |
|
182 else: |
|
183 info.block(True) |
|
184 |
|
185 return res |
186 |
186 |
187 def canRunOnScheme(self, scheme): |
187 def canRunOnScheme(self, scheme): |
188 """ |
188 """ |
189 Public method to check, if AdBlock can be performed on the scheme. |
189 Public method to check, if AdBlock can be performed on the scheme. |
190 |
190 |