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

Delta Between Two Patch Sets: messageResponder.js

Issue 29338734: Issue 3839 - Adapt for Prefs event changes (Closed)
Left Patch Set: Created March 21, 2016, 4:17 p.m.
Right Patch Set: Addresed comments Created March 22, 2016, 2:22 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 | « background.js ('k') | 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
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 for (var i = 0; i < keys.length; i++) 46 for (var i = 0; i < keys.length; i++)
47 result[keys[i]] = obj[keys[i]]; 47 result[keys[i]] = obj[keys[i]];
48 return result; 48 return result;
49 } 49 }
50 50
51 var convertSubscription = convertObject.bind(null, ["disabled", 51 var convertSubscription = convertObject.bind(null, ["disabled",
52 "downloadStatus", "homepage", "lastDownload", "title", "url"]); 52 "downloadStatus", "homepage", "lastDownload", "title", "url"]);
53 var convertFilter = convertObject.bind(null, ["text"]); 53 var convertFilter = convertObject.bind(null, ["text"]);
54 54
55 var changeListeners = new global.ext.PageMap(); 55 var changeListeners = new global.ext.PageMap();
56 var filterListenerAdded = false;
57 var listenedPreferences = []; 56 var listenedPreferences = [];
58 var messageTypes = { 57 var messageTypes = {
59 "app": "app.listen", 58 "app": "app.listen",
60 "filter": "filters.listen", 59 "filter": "filters.listen",
61 "pref": "prefs.listen", 60 "pref": "prefs.listen",
62 "subscription": "subscriptions.listen" 61 "subscription": "subscriptions.listen"
63 }; 62 };
64 63
65 function sendMessage(type, action, args) 64 function sendMessage(type, action, args)
66 { 65 {
67 var pages = changeListeners.keys(); 66 var pages = changeListeners.keys();
68 for (var i = 0; i < pages.length; i++) 67 for (var i = 0; i < pages.length; i++)
69 { 68 {
70 var filters = changeListeners.get(pages[i]); 69 var filters = changeListeners.get(pages[i]);
71 var actions = filters[type]; 70 var actions = filters[type];
72 if (actions && actions.indexOf(action) != -1) 71 if (actions && actions.indexOf(action) != -1)
73 { 72 {
74 pages[i].sendMessage({ 73 pages[i].sendMessage({
75 type: messageTypes[type], 74 type: messageTypes[type],
76 action: action, 75 action: action,
77 args: args 76 args: args
78 }); 77 });
79 } 78 }
80 } 79 }
81 } 80 }
82 81
83 function onFilterChange(action) 82 function onFilterChange(action)
84 { 83 {
84 var type;
85 if (action == "load") 85 if (action == "load")
86 action = "filter.loaded"; 86 {
87 87 type = "filter";
88 var parts = action.split(".", 2); 88 action = "loaded";
89 var type;
90 if (parts.length == 1)
91 {
92 type = "app";
93 action = parts[0];
94 } 89 }
95 else 90 else
96 { 91 {
92 var parts = action.split(".");
97 type = parts[0]; 93 type = parts[0];
98 action = parts[1]; 94 action = parts[1];
99 } 95 }
100 96
101 if (!messageTypes.hasOwnProperty(type)) 97 if (!(type in messageTypes))
102 return; 98 return;
103 99
104 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) 100 var args = [];
105 { 101 for (var i = 1; i < arguments.length; i++)
102 {
103 var arg = arguments[i];
106 if (arg instanceof Subscription) 104 if (arg instanceof Subscription)
107 return convertSubscription(arg); 105 args.push(convertSubscription(arg));
108 else if (arg instanceof Filter) 106 else if (arg instanceof Filter)
109 return convertFilter(arg); 107 args.push(convertFilter(arg));
110 else 108 else
111 return arg; 109 args.push(arg);
112 }); 110 }
111
113 sendMessage(type, action, args); 112 sendMessage(type, action, args);
114 } 113 }
115 114
116 function getListenerFilters(page, addListener) 115 function getListenerFilters(page)
117 { 116 {
118 if (addListener && !filterListenerAdded)
119 {
120 FilterNotifier.addListener(onFilterChange);
121 filterListenerAdded = true;
122 }
Thomas Greiner 2016/03/21 17:55:55 I guess we could just unconditionally start listen
Sebastian Noack 2016/03/21 19:40:07 I considered it, but I rather avoid running the li
123
124 var listenerFilters = changeListeners.get(page); 117 var listenerFilters = changeListeners.get(page);
125 if (!listenerFilters) 118 if (!listenerFilters)
126 { 119 {
127 listenerFilters = Object.create(null); 120 listenerFilters = Object.create(null);
128 changeListeners.set(page, listenerFilters); 121 changeListeners.set(page, listenerFilters);
129 } 122 }
130
131 return listenerFilters; 123 return listenerFilters;
132 } 124 }
133 125
134 global.ext.onMessage.addListener(function(message, sender, callback) 126 global.ext.onMessage.addListener(function(message, sender, callback)
135 { 127 {
136 switch (message.type) 128 switch (message.type)
137 { 129 {
138 case "app.get": 130 case "app.get":
139 if (message.what == "issues") 131 if (message.what == "issues")
140 { 132 {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 devToolsPanel: info.platform == "chromium", 170 devToolsPanel: info.platform == "chromium",
179 safariContentBlocker: "safari" in global 171 safariContentBlocker: "safari" in global
180 && "extension" in global.safari 172 && "extension" in global.safari
181 && "setContentBlocker" in global.safari.extension 173 && "setContentBlocker" in global.safari.extension
182 }); 174 });
183 } 175 }
184 else 176 else
185 callback(null); 177 callback(null);
186 break; 178 break;
187 case "app.listen": 179 case "app.listen":
188 var listenerFilters = getListenerFilters(sender.page, true); 180 var listenerFilters = getListenerFilters(sender.page);
189 if (message.filter) 181 if (message.filter)
190 listenerFilters.app = message.filter; 182 listenerFilters.app = message.filter;
191 else 183 else
192 delete listenerFilters.app; 184 delete listenerFilters.app;
193 break; 185 break;
194 case "app.open": 186 case "app.open":
195 if (message.what == "options") 187 if (message.what == "options")
196 ext.showOptions(); 188 ext.showOptions();
197 break; 189 break;
198 case "filters.add": 190 case "filters.add":
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 var filter = subscription.filters[j]; 271 var filter = subscription.filters[j];
280 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) 272 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
281 continue; 273 continue;
282 274
283 if (!(filter.text in seenFilter)) 275 if (!(filter.text in seenFilter))
284 FilterStorage.removeFilter(filter); 276 FilterStorage.removeFilter(filter);
285 } 277 }
286 } 278 }
287 break; 279 break;
288 case "filters.listen": 280 case "filters.listen":
289 var listenerFilters = getListenerFilters(sender.page, true); 281 var listenerFilters = getListenerFilters(sender.page);
290 if (message.filter) 282 if (message.filter)
283 {
284 FilterNotifier.addListener(onFilterChange);
291 listenerFilters.filter = message.filter; 285 listenerFilters.filter = message.filter;
286 }
292 else 287 else
293 delete listenerFilters.filter; 288 delete listenerFilters.filter;
294 break; 289 break;
295 case "filters.remove": 290 case "filters.remove":
296 var filter = Filter.fromText(message.text); 291 var filter = Filter.fromText(message.text);
297 var subscription = null; 292 var subscription = null;
298 if (message.subscriptionUrl) 293 if (message.subscriptionUrl)
299 subscription = Subscription.fromURL(message.subscriptionUrl); 294 subscription = Subscription.fromURL(message.subscriptionUrl);
300 295
301 if (!subscription) 296 if (!subscription)
302 FilterStorage.removeFilter(filter); 297 FilterStorage.removeFilter(filter);
303 else 298 else
304 FilterStorage.removeFilter(filter, subscription, message.index); 299 FilterStorage.removeFilter(filter, subscription, message.index);
305 break; 300 break;
306 case "prefs.get": 301 case "prefs.get":
307 callback(Prefs[message.key]); 302 callback(Prefs[message.key]);
308 break; 303 break;
309 case "prefs.listen": 304 case "prefs.listen":
310 var listenerFilters = getListenerFilters(sender.page); 305 var listenerFilters = getListenerFilters(sender.page);
311 if (message.filter) 306 if (message.filter)
312 { 307 {
313 message.filter.forEach(function(preference) 308 message.filter.forEach(function(preference)
314 { 309 {
315 if (listenedPreferences.indexOf(preference) == -1) 310 if (listenedPreferences.indexOf(preference) == -1)
Thomas Greiner 2016/03/21 17:55:55 So only the first UI that registers a listener for
Sebastian Noack 2016/03/21 19:40:07 Yes, the first UI that listens for a particular pr
316 { 311 {
317 listenedPreferences.push(preference); 312 listenedPreferences.push(preference);
318 Prefs.on(preference, function() 313 Prefs.on(preference, function()
319 { 314 {
320 sendMessage("pref", preference, [Prefs[preference]]); 315 sendMessage("pref", preference, [Prefs[preference]]);
321 }); 316 });
Thomas Greiner 2016/03/21 17:55:55 This listener is never removed. As you pointed ou
Sebastian Noack 2016/03/21 19:40:07 Removing the listeners isn't trivial, since you ca
322 } 317 }
323 }); 318 });
324 listenerFilters.pref = message.filter; 319 listenerFilters.pref = message.filter;
325 } 320 }
326 else 321 else
327 delete listenerFilters.pref; 322 delete listenerFilters.pref;
328 break; 323 break;
329 case "prefs.toggle": 324 case "prefs.toggle":
330 if (message.key == "notifications_ignoredcategories") 325 if (message.key == "notifications_ignoredcategories")
331 NotificationStorage.toggleIgnoreCategory("*"); 326 NotificationStorage.toggleIgnoreCategory("*");
332 else 327 else
333 Prefs[message.key] = !Prefs[message.key]; 328 Prefs[message.key] = !Prefs[message.key];
334 break; 329 break;
335 case "subscriptions.add": 330 case "subscriptions.add":
336 var subscription = Subscription.fromURL(message.url); 331 var subscription = Subscription.fromURL(message.url);
337 if ("title" in message) 332 if ("title" in message)
338 subscription.title = message.title; 333 subscription.title = message.title;
339 if ("homepage" in message) 334 if ("homepage" in message)
340 subscription.homepage = message.homepage; 335 subscription.homepage = message.homepage;
341 336
342 if (message.confirm) 337 if (message.confirm)
343 { 338 {
344 ext.showOptions(function() 339 ext.showOptions(function()
345 { 340 {
346 onFilterChange("addSubscription", subscription); 341 sendMessage("app", "addSubscription", [convertSubscription(subscript ion)]);
347 }); 342 });
348 } 343 }
349 else 344 else
350 { 345 {
351 subscription.disabled = false; 346 subscription.disabled = false;
352 FilterStorage.addSubscription(subscription); 347 FilterStorage.addSubscription(subscription);
353 348
354 if (subscription instanceof DownloadableSubscription && !subscription. lastDownload) 349 if (subscription instanceof DownloadableSubscription && !subscription. lastDownload)
355 Synchronizer.execute(subscription); 350 Synchronizer.execute(subscription);
356 } 351 }
357 break; 352 break;
358 case "subscriptions.get": 353 case "subscriptions.get":
359 var subscriptions = FilterStorage.subscriptions.filter(function(s) 354 var subscriptions = FilterStorage.subscriptions.filter(function(s)
360 { 355 {
361 if (message.ignoreDisabled && s.disabled) 356 if (message.ignoreDisabled && s.disabled)
362 return false; 357 return false;
363 if (s instanceof DownloadableSubscription && message.downloadable) 358 if (s instanceof DownloadableSubscription && message.downloadable)
364 return true; 359 return true;
365 if (s instanceof SpecialSubscription && message.special) 360 if (s instanceof SpecialSubscription && message.special)
366 return true; 361 return true;
367 return false; 362 return false;
368 }); 363 });
369 callback(subscriptions.map(convertSubscription)); 364 callback(subscriptions.map(convertSubscription));
370 break; 365 break;
371 case "subscriptions.listen": 366 case "subscriptions.listen":
372 var listenerFilters = getListenerFilters(sender.page, true); 367 var listenerFilters = getListenerFilters(sender.page);
Thomas Greiner 2016/03/21 17:55:55 Nice. No idea why we didn't implement it like this
373 if (message.filter) 368 if (message.filter)
369 {
370 FilterNotifier.addListener(onFilterChange);
374 listenerFilters.subscription = message.filter; 371 listenerFilters.subscription = message.filter;
372 }
375 else 373 else
376 delete listenerFilters.subscription; 374 delete listenerFilters.subscription;
377 break; 375 break;
378 case "subscriptions.remove": 376 case "subscriptions.remove":
379 var subscription = Subscription.fromURL(message.url); 377 var subscription = Subscription.fromURL(message.url);
380 if (subscription.url in FilterStorage.knownSubscriptions) 378 if (subscription.url in FilterStorage.knownSubscriptions)
381 FilterStorage.removeSubscription(subscription); 379 FilterStorage.removeSubscription(subscription);
382 break; 380 break;
383 case "subscriptions.toggle": 381 case "subscriptions.toggle":
384 var subscription = Subscription.fromURL(message.url); 382 var subscription = Subscription.fromURL(message.url);
(...skipping 27 matching lines...) Expand all
412 if (subscription instanceof DownloadableSubscription) 410 if (subscription instanceof DownloadableSubscription)
413 Synchronizer.execute(subscription, true); 411 Synchronizer.execute(subscription, true);
414 } 412 }
415 break; 413 break;
416 case "subscriptions.isDownloading": 414 case "subscriptions.isDownloading":
417 callback(Synchronizer.isExecuting(message.url)); 415 callback(Synchronizer.isExecuting(message.url));
418 break; 416 break;
419 } 417 }
420 }); 418 });
421 })(this); 419 })(this);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld