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