Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: messageResponder.js

Issue 29321198: Issue 2376 - Implement custom filters in new options page (Closed)
Patch Set: Addressed initial comments Created July 8, 2015, 6:13 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
31 var BlockingFilter = filterClasses.BlockingFilter; 32 var BlockingFilter = filterClasses.BlockingFilter;
32 var Synchronizer = require("synchronizer").Synchronizer; 33 var Synchronizer = require("synchronizer").Synchronizer;
34 var parseFilters = require("filterValidation").parseFilters;
35 var parseFilter = require("filterValidation").parseFilter;
33 36
34 var subscriptionClasses = require("subscriptionClasses"); 37 var subscriptionClasses = require("subscriptionClasses");
35 var Subscription = subscriptionClasses.Subscription; 38 var Subscription = subscriptionClasses.Subscription;
36 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; 39 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription;
37 var SpecialSubscription = subscriptionClasses.SpecialSubscription; 40 var SpecialSubscription = subscriptionClasses.SpecialSubscription;
38 41
39 function convertObject(keys, obj) 42 function convertObject(keys, obj)
40 { 43 {
41 var result = {}; 44 var result = {};
42 for (var i = 0; i < keys.length; i++) 45 for (var i = 0; i < keys.length; i++)
43 result[keys[i]] = obj[keys[i]]; 46 result[keys[i]] = obj[keys[i]];
44 return result; 47 return result;
45 } 48 }
46 49
47 var convertSubscription = convertObject.bind(null, ["disabled", 50 var convertSubscription = convertObject.bind(null, ["disabled",
48 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); 51 "downloadStatus", "homepage", "lastSuccess", "title", "url"]);
52 var convertFilterParsingError = convertObject.bind(null, ["type"]);
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
58 function onFilterChange(action) 62 function onFilterChange(action)
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 case "filters.get": 192 case "filters.get":
189 var subscription = Subscription.fromURL(message.subscriptionUrl); 193 var subscription = Subscription.fromURL(message.subscriptionUrl);
190 if (!subscription) 194 if (!subscription)
191 { 195 {
192 callback([]); 196 callback([]);
193 break; 197 break;
194 } 198 }
195 199
196 callback(subscription.filters.map(convertFilter)); 200 callback(subscription.filters.map(convertFilter));
197 break; 201 break;
202 case "filters.importRaw":
203 var result = parseFilters(message.text);
204 var errors = result.errors.filter(function(error)
205 {
206 return error.type != "unexpected-filter-list-header";
207 });
208
209 if (errors.length > 0)
210 {
211 alert(errors.join("\n"));
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;
213 }
214
215 var seenFilter = Object.create(null);
216 for (var i = 0; i < result.filters.length; i++)
217 {
218 var filter = result.filters[i];
219 FilterStorage.addFilter(filter);
220 seenFilter[filter.text] = null;
221 }
222
223 var remove = [];
224 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
225 {
226 var subscription = FilterStorage.subscriptions[i];
227 if (!(subscription instanceof SpecialSubscription))
228 continue;
229
230 for (var j = 0; j < subscription.filters.length; j++)
231 {
232 var filter = subscription.filters[j];
233 if (filter instanceof WhitelistFilter &&
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;
236
237 if (!(filter.text in seenFilter))
238 remove.push(filter);
239 }
240 }
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;
198 case "filters.listen": 244 case "filters.listen":
199 if (message.filter) 245 if (message.filter)
200 listenerFilters.filter = message.filter; 246 listenerFilters.filter = message.filter;
201 else 247 else
202 delete listenerFilters.filter; 248 delete listenerFilters.filter;
203 break; 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;
204 case "filters.remove": 257 case "filters.remove":
205 var filter = Filter.fromText(message.text); 258 var filter = Filter.fromText(message.text);
206 var subscription = null; 259 var subscription = null;
207 if (message.subscriptionUrl) 260 if (message.subscriptionUrl)
208 subscription = Subscription.fromURL(message.subscriptionUrl); 261 subscription = Subscription.fromURL(message.subscriptionUrl);
209 262
210 if (!subscription) 263 if (!subscription)
211 FilterStorage.removeFilter(filter); 264 FilterStorage.removeFilter(filter);
212 else 265 else
213 FilterStorage.removeFilter(filter, subscription, message.index); 266 FilterStorage.removeFilter(filter, subscription, message.index);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 subscription.title = message.title; 320 subscription.title = message.title;
268 subscription.homepage = message.homepage; 321 subscription.homepage = message.homepage;
269 FilterStorage.addSubscription(subscription); 322 FilterStorage.addSubscription(subscription);
270 if (!subscription.lastDownload) 323 if (!subscription.lastDownload)
271 Synchronizer.execute(subscription); 324 Synchronizer.execute(subscription);
272 } 325 }
273 break; 326 break;
274 } 327 }
275 }); 328 });
276 })(this); 329 })(this);
OLDNEW
« no previous file with comments | « locale/en-US/options.json ('k') | options.html » ('j') | options.html » ('J')

Powered by Google App Engine
This is Rietveld