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

Side by Side Diff: messageResponder.js

Issue 29338734: Issue 3839 - Adapt for Prefs event changes (Closed)
Patch Set: Created March 21, 2016, 4:17 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« background.js ('K') | « background.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 var result = {}; 45 var result = {};
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 = null; 55 var changeListeners = new global.ext.PageMap();
56 var filterListenerAdded = false;
57 var listenedPreferences = [];
56 var messageTypes = { 58 var messageTypes = {
57 "app": "app.listen", 59 "app": "app.listen",
58 "filter": "filters.listen", 60 "filter": "filters.listen",
59 "pref": "prefs.listen", 61 "pref": "prefs.listen",
60 "subscription": "subscriptions.listen" 62 "subscription": "subscriptions.listen"
61 }; 63 };
62 64
63 function sendMessage(type, action, args, page) 65 function sendMessage(type, action, args)
64 { 66 {
65 var pages = page ? [page] : changeListeners.keys(); 67 var pages = changeListeners.keys();
66 for (var i = 0; i < pages.length; i++) 68 for (var i = 0; i < pages.length; i++)
67 { 69 {
68 var filters = changeListeners.get(pages[i]); 70 var filters = changeListeners.get(pages[i]);
69 if (filters[type] && filters[type].indexOf(action) >= 0) 71 var actions = filters[type];
72 if (actions && actions.indexOf(action) != -1)
70 { 73 {
71 pages[i].sendMessage({ 74 pages[i].sendMessage({
72 type: messageTypes[type], 75 type: messageTypes[type],
73 action: action, 76 action: action,
74 args: args 77 args: args
75 }); 78 });
76 } 79 }
77 } 80 }
78 } 81 }
79 82
(...skipping 23 matching lines...) Expand all
103 if (arg instanceof Subscription) 106 if (arg instanceof Subscription)
104 return convertSubscription(arg); 107 return convertSubscription(arg);
105 else if (arg instanceof Filter) 108 else if (arg instanceof Filter)
106 return convertFilter(arg); 109 return convertFilter(arg);
107 else 110 else
108 return arg; 111 return arg;
109 }); 112 });
110 sendMessage(type, action, args); 113 sendMessage(type, action, args);
111 } 114 }
112 115
113 function onPrefChange(name) 116 function getListenerFilters(page, addListener)
114 { 117 {
115 sendMessage("pref", name, [Prefs[name]]); 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);
125 if (!listenerFilters)
126 {
127 listenerFilters = Object.create(null);
128 changeListeners.set(page, listenerFilters);
129 }
130
131 return listenerFilters;
116 } 132 }
117 133
118 global.ext.onMessage.addListener(function(message, sender, callback) 134 global.ext.onMessage.addListener(function(message, sender, callback)
119 { 135 {
120 var listenerFilters = null;
121 if (/\.listen$/.test(message.type))
122 {
123 if (!changeListeners)
124 {
125 changeListeners = new global.ext.PageMap();
126 FilterNotifier.addListener(onFilterChange);
127 Prefs.onChanged.addListener(onPrefChange);
128 }
129
130 listenerFilters = changeListeners.get(sender.page);
131 if (!listenerFilters)
132 {
133 listenerFilters = Object.create(null);
134 changeListeners.set(sender.page, listenerFilters);
135 }
136 }
137
138 switch (message.type) 136 switch (message.type)
139 { 137 {
140 case "app.get": 138 case "app.get":
141 if (message.what == "issues") 139 if (message.what == "issues")
142 { 140 {
143 var subscriptionInit; 141 var subscriptionInit;
144 try 142 try
145 { 143 {
146 subscriptionInit = require("subscriptionInit"); 144 subscriptionInit = require("subscriptionInit");
147 } 145 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 devToolsPanel: info.platform == "chromium", 178 devToolsPanel: info.platform == "chromium",
181 safariContentBlocker: "safari" in global 179 safariContentBlocker: "safari" in global
182 && "extension" in global.safari 180 && "extension" in global.safari
183 && "setContentBlocker" in global.safari.extension 181 && "setContentBlocker" in global.safari.extension
184 }); 182 });
185 } 183 }
186 else 184 else
187 callback(null); 185 callback(null);
188 break; 186 break;
189 case "app.listen": 187 case "app.listen":
188 var listenerFilters = getListenerFilters(sender.page, true);
190 if (message.filter) 189 if (message.filter)
191 listenerFilters.app = message.filter; 190 listenerFilters.app = message.filter;
192 else 191 else
193 delete listenerFilters.app; 192 delete listenerFilters.app;
194 break; 193 break;
195 case "app.open": 194 case "app.open":
196 if (message.what == "options") 195 if (message.what == "options")
197 ext.showOptions(); 196 ext.showOptions();
198 break; 197 break;
199 case "filters.add": 198 case "filters.add":
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 var filter = subscription.filters[j]; 279 var filter = subscription.filters[j];
281 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) 280 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
282 continue; 281 continue;
283 282
284 if (!(filter.text in seenFilter)) 283 if (!(filter.text in seenFilter))
285 FilterStorage.removeFilter(filter); 284 FilterStorage.removeFilter(filter);
286 } 285 }
287 } 286 }
288 break; 287 break;
289 case "filters.listen": 288 case "filters.listen":
289 var listenerFilters = getListenerFilters(sender.page, true);
290 if (message.filter) 290 if (message.filter)
291 listenerFilters.filter = message.filter; 291 listenerFilters.filter = message.filter;
292 else 292 else
293 delete listenerFilters.filter; 293 delete listenerFilters.filter;
294 break; 294 break;
295 case "filters.remove": 295 case "filters.remove":
296 var filter = Filter.fromText(message.text); 296 var filter = Filter.fromText(message.text);
297 var subscription = null; 297 var subscription = null;
298 if (message.subscriptionUrl) 298 if (message.subscriptionUrl)
299 subscription = Subscription.fromURL(message.subscriptionUrl); 299 subscription = Subscription.fromURL(message.subscriptionUrl);
300 300
301 if (!subscription) 301 if (!subscription)
302 FilterStorage.removeFilter(filter); 302 FilterStorage.removeFilter(filter);
303 else 303 else
304 FilterStorage.removeFilter(filter, subscription, message.index); 304 FilterStorage.removeFilter(filter, subscription, message.index);
305 break; 305 break;
306 case "prefs.get": 306 case "prefs.get":
307 callback(Prefs[message.key]); 307 callback(Prefs[message.key]);
308 break; 308 break;
309 case "prefs.listen": 309 case "prefs.listen":
310 var listenerFilters = getListenerFilters(sender.page);
310 if (message.filter) 311 if (message.filter)
312 {
313 message.filter.forEach(function(preference)
314 {
315 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 {
317 listenedPreferences.push(preference);
318 Prefs.on(preference, function()
319 {
320 sendMessage("pref", preference, [Prefs[preference]]);
321 });
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 }
323 });
311 listenerFilters.pref = message.filter; 324 listenerFilters.pref = message.filter;
325 }
312 else 326 else
313 delete listenerFilters.pref; 327 delete listenerFilters.pref;
314 break; 328 break;
315 case "prefs.toggle": 329 case "prefs.toggle":
316 if (message.key == "notifications_ignoredcategories") 330 if (message.key == "notifications_ignoredcategories")
317 NotificationStorage.toggleIgnoreCategory("*"); 331 NotificationStorage.toggleIgnoreCategory("*");
318 else 332 else
319 Prefs[message.key] = !Prefs[message.key]; 333 Prefs[message.key] = !Prefs[message.key];
320 break; 334 break;
321 case "subscriptions.add": 335 case "subscriptions.add":
(...skipping 26 matching lines...) Expand all
348 return false; 362 return false;
349 if (s instanceof DownloadableSubscription && message.downloadable) 363 if (s instanceof DownloadableSubscription && message.downloadable)
350 return true; 364 return true;
351 if (s instanceof SpecialSubscription && message.special) 365 if (s instanceof SpecialSubscription && message.special)
352 return true; 366 return true;
353 return false; 367 return false;
354 }); 368 });
355 callback(subscriptions.map(convertSubscription)); 369 callback(subscriptions.map(convertSubscription));
356 break; 370 break;
357 case "subscriptions.listen": 371 case "subscriptions.listen":
372 var listenerFilters = getListenerFilters(sender.page, true);
Thomas Greiner 2016/03/21 17:55:55 Nice. No idea why we didn't implement it like this
358 if (message.filter) 373 if (message.filter)
359 listenerFilters.subscription = message.filter; 374 listenerFilters.subscription = message.filter;
360 else 375 else
361 delete listenerFilters.subscription; 376 delete listenerFilters.subscription;
362 break; 377 break;
363 case "subscriptions.remove": 378 case "subscriptions.remove":
364 var subscription = Subscription.fromURL(message.url); 379 var subscription = Subscription.fromURL(message.url);
365 if (subscription.url in FilterStorage.knownSubscriptions) 380 if (subscription.url in FilterStorage.knownSubscriptions)
366 FilterStorage.removeSubscription(subscription); 381 FilterStorage.removeSubscription(subscription);
367 break; 382 break;
(...skipping 29 matching lines...) Expand all
397 if (subscription instanceof DownloadableSubscription) 412 if (subscription instanceof DownloadableSubscription)
398 Synchronizer.execute(subscription, true); 413 Synchronizer.execute(subscription, true);
399 } 414 }
400 break; 415 break;
401 case "subscriptions.isDownloading": 416 case "subscriptions.isDownloading":
402 callback(Synchronizer.isExecuting(message.url)); 417 callback(Synchronizer.isExecuting(message.url));
403 break; 418 break;
404 } 419 }
405 }); 420 });
406 })(this); 421 })(this);
OLDNEW
« background.js ('K') | « background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld