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: Added param to readme and some fixes Created July 15, 2015, 10:36 a.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)
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));
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 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 }
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)
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 subscription.title = message.title; 317 subscription.title = message.title;
268 subscription.homepage = message.homepage; 318 subscription.homepage = message.homepage;
269 FilterStorage.addSubscription(subscription); 319 FilterStorage.addSubscription(subscription);
270 if (!subscription.lastDownload) 320 if (!subscription.lastDownload)
271 Synchronizer.execute(subscription); 321 Synchronizer.execute(subscription);
272 } 322 }
273 break; 323 break;
274 } 324 }
275 }); 325 });
276 })(this); 326 })(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