Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 var FilterNotifier = require("filterNotifier").FilterNotifier; | 18 /** @module requestBlocker */ |
19 var RegExpFilter = require("filterClasses").RegExpFilter; | |
20 var platform = require("info").platform; | |
21 var devtools = require("devtools"); | |
22 | 19 |
23 ext.webRequest.getIndistinguishableTypes().forEach(function(types) | 20 "use strict"; |
21 | |
22 let {RegExpFilter, BlockingFilter} = require("filterClasses"); | |
23 let {defaultMatcher} = require("matcher"); | |
24 let {FilterNotifier} = require("filterNotifier"); | |
25 let {Prefs} = require("prefs"); | |
26 let {checkWhitelisted, getKey} = require("whitelisting"); | |
27 let {stringifyURL, extractHostFromFrame, isThirdParty} = require("url"); | |
28 let {port} = require("messaging"); | |
29 let devtools = require("devtools"); | |
30 | |
31 ext.webRequest.getIndistinguishableTypes().forEach(types => | |
24 { | 32 { |
25 for (var i = 1; i < types.length; i++) | 33 for (let i = 1; i < types.length; i++) |
26 RegExpFilter.typeMap[types[i]] = RegExpFilter.typeMap[types[0]]; | 34 RegExpFilter.typeMap[types[i]] = RegExpFilter.typeMap[types[0]]; |
27 }); | 35 }); |
28 | 36 |
29 FilterNotifier.addListener(function(action, arg) | |
30 { | |
31 switch (action) | |
32 { | |
33 case "filter.added": | |
34 case "filter.removed": | |
35 case "filter.disabled": | |
36 // Only request blocking/whitelisting filters have | |
37 // an effect on the webRequest handler behavior. | |
38 if (!(arg instanceof RegExpFilter)) | |
39 break; | |
40 case "subscription.added": | |
41 case "subscription.removed": | |
42 case "subscription.disabled": | |
43 case "subscription.updated": | |
44 case "load": | |
45 ext.webRequest.handlerBehaviorChanged(); | |
46 break; | |
47 } | |
48 }); | |
49 | |
50 function onBeforeRequestAsync(page, url, type, docDomain, | 37 function onBeforeRequestAsync(page, url, type, docDomain, |
51 thirdParty, key, specificOnly, | 38 thirdParty, key, specificOnly, |
52 filter) | 39 filter) |
53 { | 40 { |
54 if (filter) | 41 if (filter) |
55 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, page); | 42 FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, page); |
56 | 43 |
57 if (devtools) | 44 if (devtools) |
58 devtools.logRequest( | 45 devtools.logRequest( |
59 page, url, type, docDomain, | 46 page, url, type, docDomain, |
60 thirdParty, key, specificOnly, | 47 thirdParty, key, specificOnly, |
61 filter | 48 filter |
62 ); | 49 ); |
63 } | 50 } |
64 | 51 |
65 function onBeforeRequest(url, type, page, frame) | 52 ext.webRequest.onBeforeRequest.addListener((url, type, page, frame) => |
66 { | 53 { |
67 if (checkWhitelisted(page, frame)) | 54 if (checkWhitelisted(page, frame)) |
68 return true; | 55 return true; |
69 | 56 |
70 var urlString = stringifyURL(url); | 57 let urlString = stringifyURL(url); |
71 var docDomain = extractHostFromFrame(frame); | 58 let docDomain = extractHostFromFrame(frame); |
72 var thirdParty = isThirdParty(url, docDomain); | 59 let thirdParty = isThirdParty(url, docDomain); |
73 var key = getKey(page, frame); | 60 let key = getKey(page, frame); |
74 | 61 |
75 var specificOnly = !!checkWhitelisted( | 62 let specificOnly = !!checkWhitelisted( |
76 page, frame, RegExpFilter.typeMap.GENERICBLOCK | 63 page, frame, RegExpFilter.typeMap.GENERICBLOCK |
77 ); | 64 ); |
78 | 65 |
79 var filter = defaultMatcher.matchesAny( | 66 let filter = defaultMatcher.matchesAny( |
80 urlString, RegExpFilter.typeMap[type], | 67 urlString, RegExpFilter.typeMap[type], |
81 docDomain, thirdParty, key, specificOnly | 68 docDomain, thirdParty, key, specificOnly |
82 ); | 69 ); |
83 | 70 |
84 setTimeout(onBeforeRequestAsync, 0, page, urlString, | 71 setTimeout(onBeforeRequestAsync, 0, page, urlString, |
85 type, docDomain, | 72 type, docDomain, |
86 thirdParty, key, | 73 thirdParty, key, |
87 specificOnly, filter); | 74 specificOnly, filter); |
88 | 75 |
89 return !(filter instanceof BlockingFilter); | 76 return !(filter instanceof BlockingFilter); |
90 } | 77 }); |
91 | 78 |
92 ext.webRequest.onBeforeRequest.addListener(onBeforeRequest); | 79 port.on("filters.collapse", (message, sender) => |
80 { | |
81 if (checkWhitelisted(sender.page, sender.frame)) | |
82 return false; | |
93 | 83 |
94 if (platform == "chromium") | 84 let typeMask = RegExpFilter.typeMap[message.mediatype]; |
85 let documentHost = extractHostFromFrame(sender.frame); | |
86 let sitekey = getKey(sender.page, sender.frame); | |
kzar
2016/03/23 15:36:00
Nit: the name `sitekey` seems inconsistent with th
Sebastian Noack
2016/03/23 16:44:48
Well, it was inconsistent before. But fine with me
| |
87 let blocked = false; | |
88 | |
89 let specificOnly = checkWhitelisted( | |
90 sender.page, sender.frame, | |
91 RegExpFilter.typeMap.GENERICBLOCK | |
92 ); | |
93 | |
94 for (let url of message.urls) | |
95 { | |
96 let urlObj = new URL(url, message.baseURL); | |
97 let filter = defaultMatcher.matchesAny( | |
98 stringifyURL(urlObj), | |
99 typeMask, documentHost, | |
100 isThirdParty(urlObj, documentHost), | |
101 sitekey, specificOnly | |
102 ); | |
103 | |
104 if (!(filter instanceof BlockingFilter)) | |
kzar
2016/03/23 15:35:59
I think it would be clearer to check if the filter
Sebastian Noack
2016/03/23 16:44:47
I'm not sure whether I agree, that this is any eas
| |
105 continue; | |
106 if (filter.collapse != null) | |
107 return filter.collapse; | |
108 | |
109 blocked = true; | |
110 } | |
111 | |
112 return blocked && Prefs.hidePlaceholders; | |
113 }); | |
114 | |
115 let ignoreFilterNotifications = false; | |
116 FilterNotifier.addListener((action, arg) => | |
Sebastian Noack
2016/03/23 14:02:06
I hope you don't mind that I revisited the logic t
kzar
2016/03/23 15:35:59
Acknowledged.
| |
95 { | 117 { |
96 function onHeadersReceived(details) | 118 // Handler behavior is already going to be changed. We do so asynchronously, t o |
kzar
2016/03/23 15:35:59
Nit: Mind wrapping this long line?
kzar
2016/03/23 15:35:59
I don't really understand this comment. Maybe some
Sebastian Noack
2016/03/23 16:44:47
Done.
| |
119 // not trigger multiple times if multiple filter changes occur simultaneously. | |
120 if (ignoreFilterNotifications) | |
121 return; | |
122 | |
123 if (action != "load") | |
97 { | 124 { |
98 var page = new ext.Page({id: details.tabId}); | 125 let parts = action.split("."); |
99 var frame = ext.getFrame(details.tabId, details.frameId); | 126 let [category, event] = parts; |
127 if (category == "subscription") | |
128 { | |
129 if (event != "added" && | |
130 event != "removed" && | |
131 event != "updated" && | |
132 event != "disabled") | |
133 return; | |
100 | 134 |
101 if (!frame || frame.url.href != details.url) | 135 // Ignore empty subscriptions. This includes subscriptions |
136 // that have just been added, but not downloaded yet. | |
137 if (arg.filters.length == 0) | |
138 return; | |
139 } | |
140 else if (category == "filter") | |
141 { | |
142 if (event != "added" && | |
143 event != "removed" && | |
144 event != "disabled") | |
145 return; | |
146 | |
147 // Ignore all types of filters but request filters, | |
148 // only these have an effect on the handler behavior. | |
149 if (!(arg instanceof RegExpFilter)) | |
150 return; | |
151 } | |
152 else | |
102 return; | 153 return; |
103 | 154 |
104 for (var i = 0; i < details.responseHeaders.length; i++) | 155 // Ignore disabled subscriptions and filters, unless they just got |
105 { | 156 // disabled, otherwise they have no effect on the handler behavior. |
106 var header = details.responseHeaders[i]; | 157 if (arg.disabled && event != "disabled") |
107 if (header.name.toLowerCase() == "x-adblock-key" && header.value) | 158 return; |
108 processKey(header.value, page, frame); | |
109 } | |
110 } | 159 } |
111 | 160 |
112 chrome.webRequest.onHeadersReceived.addListener( | 161 ignoreFilterNotifications = true; |
113 onHeadersReceived, | 162 setTimeout(() => |
114 { | 163 { |
115 urls: ["http://*/*", "https://*/*"], | 164 ignoreFilterNotifications = false; |
116 types: ["main_frame", "sub_frame"] | 165 ext.webRequest.handlerBehaviorChanged(); |
117 }, | 166 FilterNotifier.triggerListeners("filter.behaviorChanged"); |
118 ["responseHeaders"] | 167 }); |
119 ); | 168 }); |
120 } | |
OLD | NEW |