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; | |
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"]); |
51 var convertFilterParsingError = convertObject.bind(null, ["type", | |
Thomas Greiner
2015/07/14 10:58:53
This function is no longer used.
saroyanm
2015/07/14 11:19:26
Done.
| |
52 "reason", "selector"]); | |
49 var convertFilter = convertObject.bind(null, ["text"]); | 53 var convertFilter = convertObject.bind(null, ["text"]); |
50 | 54 |
51 var changeListeners = null; | 55 var changeListeners = null; |
52 var messageTypes = { | 56 var messageTypes = { |
53 "app": "app.listen", | 57 "app": "app.listen", |
54 "filter": "filters.listen", | 58 "filter": "filters.listen", |
55 "subscription": "subscriptions.listen" | 59 "subscription": "subscriptions.listen" |
56 }; | 60 }; |
57 | 61 |
62 function sendMessage(type, action, args, page) | |
63 { | |
64 var pages = page ? [page] : changeListeners.keys(); | |
65 for (var i = 0; i < pages.length; i++) | |
66 { | |
67 var filters = changeListeners.get(pages[i]); | |
68 if (filters[type] && filters[type].indexOf(action) >= 0) | |
69 { | |
70 pages[i].sendMessage({ | |
71 type: messageTypes[type], | |
72 action: action, | |
73 args: args | |
74 }); | |
75 } | |
76 } | |
77 } | |
78 | |
58 function onFilterChange(action) | 79 function onFilterChange(action) |
59 { | 80 { |
60 if (action == "load") | 81 if (action == "load") |
61 action = "filter.loaded"; | 82 action = "filter.loaded"; |
62 | 83 |
63 var parts = action.split(".", 2); | 84 var parts = action.split(".", 2); |
64 var type; | 85 var type; |
65 if (parts.length == 1) | 86 if (parts.length == 1) |
66 { | 87 { |
67 type = "app"; | 88 type = "app"; |
(...skipping 10 matching lines...) Expand all Loading... | |
78 | 99 |
79 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) | 100 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) |
80 { | 101 { |
81 if (arg instanceof Subscription) | 102 if (arg instanceof Subscription) |
82 return convertSubscription(arg); | 103 return convertSubscription(arg); |
83 else if (arg instanceof Filter) | 104 else if (arg instanceof Filter) |
84 return convertFilter(arg); | 105 return convertFilter(arg); |
85 else | 106 else |
86 return arg; | 107 return arg; |
87 }); | 108 }); |
88 | 109 sendMessage(type, action, args); |
89 var pages = changeListeners.keys(); | 110 } |
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 | 111 |
104 global.ext.onMessage.addListener(function(message, sender, callback) | 112 global.ext.onMessage.addListener(function(message, sender, callback) |
105 { | 113 { |
106 var listenerFilters = null; | 114 var listenerFilters = null; |
107 switch (message.type) | 115 switch (message.type) |
108 { | 116 { |
109 case "app.listen": | 117 case "app.listen": |
110 case "filters.listen": | 118 case "filters.listen": |
111 case "subscriptions.listen": | 119 case "subscriptions.listen": |
112 if (!changeListeners) | 120 if (!changeListeners) |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 case "filters.get": | 196 case "filters.get": |
189 var subscription = Subscription.fromURL(message.subscriptionUrl); | 197 var subscription = Subscription.fromURL(message.subscriptionUrl); |
190 if (!subscription) | 198 if (!subscription) |
191 { | 199 { |
192 callback([]); | 200 callback([]); |
193 break; | 201 break; |
194 } | 202 } |
195 | 203 |
196 callback(subscription.filters.map(convertFilter)); | 204 callback(subscription.filters.map(convertFilter)); |
197 break; | 205 break; |
206 case "filters.importRaw": | |
207 var result = parseFilters(message.text); | |
208 var errors = result.errors.filter(function(error) | |
209 { | |
210 return error.type != "unexpected-filter-list-header"; | |
211 }); | |
212 | |
213 if (errors.length > 0) | |
214 { | |
215 sendMessage("app", "error", [errors.join("\n")], sender.page); | |
Thomas Greiner
2015/07/14 10:58:53
Just a suggestion: Since you're passing an array a
saroyanm
2015/07/14 11:19:26
Good point. Done.
Thomas Greiner
2015/07/14 11:40:08
This won't work because this would send the Filter
saroyanm
2015/07/14 13:15:47
Done.
| |
216 return; | |
217 } | |
218 | |
219 var seenFilter = Object.create(null); | |
220 for (var i = 0; i < result.filters.length; i++) | |
221 { | |
222 var filter = result.filters[i]; | |
223 FilterStorage.addFilter(filter); | |
224 seenFilter[filter.text] = null; | |
225 } | |
226 | |
227 for (var i = 0; i < FilterStorage.subscriptions.length; i++) | |
228 { | |
229 var subscription = FilterStorage.subscriptions[i]; | |
230 if (!(subscription instanceof SpecialSubscription)) | |
231 continue; | |
232 | |
233 var filters = subscription.filters.slice(); | |
234 for (var j = 0; j < filters.length; j++) | |
235 { | |
236 var filter = filters[j]; | |
237 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) | |
238 continue; | |
239 | |
240 if (!(filter.text in seenFilter)) | |
241 FilterStorage.removeFilter(filter); | |
242 } | |
243 } | |
244 break; | |
198 case "filters.listen": | 245 case "filters.listen": |
199 if (message.filter) | 246 if (message.filter) |
200 listenerFilters.filter = message.filter; | 247 listenerFilters.filter = message.filter; |
201 else | 248 else |
202 delete listenerFilters.filter; | 249 delete listenerFilters.filter; |
203 break; | 250 break; |
251 case "filters.parse": | |
252 var result = parseFilter(message.text); | |
253 if (result.error) | |
254 sendMessage("app", "error", [result.error.toString()], sender.page); | |
255 else | |
256 FilterStorage.addFilter(result.filter); | |
257 break; | |
204 case "filters.remove": | 258 case "filters.remove": |
205 var filter = Filter.fromText(message.text); | 259 var filter = Filter.fromText(message.text); |
206 var subscription = null; | 260 var subscription = null; |
207 if (message.subscriptionUrl) | 261 if (message.subscriptionUrl) |
208 subscription = Subscription.fromURL(message.subscriptionUrl); | 262 subscription = Subscription.fromURL(message.subscriptionUrl); |
209 | 263 |
210 if (!subscription) | 264 if (!subscription) |
211 FilterStorage.removeFilter(filter); | 265 FilterStorage.removeFilter(filter); |
212 else | 266 else |
213 FilterStorage.removeFilter(filter, subscription, message.index); | 267 FilterStorage.removeFilter(filter, subscription, message.index); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 subscription.title = message.title; | 321 subscription.title = message.title; |
268 subscription.homepage = message.homepage; | 322 subscription.homepage = message.homepage; |
269 FilterStorage.addSubscription(subscription); | 323 FilterStorage.addSubscription(subscription); |
270 if (!subscription.lastDownload) | 324 if (!subscription.lastDownload) |
271 Synchronizer.execute(subscription); | 325 Synchronizer.execute(subscription); |
272 } | 326 } |
273 break; | 327 break; |
274 } | 328 } |
275 }); | 329 }); |
276 })(this); | 330 })(this); |
OLD | NEW |