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: "use strict"; Created Jan. 16, 2017, 4:33 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 "use strict"; 18 "use strict";
19 19
20 { 20 {
21 if (!ext) 21 var ext = ext || require("ext_background");
22 var ext = require("ext_background"); 22
Thomas Greiner 2017/01/17 11:03:51 Detail: It'd be great if we could avoid this if-bl
kzar 2017/01/17 11:25:29 Done.
23 23 const {port} = require("messaging");
24 let port = require("messaging").port; 24 const {Prefs} = require("prefs");
Thomas Greiner 2017/01/17 11:03:51 Is there a reason not to use `const` for imported
kzar 2017/01/17 11:25:29 Well I considered that but we generally use `let`
Sebastian Noack 2017/01/17 15:37:00 If it wouldn't be for the consistency with our exi
Thomas Greiner 2017/01/17 15:48:40 I agree that we should be careful and only use `co
Sebastian Noack 2017/01/17 17:15:48 Rather than listing specific use cases in our codi
Thomas Greiner 2017/01/17 17:20:08 Sounds good.
Sebastian Noack 2017/01/17 17:30:16 (Sorry, I first replied in the globally. Copying t
kzar 2017/01/18 05:50:32 Sure that all sound sensible enough to me. I've up
25 let Prefs = require("prefs").Prefs; 25 const {Utils} = require("utils");
26 let Utils = require("utils").Utils; 26 const {FilterStorage} = require("filterStorage");
27 let FilterStorage = require("filterStorage").FilterStorage; 27 const {FilterNotifier} = require("filterNotifier");
28 let FilterNotifier = require("filterNotifier").FilterNotifier; 28 const {defaultMatcher} = require("matcher");
29 let defaultMatcher = require("matcher").defaultMatcher; 29 const {ElemHideEmulation} = require("elemHideEmulation");
30 let ElemHideEmulation = require("elemHideEmulation").ElemHideEmulation; 30 const {Notification: NotificationStorage} = require("notification");
31 let NotificationStorage = require("notification").Notification; 31
32 32 const {Filter, BlockingFilter, RegExpFilter} = require("filterClasses");
33 let filterClasses = require("filterClasses"); 33 const {Synchronizer} = require("synchronizer");
34 let Filter = filterClasses.Filter; 34
35 let BlockingFilter = filterClasses.BlockingFilter; 35 const info = require("info");
36 let RegExpFilter = filterClasses.RegExpFilter; 36 const {Subscription,
37 let Synchronizer = require("synchronizer").Synchronizer; 37 DownloadableSubscription,
38 38 SpecialSubscription} = require("subscriptionClasses");
39 let info = require("info");
40 let subscriptionClasses = require("subscriptionClasses");
41 let Subscription = subscriptionClasses.Subscription;
42 let DownloadableSubscription = subscriptionClasses.DownloadableSubscription;
43 let SpecialSubscription = subscriptionClasses.SpecialSubscription;
44 39
45 // Some modules doesn't exist on Firefox. Moreover, 40 // Some modules doesn't exist on Firefox. Moreover,
46 // require() throws an exception on Firefox in that case. 41 // require() throws an exception on Firefox in that case.
47 // However, try/catch causes the whole function to to be 42 // However, try/catch causes the whole function to to be
48 // deoptimized on V8. So we wrap it into another function. 43 // deoptimized on V8. So we wrap it into another function.
49 function tryRequire(module) 44 function tryRequire(module)
50 { 45 {
51 try 46 try
52 { 47 {
53 return require(module); 48 return require(module);
54 } 49 }
55 catch (e) 50 catch (e)
56 { 51 {
57 return null; 52 return null;
58 } 53 }
59 } 54 }
60 55
61 function convertObject(keys, obj) 56 function convertObject(keys, obj)
62 { 57 {
63 let result = {}; 58 let result = {};
64 for (let key of keys) 59 for (let key of keys)
Thomas Greiner 2017/01/17 11:03:50 Detail: Please put braces around blocks that have
kzar 2017/01/17 11:25:29 Done.
60 {
65 if (key in obj) 61 if (key in obj)
66 result[key] = obj[key]; 62 result[key] = obj[key];
63 }
67 return result; 64 return result;
68 } 65 }
69 66
70 function convertSubscription(subscription) 67 function convertSubscription(subscription)
71 { 68 {
72 let obj = convertObject(["disabled", "downloadStatus", "homepage", 69 let obj = convertObject(["disabled", "downloadStatus", "homepage",
73 "lastDownload", "title", "url"], subscription); 70 "lastDownload", "title", "url"], subscription);
74 obj.isDownloading = Synchronizer.isExecuting(subscription.url); 71 obj.isDownloading = Synchronizer.isExecuting(subscription.url);
75 return obj; 72 return obj;
76 } 73 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 type: messageTypes[type], 112 type: messageTypes[type],
116 action: action, 113 action: action,
117 args: args 114 args: args
118 }); 115 });
119 } 116 }
120 } 117 }
121 } 118 }
122 119
123 function addFilterListeners(type, actions) 120 function addFilterListeners(type, actions)
124 { 121 {
125 actions.forEach(action => 122 for (let action of actions)
Thomas Greiner 2017/01/17 11:03:51 Since we're using `let` and for-of now, there shou
kzar 2017/01/17 11:25:29 Done.
126 { 123 {
127 let name; 124 let name;
128 if (type == "filter" && action == "loaded") 125 if (type == "filter" && action == "loaded")
129 name = "load"; 126 name = "load";
130 else 127 else
131 name = type + "." + action; 128 name = type + "." + action;
132 129
133 if (!(name in listenedFilterChanges)) 130 if (!(name in listenedFilterChanges))
134 { 131 {
135 listenedFilterChanges[name] = null; 132 listenedFilterChanges[name] = null;
136 FilterNotifier.on(name, function() 133 FilterNotifier.on(name, function()
137 { 134 {
138 let args = [type, action]; 135 let args = [type, action];
139 for (let arg of arguments) 136 for (let arg of arguments)
140 args.push(arg); 137 args.push(arg);
141 sendMessage.apply(null, args); 138 sendMessage.apply(null, args);
142 }); 139 });
143 } 140 }
144 }); 141 }
145 } 142 }
146 143
147 function getListenerFilters(page) 144 function getListenerFilters(page)
148 { 145 {
149 let listenerFilters = changeListeners.get(page); 146 let listenerFilters = changeListeners.get(page);
150 if (!listenerFilters) 147 if (!listenerFilters)
151 { 148 {
152 listenerFilters = Object.create(null); 149 listenerFilters = Object.create(null);
153 changeListeners.set(page, listenerFilters); 150 changeListeners.set(page, listenerFilters);
154 } 151 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 message.thirdParty); 223 message.thirdParty);
227 224
228 return filter instanceof BlockingFilter; 225 return filter instanceof BlockingFilter;
229 }); 226 });
230 227
231 port.on("filters.get", (message, sender) => 228 port.on("filters.get", (message, sender) =>
232 { 229 {
233 if (message.what == "elemhideemulation") 230 if (message.what == "elemhideemulation")
234 { 231 {
235 let filters = []; 232 let filters = [];
236 let checkWhitelisted = require("whitelisting").checkWhitelisted; 233 const {checkWhitelisted} = require("whitelisting");
237 234
238 if (Prefs.enabled && !checkWhitelisted(sender.page, sender.frame, 235 if (Prefs.enabled && !checkWhitelisted(sender.page, sender.frame,
239 RegExpFilter.typeMap.DOCUMENT | 236 RegExpFilter.typeMap.DOCUMENT |
240 RegExpFilter.typeMap.ELEMHIDE)) 237 RegExpFilter.typeMap.ELEMHIDE))
241 { 238 {
242 let hostname = sender.frame.url.hostname; 239 let hostname = sender.frame.url.hostname;
243 filters = ElemHideEmulation.getRulesForDomain(hostname); 240 filters = ElemHideEmulation.getRulesForDomain(hostname);
244 filters = filters.map(filter => 241 filters = filters.map(filter =>
245 { 242 {
246 return { 243 return {
(...skipping 10 matching lines...) Expand all
257 return []; 254 return [];
258 255
259 return subscription.filters.map(convertFilter); 256 return subscription.filters.map(convertFilter);
260 }); 257 });
261 258
262 port.on("filters.importRaw", (message, sender) => 259 port.on("filters.importRaw", (message, sender) =>
263 { 260 {
264 let result = require("filterValidation").parseFilters(message.text); 261 let result = require("filterValidation").parseFilters(message.text);
265 let errors = []; 262 let errors = [];
266 for (let error of result.errors) 263 for (let error of result.errors)
264 {
267 if (error.type != "unexpected-filter-list-header") 265 if (error.type != "unexpected-filter-list-header")
268 errors.push(error.toString()); 266 errors.push(error.toString());
267 }
269 268
270 if (errors.length > 0) 269 if (errors.length > 0)
271 return errors; 270 return errors;
272 271
273 let seenFilter = Object.create(null); 272 let seenFilter = Object.create(null);
274 for (let filter of result.filters) 273 for (let filter of result.filters)
275 { 274 {
276 FilterStorage.addFilter(filter); 275 FilterStorage.addFilter(filter);
277 seenFilter[filter.text] = null; 276 seenFilter[filter.text] = null;
278 } 277 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 }); 318 });
320 319
321 port.on("prefs.get", (message, sender) => 320 port.on("prefs.get", (message, sender) =>
322 { 321 {
323 return Prefs[message.key]; 322 return Prefs[message.key];
324 }); 323 });
325 324
326 port.on("prefs.listen", (message, sender) => 325 port.on("prefs.listen", (message, sender) =>
327 { 326 {
328 getListenerFilters(sender.page).pref = message.filter; 327 getListenerFilters(sender.page).pref = message.filter;
329 message.filter.forEach(preference => 328 for (let preference of message.filter)
330 { 329 {
331 if (!(preference in listenedPreferences)) 330 if (!(preference in listenedPreferences))
332 { 331 {
333 listenedPreferences[preference] = null; 332 listenedPreferences[preference] = null;
334 Prefs.on(preference, () => 333 Prefs.on(preference, () =>
335 { 334 {
336 sendMessage("pref", preference, Prefs[preference]); 335 sendMessage("pref", preference, Prefs[preference]);
337 }); 336 });
338 } 337 }
339 }); 338 }
340 }); 339 });
341 340
342 port.on("prefs.toggle", (message, sender) => 341 port.on("prefs.toggle", (message, sender) =>
343 { 342 {
344 if (message.key == "notifications_ignoredcategories") 343 if (message.key == "notifications_ignoredcategories")
345 NotificationStorage.toggleIgnoreCategory("*"); 344 NotificationStorage.toggleIgnoreCategory("*");
346 else 345 else
347 Prefs[message.key] = !Prefs[message.key]; 346 Prefs[message.key] = !Prefs[message.key];
348 }); 347 });
349 348
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 if (!subscription.lastDownload) 419 if (!subscription.lastDownload)
421 Synchronizer.execute(subscription); 420 Synchronizer.execute(subscription);
422 } 421 }
423 }); 422 });
424 423
425 port.on("subscriptions.update", (message, sender) => 424 port.on("subscriptions.update", (message, sender) =>
426 { 425 {
427 let subscriptions = message.url ? [Subscription.fromURL(message.url)] : 426 let subscriptions = message.url ? [Subscription.fromURL(message.url)] :
428 FilterStorage.subscriptions; 427 FilterStorage.subscriptions;
429 for (let subscription of subscriptions) 428 for (let subscription of subscriptions)
429 {
430 if (subscription instanceof DownloadableSubscription) 430 if (subscription instanceof DownloadableSubscription)
431 Synchronizer.execute(subscription, true); 431 Synchronizer.execute(subscription, true);
432 }
432 }); 433 });
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