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: Mockup simplification and validation update Created July 14, 2015, 6:17 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 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 && result.error.type != "empty-filter")
Thomas Greiner 2015/07/15 08:32:38 Look at the else-case. This means that empty filte
Sebastian Noack 2015/07/15 09:00:45 There is no error type "empty-filter". As I said,
saroyanm 2015/07/15 10:47:11 I think it was misunderstanding, what I understood
Sebastian Noack 2015/07/15 11:19:59 What I meant is, that you should merely leave the
188 sendMessage("app", "error", [result.error.toString()], sender.page);
189 else
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));
197 break; 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 error.type != "empty-filter")
215 errors.push(error.toString());
216 }
217
218 if (errors.length > 0)
219 {
220 sendMessage("app", "error", errors, sender.page);
221 return;
222 }
223
224 var seenFilter = Object.create(null);
225 for (var i = 0; i < result.filters.length; i++)
226 {
227 var filter = result.filters[i];
228 FilterStorage.addFilter(filter);
229 seenFilter[filter.text] = null;
230 }
231
232 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
233 {
234 var subscription = FilterStorage.subscriptions[i];
235 if (!(subscription instanceof SpecialSubscription))
236 continue;
237
238 for (var j = subscription.filters.length - 1; j >= 0; j--)
239 {
240 var filter = subscription.filters[j];
241 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
242 continue;
243
244 if (!(filter.text in seenFilter))
245 FilterStorage.removeFilter(filter);
246 }
247 }
248 break;
198 case "filters.listen": 249 case "filters.listen":
199 if (message.filter) 250 if (message.filter)
200 listenerFilters.filter = message.filter; 251 listenerFilters.filter = message.filter;
201 else 252 else
202 delete listenerFilters.filter; 253 delete listenerFilters.filter;
203 break; 254 break;
204 case "filters.remove": 255 case "filters.remove":
205 var filter = Filter.fromText(message.text); 256 var filter = Filter.fromText(message.text);
206 var subscription = null; 257 var subscription = null;
207 if (message.subscriptionUrl) 258 if (message.subscriptionUrl)
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 subscription.title = message.title; 318 subscription.title = message.title;
268 subscription.homepage = message.homepage; 319 subscription.homepage = message.homepage;
269 FilterStorage.addSubscription(subscription); 320 FilterStorage.addSubscription(subscription);
270 if (!subscription.lastDownload) 321 if (!subscription.lastDownload)
271 Synchronizer.execute(subscription); 322 Synchronizer.execute(subscription);
272 } 323 }
273 break; 324 break;
274 } 325 }
275 }); 326 });
276 })(this); 327 })(this);
OLDNEW
« background.js ('K') | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld