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: Error handling Created July 13, 2015, 2:03 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
« no previous file with comments | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
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)
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)
113 {
114 var type = "app";
115 var action = "error";
saroyanm 2015/07/13 14:09:52 @Thomas for Error handling, is it something that u
Thomas Greiner 2015/07/13 15:54:21 The user is subscribing to it with the "app.listen
116 console.log(error);
117 error = convertFilterParsingError(error);
118 if (!page)
119 {
120 var pages = changeListeners.keys();
121 for (var i = 0; i < pages.length; i++)
122 sendMessage(pages[i], messageTypes[type], action, error);
123 }
124 else
125 sendMessage(page, messageTypes[type], action, [error]);
126 }
127
104 global.ext.onMessage.addListener(function(message, sender, callback) 128 global.ext.onMessage.addListener(function(message, sender, callback)
105 { 129 {
106 var listenerFilters = null; 130 var listenerFilters = null;
107 switch (message.type) 131 switch (message.type)
108 { 132 {
109 case "app.listen": 133 case "app.listen":
110 case "filters.listen": 134 case "filters.listen":
111 case "subscriptions.listen": 135 case "subscriptions.listen":
112 if (!changeListeners) 136 if (!changeListeners)
113 { 137 {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 case "filters.get": 212 case "filters.get":
189 var subscription = Subscription.fromURL(message.subscriptionUrl); 213 var subscription = Subscription.fromURL(message.subscriptionUrl);
190 if (!subscription) 214 if (!subscription)
191 { 215 {
192 callback([]); 216 callback([]);
193 break; 217 break;
194 } 218 }
195 219
196 callback(subscription.filters.map(convertFilter)); 220 callback(subscription.filters.map(convertFilter));
197 break; 221 break;
222 case "filters.importRaw":
223 var result = parseFilters(message.text);
224 var errors = result.errors.filter(function(error)
225 {
226 return error.type != "unexpected-filter-list-header";
227 });
228
229 if (errors.length > 0)
230 {
231 sendError(errors.join("\n"), sender.page);
232 return;
233 }
234
235 var seenFilter = Object.create(null);
236 for (var i = 0; i < result.filters.length; i++)
237 {
238 var filter = result.filters[i];
239 FilterStorage.addFilter(filter);
240 seenFilter[filter.text] = null;
241 }
242
243 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
244 {
245 var subscription = FilterStorage.subscriptions[i];
246 if (!(subscription instanceof SpecialSubscription))
247 continue;
248
249 var filters = subscription.filters.slice();
250 for (var j = 0; j < filters.length; j++)
251 {
252 var filter = filters[j];
253 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
254 continue;
255
256 if (!(filter.text in seenFilter))
257 FilterStorage.removeFilter(filter);
258 }
259 }
260 break;
198 case "filters.listen": 261 case "filters.listen":
199 if (message.filter) 262 if (message.filter)
200 listenerFilters.filter = message.filter; 263 listenerFilters.filter = message.filter;
201 else 264 else
202 delete listenerFilters.filter; 265 delete listenerFilters.filter;
203 break; 266 break;
267 case "filters.parse":
268 var result = parseFilter(message.text);
269 if (result.error)
270 sendError(result.error, sender.page);
271 else
272 FilterStorage.addFilter(result.filter);
273 break;
204 case "filters.remove": 274 case "filters.remove":
205 var filter = Filter.fromText(message.text); 275 var filter = Filter.fromText(message.text);
206 var subscription = null; 276 var subscription = null;
207 if (message.subscriptionUrl) 277 if (message.subscriptionUrl)
208 subscription = Subscription.fromURL(message.subscriptionUrl); 278 subscription = Subscription.fromURL(message.subscriptionUrl);
209 279
210 if (!subscription) 280 if (!subscription)
211 FilterStorage.removeFilter(filter); 281 FilterStorage.removeFilter(filter);
212 else 282 else
213 FilterStorage.removeFilter(filter, subscription, message.index); 283 FilterStorage.removeFilter(filter, subscription, message.index);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 subscription.title = message.title; 337 subscription.title = message.title;
268 subscription.homepage = message.homepage; 338 subscription.homepage = message.homepage;
269 FilterStorage.addSubscription(subscription); 339 FilterStorage.addSubscription(subscription);
270 if (!subscription.lastDownload) 340 if (!subscription.lastDownload)
271 Synchronizer.execute(subscription); 341 Synchronizer.execute(subscription);
272 } 342 }
273 break; 343 break;
274 } 344 }
275 }); 345 });
276 })(this); 346 })(this);
OLDNEW
« no previous file with comments | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld