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: Error handling improvements Created July 14, 2015, 9:55 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 parseFilters = require("filterValidation").parseFilters; 33 var filterValidation = require("filterValidation");
34 var parseFilter = require("filterValidation").parseFilter;
35 34
36 var subscriptionClasses = require("subscriptionClasses"); 35 var subscriptionClasses = require("subscriptionClasses");
37 var Subscription = subscriptionClasses.Subscription; 36 var Subscription = subscriptionClasses.Subscription;
38 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; 37 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription;
39 var SpecialSubscription = subscriptionClasses.SpecialSubscription; 38 var SpecialSubscription = subscriptionClasses.SpecialSubscription;
40 39
41 function convertObject(keys, obj) 40 function convertObject(keys, obj)
42 { 41 {
43 var result = {}; 42 var result = {};
44 for (var i = 0; i < keys.length; i++) 43 for (var i = 0; i < keys.length; i++)
45 result[keys[i]] = obj[keys[i]]; 44 result[keys[i]] = obj[keys[i]];
46 return result; 45 return result;
47 } 46 }
48 47
49 var convertSubscription = convertObject.bind(null, ["disabled", 48 var convertSubscription = convertObject.bind(null, ["disabled",
50 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); 49 "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"]);
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 };
61 58
62 function sendMessage(type, action, args, page) 59 function sendMessage(type, action, args, page)
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 listenerFilters.app = message.filter; 176 listenerFilters.app = message.filter;
180 else 177 else
181 delete listenerFilters.app; 178 delete listenerFilters.app;
182 break; 179 break;
183 case "app.open": 180 case "app.open":
184 if (message.what == "options") 181 if (message.what == "options")
185 ext.showOptions(); 182 ext.showOptions();
186 break; 183 break;
187 case "filters.add": 184 case "filters.add":
188 var filter = Filter.fromText(message.text); 185 var filter = Filter.fromText(message.text);
189 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);
190 break; 191 break;
191 case "filters.blocked": 192 case "filters.blocked":
192 var filter = defaultMatcher.matchesAny(message.url, message.requestType, 193 var filter = defaultMatcher.matchesAny(message.url, message.requestType,
193 message.docDomain, message.thirdParty); 194 message.docDomain, message.thirdParty);
194 callback(filter instanceof BlockingFilter); 195 callback(filter instanceof BlockingFilter);
195 break; 196 break;
196 case "filters.get": 197 case "filters.get":
197 var subscription = Subscription.fromURL(message.subscriptionUrl); 198 var subscription = Subscription.fromURL(message.subscriptionUrl);
198 if (!subscription) 199 if (!subscription)
199 { 200 {
200 callback([]); 201 callback([]);
201 break; 202 break;
202 } 203 }
203 204
204 callback(subscription.filters.map(convertFilter)); 205 callback(subscription.filters.map(convertFilter));
205 break; 206 break;
206 case "filters.importRaw": 207 case "filters.importRaw":
207 var result = parseFilters(message.text); 208 var result = filterValidation.parseFilters(message.text);
208 var errors = result.errors.filter(function(error) 209 var errors = [];
209 { 210 for (var i = 0; i < result.errors.length; i++)
210 return error.type != "unexpected-filter-list-header"; 211 {
211 }); 212 var error = result.errors[i];
213 if (error.type != "unexpected-filter-list-header")
214 errors.push(error.toString());
215 }
212 216
213 if (errors.length > 0) 217 if (errors.length > 0)
214 { 218 {
215 sendMessage("app", "error", [errors.join("\n")], sender.page); 219 sendMessage("app", "error", errors, 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; 220 return;
217 } 221 }
218 222
219 var seenFilter = Object.create(null); 223 var seenFilter = Object.create(null);
220 for (var i = 0; i < result.filters.length; i++) 224 for (var i = 0; i < result.filters.length; i++)
221 { 225 {
222 var filter = result.filters[i]; 226 var filter = result.filters[i];
223 FilterStorage.addFilter(filter); 227 FilterStorage.addFilter(filter);
224 seenFilter[filter.text] = null; 228 seenFilter[filter.text] = null;
225 } 229 }
226 230
227 for (var i = 0; i < FilterStorage.subscriptions.length; i++) 231 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
228 { 232 {
229 var subscription = FilterStorage.subscriptions[i]; 233 var subscription = FilterStorage.subscriptions[i];
230 if (!(subscription instanceof SpecialSubscription)) 234 if (!(subscription instanceof SpecialSubscription))
231 continue; 235 continue;
232 236
233 var filters = subscription.filters.slice(); 237 for (var j = subscription.filters.length - 1; j >= 0; j--)
234 for (var j = 0; j < filters.length; j++)
235 { 238 {
236 var filter = filters[j]; 239 var filter = subscription.filters[j];
237 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) 240 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
238 continue; 241 continue;
239 242
240 if (!(filter.text in seenFilter)) 243 if (!(filter.text in seenFilter))
241 FilterStorage.removeFilter(filter); 244 FilterStorage.removeFilter(filter);
242 } 245 }
243 } 246 }
244 break; 247 break;
245 case "filters.listen": 248 case "filters.listen":
246 if (message.filter) 249 if (message.filter)
247 listenerFilters.filter = message.filter; 250 listenerFilters.filter = message.filter;
248 else 251 else
249 delete listenerFilters.filter; 252 delete listenerFilters.filter;
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; 253 break;
258 case "filters.remove": 254 case "filters.remove":
259 var filter = Filter.fromText(message.text); 255 var filter = Filter.fromText(message.text);
260 var subscription = null; 256 var subscription = null;
261 if (message.subscriptionUrl) 257 if (message.subscriptionUrl)
262 subscription = Subscription.fromURL(message.subscriptionUrl); 258 subscription = Subscription.fromURL(message.subscriptionUrl);
263 259
264 if (!subscription) 260 if (!subscription)
265 FilterStorage.removeFilter(filter); 261 FilterStorage.removeFilter(filter);
266 else 262 else
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 subscription.title = message.title; 317 subscription.title = message.title;
322 subscription.homepage = message.homepage; 318 subscription.homepage = message.homepage;
323 FilterStorage.addSubscription(subscription); 319 FilterStorage.addSubscription(subscription);
324 if (!subscription.lastDownload) 320 if (!subscription.lastDownload)
325 Synchronizer.execute(subscription); 321 Synchronizer.execute(subscription);
326 } 322 }
327 break; 323 break;
328 } 324 }
329 }); 325 });
330 })(this); 326 })(this);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld