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 19, 2016, 7:14 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 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 listenedPreferences = [];
56 var messageTypes = { 57 var messageTypes = {
57 "app": "app.listen", 58 "app": "app.listen",
58 "filter": "filters.listen", 59 "filter": "filters.listen",
59 "pref": "prefs.listen", 60 "pref": "prefs.listen",
60 "subscription": "subscriptions.listen" 61 "subscription": "subscriptions.listen"
61 }; 62 };
62 63
63 function sendMessage(type, action, args) 64 function sendMessage(type, action, args)
64 { 65 {
65 var pages = changeListeners.keys(); 66 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++) 67 for (var i = 0; i < pages.length; i++)
67 { 68 {
68 var filters = changeListeners.get(pages[i]); 69 var filters = changeListeners.get(pages[i]);
69 var actions = filters[type]; 70 var actions = filters[type];
70 if (actions && !(actions instanceof Array && actions.indexOf(action) == -1 )) 71 if (actions && actions.indexOf(action) != -1)
Sebastian Noack 2016/03/19 19:17:29 filters.pref is an object now, not an array anymor
71 { 72 {
72 pages[i].sendMessage({ 73 pages[i].sendMessage({
73 type: messageTypes[type], 74 type: messageTypes[type],
74 action: action, 75 action: action,
75 args: args 76 args: args
76 }); 77 });
77 } 78 }
78 } 79 }
79 } 80 }
80 81
81 function onFilterChange(action) 82 function onFilterChange(action)
82 { 83 {
84 var type;
83 if (action == "load") 85 if (action == "load")
84 action = "filter.loaded"; 86 {
85 87 type = "filter";
86 var parts = action.split(".", 2); 88 action = "loaded";
87 var type;
88 if (parts.length == 1)
89 {
90 type = "app";
91 action = parts[0];
92 } 89 }
93 else 90 else
94 { 91 {
92 var parts = action.split(".");
95 type = parts[0]; 93 type = parts[0];
96 action = parts[1]; 94 action = parts[1];
97 } 95 }
98 96
99 if (!messageTypes.hasOwnProperty(type)) 97 if (!(type in messageTypes))
100 return; 98 return;
101 99
102 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) 100 var args = [];
103 { 101 for (var i = 1; i < arguments.length; i++)
102 {
103 var arg = arguments[i];
104 if (arg instanceof Subscription) 104 if (arg instanceof Subscription)
105 return convertSubscription(arg); 105 args.push(convertSubscription(arg));
106 else if (arg instanceof Filter) 106 else if (arg instanceof Filter)
107 return convertFilter(arg); 107 args.push(convertFilter(arg));
108 else 108 else
109 return arg; 109 args.push(arg);
110 }); 110 }
111
111 sendMessage(type, action, args); 112 sendMessage(type, action, args);
112 } 113 }
113 114
115 function getListenerFilters(page)
116 {
117 var listenerFilters = changeListeners.get(page);
118 if (!listenerFilters)
119 {
120 listenerFilters = Object.create(null);
121 changeListeners.set(page, listenerFilters);
122 }
123 return listenerFilters;
124 }
125
114 global.ext.onMessage.addListener(function(message, sender, callback) 126 global.ext.onMessage.addListener(function(message, sender, callback)
115 { 127 {
116 var listenerFilters = null;
117 if (/\.listen$/.test(message.type))
118 {
119 if (!changeListeners)
120 {
121 changeListeners = new global.ext.PageMap();
122 FilterNotifier.addListener(onFilterChange);
123 }
124
125 listenerFilters = changeListeners.get(sender.page);
126 if (!listenerFilters)
127 {
128 listenerFilters = Object.create(null);
129 changeListeners.set(sender.page, listenerFilters);
130 }
131 }
132
133 switch (message.type) 128 switch (message.type)
134 { 129 {
135 case "app.get": 130 case "app.get":
136 if (message.what == "issues") 131 if (message.what == "issues")
137 { 132 {
138 var subscriptionInit; 133 var subscriptionInit;
139 try 134 try
140 { 135 {
141 subscriptionInit = require("subscriptionInit"); 136 subscriptionInit = require("subscriptionInit");
142 } 137 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 devToolsPanel: info.platform == "chromium", 170 devToolsPanel: info.platform == "chromium",
176 safariContentBlocker: "safari" in global 171 safariContentBlocker: "safari" in global
177 && "extension" in global.safari 172 && "extension" in global.safari
178 && "setContentBlocker" in global.safari.extension 173 && "setContentBlocker" in global.safari.extension
179 }); 174 });
180 } 175 }
181 else 176 else
182 callback(null); 177 callback(null);
183 break; 178 break;
184 case "app.listen": 179 case "app.listen":
180 var listenerFilters = getListenerFilters(sender.page);
185 if (message.filter) 181 if (message.filter)
186 listenerFilters.app = message.filter; 182 listenerFilters.app = message.filter;
187 else 183 else
188 delete listenerFilters.app; 184 delete listenerFilters.app;
189 break; 185 break;
190 case "app.open": 186 case "app.open":
191 if (message.what == "options") 187 if (message.what == "options")
192 ext.showOptions(); 188 ext.showOptions();
193 break; 189 break;
194 case "filters.add": 190 case "filters.add":
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 var filter = subscription.filters[j]; 271 var filter = subscription.filters[j];
276 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) 272 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
277 continue; 273 continue;
278 274
279 if (!(filter.text in seenFilter)) 275 if (!(filter.text in seenFilter))
280 FilterStorage.removeFilter(filter); 276 FilterStorage.removeFilter(filter);
281 } 277 }
282 } 278 }
283 break; 279 break;
284 case "filters.listen": 280 case "filters.listen":
281 var listenerFilters = getListenerFilters(sender.page);
285 if (message.filter) 282 if (message.filter)
283 {
284 FilterNotifier.addListener(onFilterChange);
286 listenerFilters.filter = message.filter; 285 listenerFilters.filter = message.filter;
286 }
287 else 287 else
288 delete listenerFilters.filter; 288 delete listenerFilters.filter;
289 break; 289 break;
290 case "filters.remove": 290 case "filters.remove":
291 var filter = Filter.fromText(message.text); 291 var filter = Filter.fromText(message.text);
292 var subscription = null; 292 var subscription = null;
293 if (message.subscriptionUrl) 293 if (message.subscriptionUrl)
294 subscription = Subscription.fromURL(message.subscriptionUrl); 294 subscription = Subscription.fromURL(message.subscriptionUrl);
295 295
296 if (!subscription) 296 if (!subscription)
297 FilterStorage.removeFilter(filter); 297 FilterStorage.removeFilter(filter);
298 else 298 else
299 FilterStorage.removeFilter(filter, subscription, message.index); 299 FilterStorage.removeFilter(filter, subscription, message.index);
300 break; 300 break;
301 case "prefs.get": 301 case "prefs.get":
302 callback(Prefs[message.key]); 302 callback(Prefs[message.key]);
303 break; 303 break;
304 case "prefs.listen": 304 case "prefs.listen":
305 for (var preference in listenerFilters.pref) 305 var listenerFilters = getListenerFilters(sender.page);
306 Prefs.off(preference, listenerFilters.pref[preference]);
307
308 if (message.filter) 306 if (message.filter)
309 { 307 {
310 listenerFilters.pref = Object.create(null);
311 message.filter.forEach(function(preference) 308 message.filter.forEach(function(preference)
312 { 309 {
313 function callback() 310 if (listenedPreferences.indexOf(preference) == -1)
314 { 311 {
315 sendMessage("prefs", preference, [Prefs[preference]]); 312 listenedPreferences.push(preference);
313 Prefs.on(preference, function()
314 {
315 sendMessage("pref", preference, [Prefs[preference]]);
316 });
316 } 317 }
317
318 listenerFilters.pref[preference] = callback;
319 Prefs.on(preference, callback);
320 }); 318 });
319 listenerFilters.pref = message.filter;
321 } 320 }
322 else 321 else
323 delete listenerFilters.pref; 322 delete listenerFilters.pref;
324 break; 323 break;
325 case "prefs.toggle": 324 case "prefs.toggle":
326 if (message.key == "notifications_ignoredcategories") 325 if (message.key == "notifications_ignoredcategories")
327 NotificationStorage.toggleIgnoreCategory("*"); 326 NotificationStorage.toggleIgnoreCategory("*");
328 else 327 else
329 Prefs[message.key] = !Prefs[message.key]; 328 Prefs[message.key] = !Prefs[message.key];
330 break; 329 break;
331 case "subscriptions.add": 330 case "subscriptions.add":
332 var subscription = Subscription.fromURL(message.url); 331 var subscription = Subscription.fromURL(message.url);
333 if ("title" in message) 332 if ("title" in message)
334 subscription.title = message.title; 333 subscription.title = message.title;
335 if ("homepage" in message) 334 if ("homepage" in message)
336 subscription.homepage = message.homepage; 335 subscription.homepage = message.homepage;
337 336
338 if (message.confirm) 337 if (message.confirm)
339 { 338 {
340 ext.showOptions(function() 339 ext.showOptions(function()
341 { 340 {
342 onFilterChange("addSubscription", subscription); 341 sendMessage("app", "addSubscription", [convertSubscription(subscript ion)]);
343 }); 342 });
344 } 343 }
345 else 344 else
346 { 345 {
347 subscription.disabled = false; 346 subscription.disabled = false;
348 FilterStorage.addSubscription(subscription); 347 FilterStorage.addSubscription(subscription);
349 348
350 if (subscription instanceof DownloadableSubscription && !subscription. lastDownload) 349 if (subscription instanceof DownloadableSubscription && !subscription. lastDownload)
351 Synchronizer.execute(subscription); 350 Synchronizer.execute(subscription);
352 } 351 }
353 break; 352 break;
354 case "subscriptions.get": 353 case "subscriptions.get":
355 var subscriptions = FilterStorage.subscriptions.filter(function(s) 354 var subscriptions = FilterStorage.subscriptions.filter(function(s)
356 { 355 {
357 if (message.ignoreDisabled && s.disabled) 356 if (message.ignoreDisabled && s.disabled)
358 return false; 357 return false;
359 if (s instanceof DownloadableSubscription && message.downloadable) 358 if (s instanceof DownloadableSubscription && message.downloadable)
360 return true; 359 return true;
361 if (s instanceof SpecialSubscription && message.special) 360 if (s instanceof SpecialSubscription && message.special)
362 return true; 361 return true;
363 return false; 362 return false;
364 }); 363 });
365 callback(subscriptions.map(convertSubscription)); 364 callback(subscriptions.map(convertSubscription));
366 break; 365 break;
367 case "subscriptions.listen": 366 case "subscriptions.listen":
367 var listenerFilters = getListenerFilters(sender.page);
368 if (message.filter) 368 if (message.filter)
369 {
370 FilterNotifier.addListener(onFilterChange);
369 listenerFilters.subscription = message.filter; 371 listenerFilters.subscription = message.filter;
372 }
370 else 373 else
371 delete listenerFilters.subscription; 374 delete listenerFilters.subscription;
372 break; 375 break;
373 case "subscriptions.remove": 376 case "subscriptions.remove":
374 var subscription = Subscription.fromURL(message.url); 377 var subscription = Subscription.fromURL(message.url);
375 if (subscription.url in FilterStorage.knownSubscriptions) 378 if (subscription.url in FilterStorage.knownSubscriptions)
376 FilterStorage.removeSubscription(subscription); 379 FilterStorage.removeSubscription(subscription);
377 break; 380 break;
378 case "subscriptions.toggle": 381 case "subscriptions.toggle":
379 var subscription = Subscription.fromURL(message.url); 382 var subscription = Subscription.fromURL(message.url);
(...skipping 27 matching lines...) Expand all
407 if (subscription instanceof DownloadableSubscription) 410 if (subscription instanceof DownloadableSubscription)
408 Synchronizer.execute(subscription, true); 411 Synchronizer.execute(subscription, true);
409 } 412 }
410 break; 413 break;
411 case "subscriptions.isDownloading": 414 case "subscriptions.isDownloading":
412 callback(Synchronizer.isExecuting(message.url)); 415 callback(Synchronizer.isExecuting(message.url));
413 break; 416 break;
414 } 417 }
415 }); 418 });
416 })(this); 419 })(this);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld