Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 |
(...skipping 10 matching lines...) Expand all Loading... | |
21 global.ext = require("ext_background"); | 21 global.ext = require("ext_background"); |
22 | 22 |
23 var Prefs = require("prefs").Prefs; | 23 var Prefs = require("prefs").Prefs; |
24 var Utils = require("utils").Utils; | 24 var Utils = require("utils").Utils; |
25 var FilterStorage = require("filterStorage").FilterStorage; | 25 var FilterStorage = require("filterStorage").FilterStorage; |
26 var FilterNotifier = require("filterNotifier").FilterNotifier; | 26 var FilterNotifier = require("filterNotifier").FilterNotifier; |
27 var defaultMatcher = require("matcher").defaultMatcher; | 27 var defaultMatcher = require("matcher").defaultMatcher; |
28 | 28 |
29 var filterClasses = require("filterClasses"); | 29 var filterClasses = require("filterClasses"); |
30 var Filter = filterClasses.Filter; | 30 var Filter = filterClasses.Filter; |
31 var WhitelistFilter = filterClasses.WhitelistFilter | |
32 var BlockingFilter = filterClasses.BlockingFilter; | 31 var BlockingFilter = filterClasses.BlockingFilter; |
33 var Synchronizer = require("synchronizer").Synchronizer; | 32 var Synchronizer = require("synchronizer").Synchronizer; |
34 var parseFilters = require("filterValidation").parseFilters; | 33 var filterValidation = require("filterValidation"); |
35 var parseFilter = require("filterValidation").parseFilter; | |
36 | 34 |
37 var subscriptionClasses = require("subscriptionClasses"); | 35 var subscriptionClasses = require("subscriptionClasses"); |
38 var Subscription = subscriptionClasses.Subscription; | 36 var Subscription = subscriptionClasses.Subscription; |
39 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; | 37 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; |
40 var SpecialSubscription = subscriptionClasses.SpecialSubscription; | 38 var SpecialSubscription = subscriptionClasses.SpecialSubscription; |
41 | 39 |
42 function convertObject(keys, obj) | 40 function convertObject(keys, obj) |
43 { | 41 { |
44 var result = {}; | 42 var result = {}; |
45 for (var i = 0; i < keys.length; i++) | 43 for (var i = 0; i < keys.length; i++) |
46 result[keys[i]] = obj[keys[i]]; | 44 result[keys[i]] = obj[keys[i]]; |
47 return result; | 45 return result; |
48 } | 46 } |
49 | 47 |
50 var convertSubscription = convertObject.bind(null, ["disabled", | 48 var convertSubscription = convertObject.bind(null, ["disabled", |
51 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); | 49 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); |
52 var convertFilterParsingError = convertObject.bind(null, ["type"]); | |
53 var convertFilter = convertObject.bind(null, ["text"]); | 50 var convertFilter = convertObject.bind(null, ["text"]); |
54 | 51 |
55 var changeListeners = null; | 52 var changeListeners = null; |
56 var messageTypes = { | 53 var messageTypes = { |
57 "app": "app.listen", | 54 "app": "app.listen", |
58 "filter": "filters.listen", | 55 "filter": "filters.listen", |
59 "subscription": "subscriptions.listen" | 56 "subscription": "subscriptions.listen" |
60 }; | 57 }; |
58 | |
59 function sendMessage(type, action, args, page) | |
60 { | |
61 var pages = page ? [page] : changeListeners.keys(); | |
62 for (var i = 0; i < pages.length; i++) | |
63 { | |
64 var filters = changeListeners.get(pages[i]); | |
65 if (filters[type] && filters[type].indexOf(action) >= 0) | |
66 { | |
67 pages[i].sendMessage({ | |
68 type: messageTypes[type], | |
69 action: action, | |
70 args: args | |
71 }); | |
72 } | |
73 } | |
74 } | |
61 | 75 |
62 function onFilterChange(action) | 76 function onFilterChange(action) |
63 { | 77 { |
64 if (action == "load") | 78 if (action == "load") |
65 action = "filter.loaded"; | 79 action = "filter.loaded"; |
66 | 80 |
67 var parts = action.split(".", 2); | 81 var parts = action.split(".", 2); |
68 var type; | 82 var type; |
69 if (parts.length == 1) | 83 if (parts.length == 1) |
70 { | 84 { |
(...skipping 11 matching lines...) Expand all Loading... | |
82 | 96 |
83 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) | 97 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) |
84 { | 98 { |
85 if (arg instanceof Subscription) | 99 if (arg instanceof Subscription) |
86 return convertSubscription(arg); | 100 return convertSubscription(arg); |
87 else if (arg instanceof Filter) | 101 else if (arg instanceof Filter) |
88 return convertFilter(arg); | 102 return convertFilter(arg); |
89 else | 103 else |
90 return arg; | 104 return arg; |
91 }); | 105 }); |
92 | 106 sendMessage(type, action, args); |
93 var pages = changeListeners.keys(); | 107 } |
94 for (var i = 0; i < pages.length; i++) | |
95 { | |
96 var filters = changeListeners.get(pages[i]); | |
97 if (filters[type] && filters[type].indexOf(action) >= 0) | |
98 { | |
99 pages[i].sendMessage({ | |
100 type: messageTypes[type], | |
101 action: action, | |
102 args: args | |
103 }); | |
104 } | |
105 } | |
106 }; | |
107 | 108 |
108 global.ext.onMessage.addListener(function(message, sender, callback) | 109 global.ext.onMessage.addListener(function(message, sender, callback) |
109 { | 110 { |
110 var listenerFilters = null; | 111 var listenerFilters = null; |
111 switch (message.type) | 112 switch (message.type) |
112 { | 113 { |
113 case "app.listen": | 114 case "app.listen": |
114 case "filters.listen": | 115 case "filters.listen": |
115 case "subscriptions.listen": | 116 case "subscriptions.listen": |
116 if (!changeListeners) | 117 if (!changeListeners) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 listenerFilters.app = message.filter; | 176 listenerFilters.app = message.filter; |
176 else | 177 else |
177 delete listenerFilters.app; | 178 delete listenerFilters.app; |
178 break; | 179 break; |
179 case "app.open": | 180 case "app.open": |
180 if (message.what == "options") | 181 if (message.what == "options") |
181 ext.showOptions(); | 182 ext.showOptions(); |
182 break; | 183 break; |
183 case "filters.add": | 184 case "filters.add": |
184 var filter = Filter.fromText(message.text); | 185 var filter = Filter.fromText(message.text); |
185 FilterStorage.addFilter(filter); | 186 var result = filterValidation.parseFilter(message.text); |
187 if (result.error) | |
188 sendMessage("app", "error", [result.error.toString()], sender.page); | |
189 else if (result.filter) | |
190 FilterStorage.addFilter(result.filter); | |
186 break; | 191 break; |
187 case "filters.blocked": | 192 case "filters.blocked": |
188 var filter = defaultMatcher.matchesAny(message.url, message.requestType, | 193 var filter = defaultMatcher.matchesAny(message.url, message.requestType, |
189 message.docDomain, message.thirdParty); | 194 message.docDomain, message.thirdParty); |
190 callback(filter instanceof BlockingFilter); | 195 callback(filter instanceof BlockingFilter); |
191 break; | 196 break; |
192 case "filters.get": | 197 case "filters.get": |
193 var subscription = Subscription.fromURL(message.subscriptionUrl); | 198 var subscription = Subscription.fromURL(message.subscriptionUrl); |
194 if (!subscription) | 199 if (!subscription) |
195 { | 200 { |
196 callback([]); | 201 callback([]); |
197 break; | 202 break; |
198 } | 203 } |
199 | 204 |
200 callback(subscription.filters.map(convertFilter)); | 205 callback(subscription.filters.map(convertFilter)); |
201 break; | 206 break; |
202 case "filters.importRaw": | 207 case "filters.importRaw": |
203 var result = parseFilters(message.text); | 208 var result = filterValidation.parseFilters(message.text); |
204 var errors = result.errors.filter(function(error) | 209 var errors = []; |
205 { | 210 for (var i = 0; i < result.errors.length; i++) |
206 return error.type != "unexpected-filter-list-header"; | 211 { |
207 }); | 212 var error = result.errors[i]; |
213 if (error.type != "unexpected-filter-list-header") | |
214 errors.push(error.toString()); | |
215 } | |
208 | 216 |
209 if (errors.length > 0) | 217 if (errors.length > 0) |
210 { | 218 { |
211 alert(errors.join("\n")); | 219 sendMessage("app", "error", errors, sender.page); |
Thomas Greiner
2015/07/09 11:07:55
This won't work because this code is running in th
Sebastian Noack
2015/07/10 07:31:00
What could go wrong when adding filters? Currently
Thomas Greiner
2015/07/10 12:38:52
We're talking about the same issue: the placement
saroyanm
2015/07/13 14:05:34
Done.
| |
212 return; | 220 return; |
213 } | 221 } |
214 | 222 |
215 var seenFilter = Object.create(null); | 223 var seenFilter = Object.create(null); |
216 for (var i = 0; i < result.filters.length; i++) | 224 for (var i = 0; i < result.filters.length; i++) |
217 { | 225 { |
218 var filter = result.filters[i]; | 226 var filter = result.filters[i]; |
219 FilterStorage.addFilter(filter); | 227 FilterStorage.addFilter(filter); |
220 seenFilter[filter.text] = null; | 228 seenFilter[filter.text] = null; |
221 } | 229 } |
222 | 230 |
223 var remove = []; | |
224 for (var i = 0; i < FilterStorage.subscriptions.length; i++) | 231 for (var i = 0; i < FilterStorage.subscriptions.length; i++) |
225 { | 232 { |
226 var subscription = FilterStorage.subscriptions[i]; | 233 var subscription = FilterStorage.subscriptions[i]; |
227 if (!(subscription instanceof SpecialSubscription)) | 234 if (!(subscription instanceof SpecialSubscription)) |
228 continue; | 235 continue; |
229 | 236 |
230 for (var j = 0; j < subscription.filters.length; j++) | 237 for (var j = subscription.filters.length - 1; j >= 0; j--) |
231 { | 238 { |
232 var filter = subscription.filters[j]; | 239 var filter = subscription.filters[j]; |
233 if (filter instanceof WhitelistFilter && | 240 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) |
Sebastian Noack
2015/07/09 12:12:14
Nit: Isn't |filter instanceof WhilelistFilter| red
saroyanm
2015/07/09 16:31:41
Yes this redundant, but it's the way it was implem
Sebastian Noack
2015/07/10 07:31:00
Just because the previous code is doing something,
saroyanm
2015/07/13 14:05:34
Done.
| |
234 /^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) | |
235 continue; | 241 continue; |
236 | 242 |
237 if (!(filter.text in seenFilter)) | 243 if (!(filter.text in seenFilter)) |
238 remove.push(filter); | 244 FilterStorage.removeFilter(filter); |
239 } | 245 } |
240 } | 246 } |
241 for (var i = 0; i < remove.length; i++) | |
242 FilterStorage.removeFilter(remove[i]); | |
Sebastian Noack
2015/07/09 12:12:14
Any reason to collect the filters to be removed in
saroyanm
2015/07/09 16:31:40
In that case we will have object relation issue, b
Sebastian Noack
2015/07/10 07:31:00
What one would usually do in this case, is simply
Thomas Greiner
2015/07/10 12:38:53
However, it's just as confusing as the current met
Sebastian Noack
2015/07/14 11:57:24
If you keep the performance aspect aside, I suppos
Thomas Greiner
2015/07/14 12:47:16
Remembering what you said that some people copy en
saroyanm
2015/07/14 13:15:46
Agree as well, done.
| |
243 break; | 247 break; |
244 case "filters.listen": | 248 case "filters.listen": |
245 if (message.filter) | 249 if (message.filter) |
246 listenerFilters.filter = message.filter; | 250 listenerFilters.filter = message.filter; |
247 else | 251 else |
248 delete listenerFilters.filter; | 252 delete listenerFilters.filter; |
249 break; | |
250 case "filter.parse": | |
Thomas Greiner
2015/07/09 11:07:55
Plural for consistency: "filters.parse"
saroyanm
2015/07/09 16:31:40
Done.
| |
251 var result = parseFilter(message.text); | |
252 if (result.filter) | |
Sebastian Noack
2015/07/09 12:12:14
Note that if the normalized filter text is empty r
saroyanm
2015/07/09 16:31:40
We changed the implementation here, we will need t
Sebastian Noack
2015/07/10 07:31:00
This sounds rather complicated and backwards to me
Thomas Greiner
2015/07/10 12:38:53
It would be more inline with how we return filters
Sebastian Noack
2015/07/14 11:57:24
Yes, we need to listen for subscription download e
saroyanm
2015/07/14 13:15:46
Currently we have implemented global error reporti
| |
253 callback({filter: convertFilter(result.filter)}); | |
254 else | |
255 callback({error: convertFilterParsingError(result.error)}); | |
256 break; | 253 break; |
257 case "filters.remove": | 254 case "filters.remove": |
258 var filter = Filter.fromText(message.text); | 255 var filter = Filter.fromText(message.text); |
259 var subscription = null; | 256 var subscription = null; |
260 if (message.subscriptionUrl) | 257 if (message.subscriptionUrl) |
261 subscription = Subscription.fromURL(message.subscriptionUrl); | 258 subscription = Subscription.fromURL(message.subscriptionUrl); |
262 | 259 |
263 if (!subscription) | 260 if (!subscription) |
264 FilterStorage.removeFilter(filter); | 261 FilterStorage.removeFilter(filter); |
265 else | 262 else |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 subscription.title = message.title; | 317 subscription.title = message.title; |
321 subscription.homepage = message.homepage; | 318 subscription.homepage = message.homepage; |
322 FilterStorage.addSubscription(subscription); | 319 FilterStorage.addSubscription(subscription); |
323 if (!subscription.lastDownload) | 320 if (!subscription.lastDownload) |
324 Synchronizer.execute(subscription); | 321 Synchronizer.execute(subscription); |
325 } | 322 } |
326 break; | 323 break; |
327 } | 324 } |
328 }); | 325 }); |
329 })(this); | 326 })(this); |
LEFT | RIGHT |