Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: messageResponder.js

Issue 29370999: Issue 4783 - Use modern JavaScript syntax for the messageResponder (Closed)
Left Patch Set: Created Jan. 11, 2017, 8:41 a.m.
Right Patch Set: Addressed further feedback Created Jan. 18, 2017, 11:51 a.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 | « no previous file | no next file » | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 (global => 18 "use strict";
Sebastian Noack 2017/01/11 16:29:08 Do we even need an IIFE with ES6? Wouldn't a simpl
kzar 2017/01/13 08:44:49 Good point, Done.
Sebastian Noack 2017/01/13 11:54:48 I just noticed, you'd also have to use strict mode
kzar 2017/01/16 04:41:49 Done.
19
19 { 20 {
20 if (!global.ext) 21 var ext = ext || require("ext_background");
21 global.ext = require("ext_background"); 22
22 23 const {port} = require("messaging");
23 let port = require("messaging").port; 24 const {Prefs} = require("prefs");
24 let Prefs = require("prefs").Prefs; 25 const {Utils} = require("utils");
25 let Utils = require("utils").Utils; 26 const {FilterStorage} = require("filterStorage");
26 let FilterStorage = require("filterStorage").FilterStorage; 27 const {FilterNotifier} = require("filterNotifier");
27 let FilterNotifier = require("filterNotifier").FilterNotifier; 28 const {defaultMatcher} = require("matcher");
28 let defaultMatcher = require("matcher").defaultMatcher; 29 const {ElemHideEmulation} = require("elemHideEmulation");
29 let ElemHideEmulation = require("elemHideEmulation").ElemHideEmulation; 30 const {Notification: NotificationStorage} = require("notification");
30 let NotificationStorage = require("notification").Notification; 31
31 32 const {Filter, BlockingFilter, RegExpFilter} = require("filterClasses");
32 let filterClasses = require("filterClasses"); 33 const {Synchronizer} = require("synchronizer");
33 let Filter = filterClasses.Filter; 34
34 let BlockingFilter = filterClasses.BlockingFilter; 35 const info = require("info");
35 let RegExpFilter = filterClasses.RegExpFilter; 36 const {Subscription,
36 let Synchronizer = require("synchronizer").Synchronizer; 37 DownloadableSubscription,
37 38 SpecialSubscription} = require("subscriptionClasses");
38 let info = require("info");
39 let subscriptionClasses = require("subscriptionClasses");
40 let Subscription = subscriptionClasses.Subscription;
41 let DownloadableSubscription = subscriptionClasses.DownloadableSubscription;
42 let SpecialSubscription = subscriptionClasses.SpecialSubscription;
43 39
44 // Some modules doesn't exist on Firefox. Moreover, 40 // Some modules doesn't exist on Firefox. Moreover,
45 // require() throws an exception on Firefox in that case. 41 // require() throws an exception on Firefox in that case.
46 // However, try/catch causes the whole function to to be 42 // However, try/catch causes the whole function to to be
47 // deoptimized on V8. So we wrap it into another function. 43 // deoptimized on V8. So we wrap it into another function.
48 function tryRequire(module) 44 function tryRequire(module)
49 { 45 {
50 try 46 try
51 { 47 {
52 return require(module); 48 return require(module);
53 } 49 }
54 catch (e) 50 catch (e)
55 { 51 {
56 return null; 52 return null;
57 } 53 }
58 } 54 }
59 55
60 function convertObject(keys, obj) 56 function convertObject(keys, obj)
61 { 57 {
62 let result = {}; 58 let result = {};
63 for (let key of keys) 59 for (let key of keys)
60 {
64 if (key in obj) 61 if (key in obj)
65 result[key] = obj[key]; 62 result[key] = obj[key];
63 }
66 return result; 64 return result;
67 } 65 }
68 66
69 function convertSubscription(subscription) 67 function convertSubscription(subscription)
70 { 68 {
71 let obj = convertObject(["disabled", "downloadStatus", "homepage", 69 let obj = convertObject(["disabled", "downloadStatus", "homepage",
72 "lastDownload", "title", "url"], subscription); 70 "lastDownload", "title", "url"], subscription);
73 obj.isDownloading = Synchronizer.isExecuting(subscription.url); 71 obj.isDownloading = Synchronizer.isExecuting(subscription.url);
74 return obj; 72 return obj;
75 } 73 }
76 74
77 let convertFilter = convertObject.bind(null, ["text"]); 75 let convertFilter = convertObject.bind(null, ["text"]);
78 76
79 let changeListeners = new global.ext.PageMap(); 77 let changeListeners = new ext.PageMap();
80 let listenedPreferences = Object.create(null); 78 let listenedPreferences = Object.create(null);
81 let listenedFilterChanges = Object.create(null); 79 let listenedFilterChanges = Object.create(null);
82 let messageTypes = { 80 let messageTypes = {
83 "app": "app.respond", 81 "app": "app.respond",
84 "filter": "filters.respond", 82 "filter": "filters.respond",
85 "pref": "prefs.respond", 83 "pref": "prefs.respond",
86 "subscription": "subscriptions.respond" 84 "subscription": "subscriptions.respond"
87 }; 85 };
88 86
89 function sendMessage(type, action) 87 function sendMessage(type, action)
(...skipping 24 matching lines...) Expand all
114 type: messageTypes[type], 112 type: messageTypes[type],
115 action: action, 113 action: action,
116 args: args 114 args: args
117 }); 115 });
118 } 116 }
119 } 117 }
120 } 118 }
121 119
122 function addFilterListeners(type, actions) 120 function addFilterListeners(type, actions)
123 { 121 {
124 actions.forEach(action => 122 for (let action of actions)
125 { 123 {
126 let name; 124 let name;
127 if (type == "filter" && action == "loaded") 125 if (type == "filter" && action == "loaded")
128 name = "load"; 126 name = "load";
129 else 127 else
130 name = type + "." + action; 128 name = type + "." + action;
131 129
132 if (!(name in listenedFilterChanges)) 130 if (!(name in listenedFilterChanges))
133 { 131 {
134 listenedFilterChanges[name] = null; 132 listenedFilterChanges[name] = null;
135 FilterNotifier.on(name, function() 133 FilterNotifier.on(name, function()
kzar 2017/01/11 08:50:44 (I originally used an arrow function here but that
136 { 134 {
137 let args = [type, action]; 135 let args = [type, action];
138 for (let arg of arguments) 136 for (let arg of arguments)
139 args.push(arg); 137 args.push(arg);
140 sendMessage.apply(null, args); 138 sendMessage.apply(null, args);
141 }); 139 });
142 } 140 }
143 }); 141 }
144 } 142 }
145 143
146 function getListenerFilters(page) 144 function getListenerFilters(page)
147 { 145 {
148 let listenerFilters = changeListeners.get(page); 146 let listenerFilters = changeListeners.get(page);
149 if (!listenerFilters) 147 if (!listenerFilters)
150 { 148 {
151 listenerFilters = Object.create(null); 149 listenerFilters = Object.create(null);
152 changeListeners.set(page, listenerFilters); 150 changeListeners.set(page, listenerFilters);
153 } 151 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 message.thirdParty); 223 message.thirdParty);
226 224
227 return filter instanceof BlockingFilter; 225 return filter instanceof BlockingFilter;
228 }); 226 });
229 227
230 port.on("filters.get", (message, sender) => 228 port.on("filters.get", (message, sender) =>
231 { 229 {
232 if (message.what == "elemhideemulation") 230 if (message.what == "elemhideemulation")
233 { 231 {
234 let filters = []; 232 let filters = [];
235 let checkWhitelisted = require("whitelisting").checkWhitelisted; 233 const {checkWhitelisted} = require("whitelisting");
236 234
237 if (Prefs.enabled && !checkWhitelisted(sender.page, sender.frame, 235 if (Prefs.enabled && !checkWhitelisted(sender.page, sender.frame,
238 RegExpFilter.typeMap.DOCUMENT | 236 RegExpFilter.typeMap.DOCUMENT |
239 RegExpFilter.typeMap.ELEMHIDE)) 237 RegExpFilter.typeMap.ELEMHIDE))
240 { 238 {
241 let hostname = sender.frame.url.hostname; 239 let hostname = sender.frame.url.hostname;
242 filters = ElemHideEmulation.getRulesForDomain(hostname); 240 filters = ElemHideEmulation.getRulesForDomain(hostname);
243 filters = filters.map(filter => 241 filters = filters.map(filter =>
244 { 242 {
245 return { 243 return {
(...skipping 10 matching lines...) Expand all
256 return []; 254 return [];
257 255
258 return subscription.filters.map(convertFilter); 256 return subscription.filters.map(convertFilter);
259 }); 257 });
260 258
261 port.on("filters.importRaw", (message, sender) => 259 port.on("filters.importRaw", (message, sender) =>
262 { 260 {
263 let result = require("filterValidation").parseFilters(message.text); 261 let result = require("filterValidation").parseFilters(message.text);
264 let errors = []; 262 let errors = [];
265 for (let error of result.errors) 263 for (let error of result.errors)
264 {
266 if (error.type != "unexpected-filter-list-header") 265 if (error.type != "unexpected-filter-list-header")
267 errors.push(error.toString()); 266 errors.push(error.toString());
267 }
268 268
269 if (errors.length > 0) 269 if (errors.length > 0)
270 return errors; 270 return errors;
271 271
272 let seenFilter = Object.create(null); 272 let seenFilter = Object.create(null);
273 for (let filter of result.filters) 273 for (let filter of result.filters)
274 { 274 {
275 FilterStorage.addFilter(filter); 275 FilterStorage.addFilter(filter);
276 seenFilter[filter.text] = null; 276 seenFilter[filter.text] = null;
277 } 277 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 }); 318 });
319 319
320 port.on("prefs.get", (message, sender) => 320 port.on("prefs.get", (message, sender) =>
321 { 321 {
322 return Prefs[message.key]; 322 return Prefs[message.key];
323 }); 323 });
324 324
325 port.on("prefs.listen", (message, sender) => 325 port.on("prefs.listen", (message, sender) =>
326 { 326 {
327 getListenerFilters(sender.page).pref = message.filter; 327 getListenerFilters(sender.page).pref = message.filter;
328 message.filter.forEach(preference => 328 for (let preference of message.filter)
329 { 329 {
330 if (!(preference in listenedPreferences)) 330 if (!(preference in listenedPreferences))
331 { 331 {
332 listenedPreferences[preference] = null; 332 listenedPreferences[preference] = null;
333 Prefs.on(preference, () => 333 Prefs.on(preference, () =>
334 { 334 {
335 sendMessage("pref", preference, Prefs[preference]); 335 sendMessage("pref", preference, Prefs[preference]);
336 }); 336 });
337 } 337 }
338 }); 338 }
339 }); 339 });
340 340
341 port.on("prefs.toggle", (message, sender) => 341 port.on("prefs.toggle", (message, sender) =>
342 { 342 {
343 if (message.key == "notifications_ignoredcategories") 343 if (message.key == "notifications_ignoredcategories")
344 NotificationStorage.toggleIgnoreCategory("*"); 344 NotificationStorage.toggleIgnoreCategory("*");
345 else 345 else
346 Prefs[message.key] = !Prefs[message.key]; 346 Prefs[message.key] = !Prefs[message.key];
347 }); 347 });
348 348
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 if (!subscription.lastDownload) 419 if (!subscription.lastDownload)
420 Synchronizer.execute(subscription); 420 Synchronizer.execute(subscription);
421 } 421 }
422 }); 422 });
423 423
424 port.on("subscriptions.update", (message, sender) => 424 port.on("subscriptions.update", (message, sender) =>
425 { 425 {
426 let subscriptions = message.url ? [Subscription.fromURL(message.url)] : 426 let subscriptions = message.url ? [Subscription.fromURL(message.url)] :
427 FilterStorage.subscriptions; 427 FilterStorage.subscriptions;
428 for (let subscription of subscriptions) 428 for (let subscription of subscriptions)
429 {
429 if (subscription instanceof DownloadableSubscription) 430 if (subscription instanceof DownloadableSubscription)
430 Synchronizer.execute(subscription, true); 431 Synchronizer.execute(subscription, true);
431 }); 432 }
432 })(this); 433 });
434 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld