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: removed the console.log Created July 13, 2015, 2:08 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
Thomas Greiner 2015/07/13 15:54:23 This line is no longer necessary. Same applies for
saroyanm 2015/07/14 10:20:59 Done.
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",
53 "reason", "selector"]);
49 var convertFilter = convertObject.bind(null, ["text"]); 54 var convertFilter = convertObject.bind(null, ["text"]);
50 55
51 var changeListeners = null; 56 var changeListeners = null;
52 var messageTypes = { 57 var messageTypes = {
53 "app": "app.listen", 58 "app": "app.listen",
54 "filter": "filters.listen", 59 "filter": "filters.listen",
55 "subscription": "subscriptions.listen" 60 "subscription": "subscriptions.listen"
56 }; 61 };
57 62
63 function sendMessage(page, type, action, args)
Thomas Greiner 2015/07/13 15:54:22 This function is not providing any added value. It
saroyanm 2015/07/14 10:20:58 Done.
64 {
65 page.sendMessage({
66 type: type,
67 action: action,
68 args: args
69 });
70 }
71
58 function onFilterChange(action) 72 function onFilterChange(action)
59 { 73 {
60 if (action == "load") 74 if (action == "load")
61 action = "filter.loaded"; 75 action = "filter.loaded";
62 76
63 var parts = action.split(".", 2); 77 var parts = action.split(".", 2);
64 var type; 78 var type;
65 if (parts.length == 1) 79 if (parts.length == 1)
66 { 80 {
67 type = "app"; 81 type = "app";
(...skipping 16 matching lines...) Expand all
84 return convertFilter(arg); 98 return convertFilter(arg);
85 else 99 else
86 return arg; 100 return arg;
87 }); 101 });
88 102
89 var pages = changeListeners.keys(); 103 var pages = changeListeners.keys();
90 for (var i = 0; i < pages.length; i++) 104 for (var i = 0; i < pages.length; i++)
91 { 105 {
92 var filters = changeListeners.get(pages[i]); 106 var filters = changeListeners.get(pages[i]);
93 if (filters[type] && filters[type].indexOf(action) >= 0) 107 if (filters[type] && filters[type].indexOf(action) >= 0)
94 { 108 sendMessage(pages[i], messageTypes[type], action, args);
95 pages[i].sendMessage({
96 type: messageTypes[type],
97 action: action,
98 args: args
99 });
100 }
101 } 109 }
102 }; 110 };
103 111
112 function sendError(error, page)
Thomas Greiner 2015/07/13 15:54:22 This function signature is inconsistent because yo
113 {
114 var type = "app";
115 var action = "error";
116 error = convertFilterParsingError(error);
Thomas Greiner 2015/07/13 15:54:22 Since `FilterParsingError.toString()` does not exi
117 if (!page)
118 {
119 var pages = changeListeners.keys();
120 for (var i = 0; i < pages.length; i++)
121 sendMessage(pages[i], messageTypes[type], action, error);
122 }
123 else
124 sendMessage(page, messageTypes[type], action, [error]);
125 }
126
104 global.ext.onMessage.addListener(function(message, sender, callback) 127 global.ext.onMessage.addListener(function(message, sender, callback)
105 { 128 {
106 var listenerFilters = null; 129 var listenerFilters = null;
107 switch (message.type) 130 switch (message.type)
108 { 131 {
109 case "app.listen": 132 case "app.listen":
110 case "filters.listen": 133 case "filters.listen":
111 case "subscriptions.listen": 134 case "subscriptions.listen":
112 if (!changeListeners) 135 if (!changeListeners)
113 { 136 {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 case "filters.get": 211 case "filters.get":
189 var subscription = Subscription.fromURL(message.subscriptionUrl); 212 var subscription = Subscription.fromURL(message.subscriptionUrl);
190 if (!subscription) 213 if (!subscription)
191 { 214 {
192 callback([]); 215 callback([]);
193 break; 216 break;
194 } 217 }
195 218
196 callback(subscription.filters.map(convertFilter)); 219 callback(subscription.filters.map(convertFilter));
197 break; 220 break;
221 case "filters.importRaw":
222 var result = parseFilters(message.text);
223 var errors = result.errors.filter(function(error)
224 {
225 return error.type != "unexpected-filter-list-header";
226 });
227
228 if (errors.length > 0)
229 {
230 sendError(errors.join("\n"), sender.page);
Thomas Greiner 2015/07/13 15:54:22 Note that after making the changes above you proba
saroyanm 2015/07/14 10:20:58 Done.
231 return;
232 }
233
234 var seenFilter = Object.create(null);
235 for (var i = 0; i < result.filters.length; i++)
236 {
237 var filter = result.filters[i];
238 FilterStorage.addFilter(filter);
239 seenFilter[filter.text] = null;
240 }
241
242 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
243 {
244 var subscription = FilterStorage.subscriptions[i];
245 if (!(subscription instanceof SpecialSubscription))
246 continue;
247
248 var filters = subscription.filters.slice();
249 for (var j = 0; j < filters.length; j++)
250 {
251 var filter = filters[j];
252 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
253 continue;
254
255 if (!(filter.text in seenFilter))
256 FilterStorage.removeFilter(filter);
257 }
258 }
259 break;
198 case "filters.listen": 260 case "filters.listen":
199 if (message.filter) 261 if (message.filter)
200 listenerFilters.filter = message.filter; 262 listenerFilters.filter = message.filter;
201 else 263 else
202 delete listenerFilters.filter; 264 delete listenerFilters.filter;
203 break; 265 break;
266 case "filters.parse":
267 var result = parseFilter(message.text);
268 if (result.error)
269 sendError(result.error, sender.page);
Thomas Greiner 2015/07/13 15:54:22 Note that after making the changes above you proba
saroyanm 2015/07/14 10:20:59 Done.
270 else
271 FilterStorage.addFilter(result.filter);
272 break;
204 case "filters.remove": 273 case "filters.remove":
205 var filter = Filter.fromText(message.text); 274 var filter = Filter.fromText(message.text);
206 var subscription = null; 275 var subscription = null;
207 if (message.subscriptionUrl) 276 if (message.subscriptionUrl)
208 subscription = Subscription.fromURL(message.subscriptionUrl); 277 subscription = Subscription.fromURL(message.subscriptionUrl);
209 278
210 if (!subscription) 279 if (!subscription)
211 FilterStorage.removeFilter(filter); 280 FilterStorage.removeFilter(filter);
212 else 281 else
213 FilterStorage.removeFilter(filter, subscription, message.index); 282 FilterStorage.removeFilter(filter, subscription, message.index);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 subscription.title = message.title; 336 subscription.title = message.title;
268 subscription.homepage = message.homepage; 337 subscription.homepage = message.homepage;
269 FilterStorage.addSubscription(subscription); 338 FilterStorage.addSubscription(subscription);
270 if (!subscription.lastDownload) 339 if (!subscription.lastDownload)
271 Synchronizer.execute(subscription); 340 Synchronizer.execute(subscription);
272 } 341 }
273 break; 342 break;
274 } 343 }
275 }); 344 });
276 })(this); 345 })(this);
OLDNEW

Powered by Google App Engine
This is Rietveld