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-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 12 matching lines...) Expand all Loading... | |
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 BlockingFilter = filterClasses.BlockingFilter; | 31 var BlockingFilter = filterClasses.BlockingFilter; |
32 var Synchronizer = require("synchronizer").Synchronizer; | 32 var Synchronizer = require("synchronizer").Synchronizer; |
33 var parseFilters = require("filterValidation").parseFilters; | |
Sebastian Noack
2015/07/14 13:36:31
You might want to cache the module into a variable
saroyanm
2015/07/14 13:53:29
Good point, done.
| |
34 var parseFilter = require("filterValidation").parseFilter; | |
33 | 35 |
34 var subscriptionClasses = require("subscriptionClasses"); | 36 var subscriptionClasses = require("subscriptionClasses"); |
35 var Subscription = subscriptionClasses.Subscription; | 37 var Subscription = subscriptionClasses.Subscription; |
36 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; | 38 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; |
37 var SpecialSubscription = subscriptionClasses.SpecialSubscription; | 39 var SpecialSubscription = subscriptionClasses.SpecialSubscription; |
38 | 40 |
39 function convertObject(keys, obj) | 41 function convertObject(keys, obj) |
40 { | 42 { |
41 var result = {}; | 43 var result = {}; |
42 for (var i = 0; i < keys.length; i++) | 44 for (var i = 0; i < keys.length; i++) |
43 result[keys[i]] = obj[keys[i]]; | 45 result[keys[i]] = obj[keys[i]]; |
44 return result; | 46 return result; |
45 } | 47 } |
46 | 48 |
47 var convertSubscription = convertObject.bind(null, ["disabled", | 49 var convertSubscription = convertObject.bind(null, ["disabled", |
48 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); | 50 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); |
49 var convertFilter = convertObject.bind(null, ["text"]); | 51 var convertFilter = convertObject.bind(null, ["text"]); |
50 | 52 |
51 var changeListeners = null; | 53 var changeListeners = null; |
52 var messageTypes = { | 54 var messageTypes = { |
53 "app": "app.listen", | 55 "app": "app.listen", |
54 "filter": "filters.listen", | 56 "filter": "filters.listen", |
55 "subscription": "subscriptions.listen" | 57 "subscription": "subscriptions.listen" |
56 }; | 58 }; |
57 | 59 |
60 function sendMessage(type, action, args, page) | |
61 { | |
62 var pages = page ? [page] : changeListeners.keys(); | |
63 for (var i = 0; i < pages.length; i++) | |
64 { | |
65 var filters = changeListeners.get(pages[i]); | |
66 if (filters[type] && filters[type].indexOf(action) >= 0) | |
67 { | |
68 pages[i].sendMessage({ | |
69 type: messageTypes[type], | |
70 action: action, | |
71 args: args | |
72 }); | |
73 } | |
74 } | |
75 } | |
76 | |
58 function onFilterChange(action) | 77 function onFilterChange(action) |
59 { | 78 { |
60 if (action == "load") | 79 if (action == "load") |
61 action = "filter.loaded"; | 80 action = "filter.loaded"; |
62 | 81 |
63 var parts = action.split(".", 2); | 82 var parts = action.split(".", 2); |
64 var type; | 83 var type; |
65 if (parts.length == 1) | 84 if (parts.length == 1) |
66 { | 85 { |
67 type = "app"; | 86 type = "app"; |
(...skipping 10 matching lines...) Expand all Loading... | |
78 | 97 |
79 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) | 98 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) |
80 { | 99 { |
81 if (arg instanceof Subscription) | 100 if (arg instanceof Subscription) |
82 return convertSubscription(arg); | 101 return convertSubscription(arg); |
83 else if (arg instanceof Filter) | 102 else if (arg instanceof Filter) |
84 return convertFilter(arg); | 103 return convertFilter(arg); |
85 else | 104 else |
86 return arg; | 105 return arg; |
87 }); | 106 }); |
88 | 107 sendMessage(type, action, args); |
89 var pages = changeListeners.keys(); | 108 } |
90 for (var i = 0; i < pages.length; i++) | |
91 { | |
92 var filters = changeListeners.get(pages[i]); | |
93 if (filters[type] && filters[type].indexOf(action) >= 0) | |
94 { | |
95 pages[i].sendMessage({ | |
96 type: messageTypes[type], | |
97 action: action, | |
98 args: args | |
99 }); | |
100 } | |
101 } | |
102 }; | |
103 | 109 |
104 global.ext.onMessage.addListener(function(message, sender, callback) | 110 global.ext.onMessage.addListener(function(message, sender, callback) |
105 { | 111 { |
106 var listenerFilters = null; | 112 var listenerFilters = null; |
107 switch (message.type) | 113 switch (message.type) |
108 { | 114 { |
109 case "app.listen": | 115 case "app.listen": |
110 case "filters.listen": | 116 case "filters.listen": |
111 case "subscriptions.listen": | 117 case "subscriptions.listen": |
112 if (!changeListeners) | 118 if (!changeListeners) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 listenerFilters.app = message.filter; | 177 listenerFilters.app = message.filter; |
172 else | 178 else |
173 delete listenerFilters.app; | 179 delete listenerFilters.app; |
174 break; | 180 break; |
175 case "app.open": | 181 case "app.open": |
176 if (message.what == "options") | 182 if (message.what == "options") |
177 ext.showOptions(); | 183 ext.showOptions(); |
178 break; | 184 break; |
179 case "filters.add": | 185 case "filters.add": |
180 var filter = Filter.fromText(message.text); | 186 var filter = Filter.fromText(message.text); |
181 FilterStorage.addFilter(filter); | 187 var result = parseFilter(message.text); |
188 if (result.error) | |
189 sendMessage("app", "error", [result.error.toString()], sender.page); | |
190 else | |
191 FilterStorage.addFilter(result.filter); | |
182 break; | 192 break; |
183 case "filters.blocked": | 193 case "filters.blocked": |
184 var filter = defaultMatcher.matchesAny(message.url, message.requestType, | 194 var filter = defaultMatcher.matchesAny(message.url, message.requestType, |
185 message.docDomain, message.thirdParty); | 195 message.docDomain, message.thirdParty); |
186 callback(filter instanceof BlockingFilter); | 196 callback(filter instanceof BlockingFilter); |
187 break; | 197 break; |
188 case "filters.get": | 198 case "filters.get": |
189 var subscription = Subscription.fromURL(message.subscriptionUrl); | 199 var subscription = Subscription.fromURL(message.subscriptionUrl); |
190 if (!subscription) | 200 if (!subscription) |
191 { | 201 { |
192 callback([]); | 202 callback([]); |
193 break; | 203 break; |
194 } | 204 } |
195 | 205 |
196 callback(subscription.filters.map(convertFilter)); | 206 callback(subscription.filters.map(convertFilter)); |
197 break; | 207 break; |
208 case "filters.importRaw": | |
209 var result = parseFilters(message.text); | |
210 var errors = result.errors.filter(function(error) | |
Sebastian Noack
2015/07/14 13:36:31
I'd rather go with for-loop, rather than chaining
saroyanm
2015/07/14 13:53:28
Good point, done.
| |
211 { | |
212 return error.type != "unexpected-filter-list-header"; | |
213 }).map(function(error) | |
214 { | |
215 return error.toString(); | |
216 }); | |
217 | |
218 if (errors.length > 0) | |
219 { | |
220 sendMessage("app", "error", errors, sender.page); | |
221 return; | |
222 } | |
223 | |
224 var seenFilter = Object.create(null); | |
225 for (var i = 0; i < result.filters.length; i++) | |
226 { | |
227 var filter = result.filters[i]; | |
228 FilterStorage.addFilter(filter); | |
229 seenFilter[filter.text] = null; | |
230 } | |
231 | |
232 for (var i = 0; i < FilterStorage.subscriptions.length; i++) | |
233 { | |
234 var subscription = FilterStorage.subscriptions[i]; | |
235 if (!(subscription instanceof SpecialSubscription)) | |
236 continue; | |
237 | |
238 for (var j = subscription.filters.length - 1; j >= 0; j--) | |
239 { | |
240 var filter = subscription.filters[j]; | |
241 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) | |
242 continue; | |
243 | |
244 if (!(filter.text in seenFilter)) | |
245 FilterStorage.removeFilter(filter); | |
246 } | |
247 } | |
248 break; | |
198 case "filters.listen": | 249 case "filters.listen": |
199 if (message.filter) | 250 if (message.filter) |
200 listenerFilters.filter = message.filter; | 251 listenerFilters.filter = message.filter; |
201 else | 252 else |
202 delete listenerFilters.filter; | 253 delete listenerFilters.filter; |
203 break; | 254 break; |
204 case "filters.remove": | 255 case "filters.remove": |
205 var filter = Filter.fromText(message.text); | 256 var filter = Filter.fromText(message.text); |
206 var subscription = null; | 257 var subscription = null; |
207 if (message.subscriptionUrl) | 258 if (message.subscriptionUrl) |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 subscription.title = message.title; | 318 subscription.title = message.title; |
268 subscription.homepage = message.homepage; | 319 subscription.homepage = message.homepage; |
269 FilterStorage.addSubscription(subscription); | 320 FilterStorage.addSubscription(subscription); |
270 if (!subscription.lastDownload) | 321 if (!subscription.lastDownload) |
271 Synchronizer.execute(subscription); | 322 Synchronizer.execute(subscription); |
272 } | 323 } |
273 break; | 324 break; |
274 } | 325 } |
275 }); | 326 }); |
276 })(this); | 327 })(this); |
OLD | NEW |