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

Delta Between Two Patch Sets: messageResponder.js

Issue 29321198: Issue 2376 - Implement custom filters in new options page (Closed)
Left Patch Set: Created June 29, 2015, 11:21 a.m.
Right Patch Set: Nit fixes Created July 15, 2015, 2:35 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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 filterValidation = require("filterValidation");
33 34
34 var subscriptionClasses = require("subscriptionClasses"); 35 var subscriptionClasses = require("subscriptionClasses");
35 var Subscription = subscriptionClasses.Subscription; 36 var Subscription = subscriptionClasses.Subscription;
36 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; 37 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription;
37 var SpecialSubscription = subscriptionClasses.SpecialSubscription; 38 var SpecialSubscription = subscriptionClasses.SpecialSubscription;
38 39
39 function convertObject(keys, obj) 40 function convertObject(keys, obj)
40 { 41 {
41 var result = {}; 42 var result = {};
42 for (var i = 0; i < keys.length; i++) 43 for (var i = 0; i < keys.length; i++)
43 result[keys[i]] = obj[keys[i]]; 44 result[keys[i]] = obj[keys[i]];
44 return result; 45 return result;
45 } 46 }
46 47
47 var convertSubscription = convertObject.bind(null, ["disabled", 48 var convertSubscription = convertObject.bind(null, ["disabled",
48 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); 49 "downloadStatus", "homepage", "lastSuccess", "title", "url"]);
49 var convertFilter = convertObject.bind(null, ["text"]); 50 var convertFilter = convertObject.bind(null, ["text"]);
50 51
51 var changeListeners = null; 52 var changeListeners = null;
52 var messageTypes = { 53 var messageTypes = {
53 "app": "app.listen", 54 "app": "app.listen",
54 "filter": "filters.listen", 55 "filter": "filters.listen",
55 "subscription": "subscriptions.listen" 56 "subscription": "subscriptions.listen"
56 }; 57 };
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 }
75
58 function onFilterChange(action) 76 function onFilterChange(action)
59 { 77 {
60 if (action == "load") 78 if (action == "load")
61 action = "filter.loaded"; 79 action = "filter.loaded";
62 80
63 var parts = action.split(".", 2); 81 var parts = action.split(".", 2);
64 var type; 82 var type;
65 if (parts.length == 1) 83 if (parts.length == 1)
66 { 84 {
67 type = "app"; 85 type = "app";
(...skipping 10 matching lines...) Expand all
78 96
79 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) 97 var args = Array.prototype.slice.call(arguments, 1).map(function(arg)
80 { 98 {
81 if (arg instanceof Subscription) 99 if (arg instanceof Subscription)
82 return convertSubscription(arg); 100 return convertSubscription(arg);
83 else if (arg instanceof Filter) 101 else if (arg instanceof Filter)
84 return convertFilter(arg); 102 return convertFilter(arg);
85 else 103 else
86 return arg; 104 return arg;
87 }); 105 });
88 106 sendMessage(type, action, args);
89 var pages = changeListeners.keys(); 107 }
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 108
104 global.ext.onMessage.addListener(function(message, sender, callback) 109 global.ext.onMessage.addListener(function(message, sender, callback)
105 { 110 {
106 var listenerFilters = null; 111 var listenerFilters = null;
107 switch (message.type) 112 switch (message.type)
108 { 113 {
109 case "app.listen": 114 case "app.listen":
110 case "filters.listen": 115 case "filters.listen":
111 case "subscriptions.listen": 116 case "subscriptions.listen":
112 if (!changeListeners) 117 if (!changeListeners)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 listenerFilters.app = message.filter; 176 listenerFilters.app = message.filter;
172 else 177 else
173 delete listenerFilters.app; 178 delete listenerFilters.app;
174 break; 179 break;
175 case "app.open": 180 case "app.open":
176 if (message.what == "options") 181 if (message.what == "options")
177 ext.showOptions(); 182 ext.showOptions();
178 break; 183 break;
179 case "filters.add": 184 case "filters.add":
180 var filter = Filter.fromText(message.text); 185 var filter = Filter.fromText(message.text);
181 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);
182 break; 191 break;
183 case "filters.blocked": 192 case "filters.blocked":
184 var filter = defaultMatcher.matchesAny(message.url, message.requestType, 193 var filter = defaultMatcher.matchesAny(message.url, message.requestType,
185 message.docDomain, message.thirdParty); 194 message.docDomain, message.thirdParty);
186 callback(filter instanceof BlockingFilter); 195 callback(filter instanceof BlockingFilter);
187 break; 196 break;
188 case "filters.get": 197 case "filters.get":
189 var subscription = Subscription.fromURL(message.subscriptionUrl); 198 var subscription = Subscription.fromURL(message.subscriptionUrl);
190 if (!subscription) 199 if (!subscription)
191 { 200 {
192 callback([]); 201 callback([]);
193 break; 202 break;
194 } 203 }
195 204
196 callback(subscription.filters.map(convertFilter)); 205 callback(subscription.filters.map(convertFilter));
206 break;
207 case "filters.importRaw":
208 var result = filterValidation.parseFilters(message.text);
209 var errors = [];
210 for (var i = 0; i < result.errors.length; i++)
211 {
212 var error = result.errors[i];
213 if (error.type != "unexpected-filter-list-header")
214 errors.push(error.toString());
215 }
216
217 if (errors.length > 0)
218 {
219 sendMessage("app", "error", errors, sender.page);
220 return;
221 }
222
223 var seenFilter = Object.create(null);
224 for (var i = 0; i < result.filters.length; i++)
225 {
226 var filter = result.filters[i];
227 FilterStorage.addFilter(filter);
228 seenFilter[filter.text] = null;
229 }
230
231 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
232 {
233 var subscription = FilterStorage.subscriptions[i];
234 if (!(subscription instanceof SpecialSubscription))
235 continue;
236
237 for (var j = subscription.filters.length - 1; j >= 0; j--)
238 {
239 var filter = subscription.filters[j];
240 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
241 continue;
242
243 if (!(filter.text in seenFilter))
244 FilterStorage.removeFilter(filter);
245 }
246 }
197 break; 247 break;
198 case "filters.listen": 248 case "filters.listen":
199 if (message.filter) 249 if (message.filter)
200 listenerFilters.filter = message.filter; 250 listenerFilters.filter = message.filter;
201 else 251 else
202 delete listenerFilters.filter; 252 delete listenerFilters.filter;
203 break; 253 break;
204 case "filters.remove": 254 case "filters.remove":
205 var filter = Filter.fromText(message.text); 255 var filter = Filter.fromText(message.text);
206 var subscription = null; 256 var subscription = null;
207 if (message.subscriptionUrl) 257 if (message.subscriptionUrl)
208 subscription = Subscription.fromURL(message.subscriptionUrl); 258 subscription = Subscription.fromURL(message.subscriptionUrl);
209 259
210 if (!subscription) 260 if (!subscription)
211 FilterStorage.removeFilter(filter); 261 FilterStorage.removeFilter(filter);
212 else 262 else
213 FilterStorage.removeFilter(filter, subscription, message.index); 263 FilterStorage.removeFilter(filter, subscription, message.index);
214 break;
215 case "filters.importRaw":
Thomas Greiner 2015/06/30 09:23:27 It's easier to search for the right method if the
saroyanm 2015/07/08 18:25:39 Done.
216 var parseFilters = require("filterValidation").parseFilters;
217 var result = parseFilters(message.text);
218
219 var errors = result.errors.filter(function(e)
Thomas Greiner 2015/06/30 09:23:27 Please rename this variable from "e" to "error". G
saroyanm 2015/07/08 18:25:39 Done, Agree
220 {
221 return e.type != "unexpected-filter-list-header";
Thomas Greiner 2015/06/30 09:23:26 I'm aware that you didn't write this but what's th
saroyanm 2015/07/08 18:25:40 It's being fired when the the filter starts with "
Thomas Greiner 2015/07/09 11:07:54 Asked Sebastian about this and the line of reasoni
222 });
223
224 if (errors.length > 0)
225 {
226 alert(errors.join("\n"));
227 return;
228 }
229
230 var seenFilter = Object.create(null);
231 for (var i = 0; i < result.filters.length; i++)
232 {
233 var filter = result.filters[i];
234 FilterStorage.addFilter(filter);
235 seenFilter[filter.text] = null;
236 }
237
238 var remove = [];
239 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
240 {
241 var subscription = FilterStorage.subscriptions[i];
242 if (!(subscription instanceof SpecialSubscription))
243 continue;
244
245 for (var j = 0; j < subscription.filters.length; j++)
246 {
247 var filter = subscription.filters[j];
248 if (filter instanceof WhitelistFilter && /^@@\|\|([^\/:]+)\^\$docume nt$/.test(filter.text))
Thomas Greiner 2015/06/30 09:23:27 Beware of the 80 characters limit
saroyanm 2015/07/08 18:25:39 Done.
249 continue;
250
251 if (!(filter.text in seenFilter))
252 FilterStorage.removeFilter(filter);
253 }
254 }
255 break;
256 case "parse.filter":
Thomas Greiner 2015/06/30 09:23:27 This name is inconsistent with the other ones whic
saroyanm 2015/07/08 18:25:40 Done.
257 var parseFilter = require("filterValidation").parseFilter;
Thomas Greiner 2015/06/30 09:23:26 No need to require it each time it's called so ple
saroyanm 2015/07/08 18:25:40 Agree, was stupid.
258 callback(parseFilter(message.text));
Thomas Greiner 2015/06/30 09:23:26 "parseFilter" returns an instance of `Filter` (e.g
saroyanm 2015/07/08 18:25:39 Good point, done.
259 break; 264 break;
260 case "prefs.get": 265 case "prefs.get":
261 callback(Prefs[message.key]); 266 callback(Prefs[message.key]);
262 break; 267 break;
263 case "subscriptions.add": 268 case "subscriptions.add":
264 if (message.url in FilterStorage.knownSubscriptions) 269 if (message.url in FilterStorage.knownSubscriptions)
265 return; 270 return;
266 271
267 var subscription = Subscription.fromURL(message.url); 272 var subscription = Subscription.fromURL(message.url);
268 if (!subscription) 273 if (!subscription)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 subscription.title = message.title; 317 subscription.title = message.title;
313 subscription.homepage = message.homepage; 318 subscription.homepage = message.homepage;
314 FilterStorage.addSubscription(subscription); 319 FilterStorage.addSubscription(subscription);
315 if (!subscription.lastDownload) 320 if (!subscription.lastDownload)
316 Synchronizer.execute(subscription); 321 Synchronizer.execute(subscription);
317 } 322 }
318 break; 323 break;
319 } 324 }
320 }); 325 });
321 })(this); 326 })(this);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld