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 Created July 13, 2015, 2:03 p.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 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
32 var BlockingFilter = filterClasses.BlockingFilter; 31 var BlockingFilter = filterClasses.BlockingFilter;
33 var Synchronizer = require("synchronizer").Synchronizer; 32 var Synchronizer = require("synchronizer").Synchronizer;
34 var parseFilters = require("filterValidation").parseFilters; 33 var filterValidation = require("filterValidation");
35 var parseFilter = require("filterValidation").parseFilter;
36 34
37 var subscriptionClasses = require("subscriptionClasses"); 35 var subscriptionClasses = require("subscriptionClasses");
38 var Subscription = subscriptionClasses.Subscription; 36 var Subscription = subscriptionClasses.Subscription;
39 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; 37 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription;
40 var SpecialSubscription = subscriptionClasses.SpecialSubscription; 38 var SpecialSubscription = subscriptionClasses.SpecialSubscription;
41 39
42 function convertObject(keys, obj) 40 function convertObject(keys, obj)
43 { 41 {
44 var result = {}; 42 var result = {};
45 for (var i = 0; i < keys.length; i++) 43 for (var i = 0; i < keys.length; i++)
46 result[keys[i]] = obj[keys[i]]; 44 result[keys[i]] = obj[keys[i]];
47 return result; 45 return result;
48 } 46 }
49 47
50 var convertSubscription = convertObject.bind(null, ["disabled", 48 var convertSubscription = convertObject.bind(null, ["disabled",
51 "downloadStatus", "homepage", "lastSuccess", "title", "url"]); 49 "downloadStatus", "homepage", "lastSuccess", "title", "url"]);
52 var convertFilterParsingError = convertObject.bind(null, ["type",
53 "reason", "selector"]);
54 var convertFilter = convertObject.bind(null, ["text"]); 50 var convertFilter = convertObject.bind(null, ["text"]);
55 51
56 var changeListeners = null; 52 var changeListeners = null;
57 var messageTypes = { 53 var messageTypes = {
58 "app": "app.listen", 54 "app": "app.listen",
59 "filter": "filters.listen", 55 "filter": "filters.listen",
60 "subscription": "subscriptions.listen" 56 "subscription": "subscriptions.listen"
61 }; 57 };
62 58
63 function sendMessage(page, type, action, args) 59 function sendMessage(type, action, args, page)
64 { 60 {
65 page.sendMessage({ 61 var pages = page ? [page] : changeListeners.keys();
66 type: type, 62 for (var i = 0; i < pages.length; i++)
67 action: action, 63 {
68 args: args 64 var filters = changeListeners.get(pages[i]);
69 }); 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 }
70 } 74 }
71 75
72 function onFilterChange(action) 76 function onFilterChange(action)
73 { 77 {
74 if (action == "load") 78 if (action == "load")
75 action = "filter.loaded"; 79 action = "filter.loaded";
76 80
77 var parts = action.split(".", 2); 81 var parts = action.split(".", 2);
78 var type; 82 var type;
79 if (parts.length == 1) 83 if (parts.length == 1)
(...skipping 12 matching lines...) Expand all
92 96
93 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) 97 var args = Array.prototype.slice.call(arguments, 1).map(function(arg)
94 { 98 {
95 if (arg instanceof Subscription) 99 if (arg instanceof Subscription)
96 return convertSubscription(arg); 100 return convertSubscription(arg);
97 else if (arg instanceof Filter) 101 else if (arg instanceof Filter)
98 return convertFilter(arg); 102 return convertFilter(arg);
99 else 103 else
100 return arg; 104 return arg;
101 }); 105 });
102 106 sendMessage(type, action, args);
103 var pages = changeListeners.keys();
104 for (var i = 0; i < pages.length; i++)
105 {
106 var filters = changeListeners.get(pages[i]);
107 if (filters[type] && filters[type].indexOf(action) >= 0)
108 sendMessage(pages[i], messageTypes[type], action, args);
109 }
110 };
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 } 107 }
127 108
128 global.ext.onMessage.addListener(function(message, sender, callback) 109 global.ext.onMessage.addListener(function(message, sender, callback)
129 { 110 {
130 var listenerFilters = null; 111 var listenerFilters = null;
131 switch (message.type) 112 switch (message.type)
132 { 113 {
133 case "app.listen": 114 case "app.listen":
134 case "filters.listen": 115 case "filters.listen":
135 case "subscriptions.listen": 116 case "subscriptions.listen":
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 listenerFilters.app = message.filter; 176 listenerFilters.app = message.filter;
196 else 177 else
197 delete listenerFilters.app; 178 delete listenerFilters.app;
198 break; 179 break;
199 case "app.open": 180 case "app.open":
200 if (message.what == "options") 181 if (message.what == "options")
201 ext.showOptions(); 182 ext.showOptions();
202 break; 183 break;
203 case "filters.add": 184 case "filters.add":
204 var filter = Filter.fromText(message.text); 185 var filter = Filter.fromText(message.text);
205 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);
206 break; 191 break;
207 case "filters.blocked": 192 case "filters.blocked":
208 var filter = defaultMatcher.matchesAny(message.url, message.requestType, 193 var filter = defaultMatcher.matchesAny(message.url, message.requestType,
209 message.docDomain, message.thirdParty); 194 message.docDomain, message.thirdParty);
210 callback(filter instanceof BlockingFilter); 195 callback(filter instanceof BlockingFilter);
211 break; 196 break;
212 case "filters.get": 197 case "filters.get":
213 var subscription = Subscription.fromURL(message.subscriptionUrl); 198 var subscription = Subscription.fromURL(message.subscriptionUrl);
214 if (!subscription) 199 if (!subscription)
215 { 200 {
216 callback([]); 201 callback([]);
217 break; 202 break;
218 } 203 }
219 204
220 callback(subscription.filters.map(convertFilter)); 205 callback(subscription.filters.map(convertFilter));
221 break; 206 break;
222 case "filters.importRaw": 207 case "filters.importRaw":
223 var result = parseFilters(message.text); 208 var result = filterValidation.parseFilters(message.text);
224 var errors = result.errors.filter(function(error) 209 var errors = [];
225 { 210 for (var i = 0; i < result.errors.length; i++)
226 return error.type != "unexpected-filter-list-header"; 211 {
227 }); 212 var error = result.errors[i];
213 if (error.type != "unexpected-filter-list-header")
214 errors.push(error.toString());
215 }
228 216
229 if (errors.length > 0) 217 if (errors.length > 0)
230 { 218 {
231 sendError(errors.join("\n"), sender.page); 219 sendMessage("app", "error", errors, sender.page);
232 return; 220 return;
233 } 221 }
234 222
235 var seenFilter = Object.create(null); 223 var seenFilter = Object.create(null);
236 for (var i = 0; i < result.filters.length; i++) 224 for (var i = 0; i < result.filters.length; i++)
237 { 225 {
238 var filter = result.filters[i]; 226 var filter = result.filters[i];
239 FilterStorage.addFilter(filter); 227 FilterStorage.addFilter(filter);
240 seenFilter[filter.text] = null; 228 seenFilter[filter.text] = null;
241 } 229 }
242 230
243 for (var i = 0; i < FilterStorage.subscriptions.length; i++) 231 for (var i = 0; i < FilterStorage.subscriptions.length; i++)
244 { 232 {
245 var subscription = FilterStorage.subscriptions[i]; 233 var subscription = FilterStorage.subscriptions[i];
246 if (!(subscription instanceof SpecialSubscription)) 234 if (!(subscription instanceof SpecialSubscription))
247 continue; 235 continue;
248 236
249 var filters = subscription.filters.slice(); 237 for (var j = subscription.filters.length - 1; j >= 0; j--)
250 for (var j = 0; j < filters.length; j++)
251 { 238 {
252 var filter = filters[j]; 239 var filter = subscription.filters[j];
253 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) 240 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
254 continue; 241 continue;
255 242
256 if (!(filter.text in seenFilter)) 243 if (!(filter.text in seenFilter))
257 FilterStorage.removeFilter(filter); 244 FilterStorage.removeFilter(filter);
258 } 245 }
259 } 246 }
260 break; 247 break;
261 case "filters.listen": 248 case "filters.listen":
262 if (message.filter) 249 if (message.filter)
263 listenerFilters.filter = message.filter; 250 listenerFilters.filter = message.filter;
264 else 251 else
265 delete listenerFilters.filter; 252 delete listenerFilters.filter;
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; 253 break;
274 case "filters.remove": 254 case "filters.remove":
275 var filter = Filter.fromText(message.text); 255 var filter = Filter.fromText(message.text);
276 var subscription = null; 256 var subscription = null;
277 if (message.subscriptionUrl) 257 if (message.subscriptionUrl)
278 subscription = Subscription.fromURL(message.subscriptionUrl); 258 subscription = Subscription.fromURL(message.subscriptionUrl);
279 259
280 if (!subscription) 260 if (!subscription)
281 FilterStorage.removeFilter(filter); 261 FilterStorage.removeFilter(filter);
282 else 262 else
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 subscription.title = message.title; 317 subscription.title = message.title;
338 subscription.homepage = message.homepage; 318 subscription.homepage = message.homepage;
339 FilterStorage.addSubscription(subscription); 319 FilterStorage.addSubscription(subscription);
340 if (!subscription.lastDownload) 320 if (!subscription.lastDownload)
341 Synchronizer.execute(subscription); 321 Synchronizer.execute(subscription);
342 } 322 }
343 break; 323 break;
344 } 324 }
345 }); 325 });
346 })(this); 326 })(this);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld