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 19, 2016, 7:14 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
« no previous file with comments | « no previous file | 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 var convertFilter = convertObject.bind(null, ["text"]); 53 var convertFilter = convertObject.bind(null, ["text"]);
54 54
55 var changeListeners = null; 55 var changeListeners = null;
56 var messageTypes = { 56 var messageTypes = {
57 "app": "app.listen", 57 "app": "app.listen",
58 "filter": "filters.listen", 58 "filter": "filters.listen",
59 "pref": "prefs.listen", 59 "pref": "prefs.listen",
60 "subscription": "subscriptions.listen" 60 "subscription": "subscriptions.listen"
61 }; 61 };
62 62
63 function sendMessage(type, action, args, page) 63 function sendMessage(type, action, args)
64 { 64 {
65 var pages = page ? [page] : changeListeners.keys(); 65 var pages = changeListeners.keys();
Sebastian Noack 2016/03/19 19:17:29 This is unrelated, but since page is never specifi
66 for (var i = 0; i < pages.length; i++) 66 for (var i = 0; i < pages.length; i++)
67 { 67 {
68 var filters = changeListeners.get(pages[i]); 68 var filters = changeListeners.get(pages[i]);
69 if (filters[type] && filters[type].indexOf(action) >= 0) 69 var actions = filters[type];
70 if (actions && !(actions instanceof Array && actions.indexOf(action) == -1 ))
Sebastian Noack 2016/03/19 19:17:29 filters.pref is an object now, not an array anymor
70 { 71 {
71 pages[i].sendMessage({ 72 pages[i].sendMessage({
72 type: messageTypes[type], 73 type: messageTypes[type],
73 action: action, 74 action: action,
74 args: args 75 args: args
75 }); 76 });
76 } 77 }
77 } 78 }
78 } 79 }
79 80
(...skipping 23 matching lines...) Expand all
103 if (arg instanceof Subscription) 104 if (arg instanceof Subscription)
104 return convertSubscription(arg); 105 return convertSubscription(arg);
105 else if (arg instanceof Filter) 106 else if (arg instanceof Filter)
106 return convertFilter(arg); 107 return convertFilter(arg);
107 else 108 else
108 return arg; 109 return arg;
109 }); 110 });
110 sendMessage(type, action, args); 111 sendMessage(type, action, args);
111 } 112 }
112 113
113 function onPrefChange(name)
114 {
115 sendMessage("pref", name, [Prefs[name]]);
116 }
117
118 global.ext.onMessage.addListener(function(message, sender, callback) 114 global.ext.onMessage.addListener(function(message, sender, callback)
119 { 115 {
120 var listenerFilters = null; 116 var listenerFilters = null;
121 if (/\.listen$/.test(message.type)) 117 if (/\.listen$/.test(message.type))
122 { 118 {
123 if (!changeListeners) 119 if (!changeListeners)
124 { 120 {
125 changeListeners = new global.ext.PageMap(); 121 changeListeners = new global.ext.PageMap();
126 FilterNotifier.addListener(onFilterChange); 122 FilterNotifier.addListener(onFilterChange);
127 Prefs.onChanged.addListener(onPrefChange);
128 } 123 }
129 124
130 listenerFilters = changeListeners.get(sender.page); 125 listenerFilters = changeListeners.get(sender.page);
131 if (!listenerFilters) 126 if (!listenerFilters)
132 { 127 {
133 listenerFilters = Object.create(null); 128 listenerFilters = Object.create(null);
134 changeListeners.set(sender.page, listenerFilters); 129 changeListeners.set(sender.page, listenerFilters);
135 } 130 }
136 } 131 }
137 132
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
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":
305 for (var preference in listenerFilters.pref)
306 Prefs.off(preference, listenerFilters.pref[preference]);
307
310 if (message.filter) 308 if (message.filter)
311 listenerFilters.pref = message.filter; 309 {
310 listenerFilters.pref = Object.create(null);
311 message.filter.forEach(function(preference)
312 {
313 function callback()
314 {
315 sendMessage("prefs", preference, [Prefs[preference]]);
316 }
317
318 listenerFilters.pref[preference] = callback;
319 Prefs.on(preference, callback);
320 });
321 }
312 else 322 else
313 delete listenerFilters.pref; 323 delete listenerFilters.pref;
314 break; 324 break;
315 case "prefs.toggle": 325 case "prefs.toggle":
316 if (message.key == "notifications_ignoredcategories") 326 if (message.key == "notifications_ignoredcategories")
317 NotificationStorage.toggleIgnoreCategory("*"); 327 NotificationStorage.toggleIgnoreCategory("*");
318 else 328 else
319 Prefs[message.key] = !Prefs[message.key]; 329 Prefs[message.key] = !Prefs[message.key];
320 break; 330 break;
321 case "subscriptions.add": 331 case "subscriptions.add":
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 if (subscription instanceof DownloadableSubscription) 407 if (subscription instanceof DownloadableSubscription)
398 Synchronizer.execute(subscription, true); 408 Synchronizer.execute(subscription, true);
399 } 409 }
400 break; 410 break;
401 case "subscriptions.isDownloading": 411 case "subscriptions.isDownloading":
402 callback(Synchronizer.isExecuting(message.url)); 412 callback(Synchronizer.isExecuting(message.url));
403 break; 413 break;
404 } 414 }
405 }); 415 });
406 })(this); 416 })(this);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld