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

Side by Side Diff: messageResponder.js

Issue 29339034: Issue 3869 - Use the new FilterNotifier API (Closed)
Patch Set: Created March 23, 2016, 11 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 | « 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 listenedPreferences = []; 56 var listenedPreferences = [];
57 var listenedFilterChanges = [];
57 var messageTypes = { 58 var messageTypes = {
58 "app": "app.listen", 59 "app": "app.listen",
59 "filter": "filters.listen", 60 "filter": "filters.listen",
60 "pref": "prefs.listen", 61 "pref": "prefs.listen",
61 "subscription": "subscriptions.listen" 62 "subscription": "subscriptions.listen"
62 }; 63 };
63 64
64 function sendMessage(type, action, args) 65 function sendMessage(type, action)
Sebastian Noack 2016/03/23 23:15:51 Sorry for refactoring sendMessage() again, but wit
65 { 66 {
66 var pages = changeListeners.keys(); 67 var pages = changeListeners.keys();
67 for (var i = 0; i < pages.length; i++) 68 if (pages.length == 0)
68 {
69 var filters = changeListeners.get(pages[i]);
70 var actions = filters[type];
71 if (actions && actions.indexOf(action) != -1)
72 {
73 pages[i].sendMessage({
74 type: messageTypes[type],
75 action: action,
76 args: args
77 });
78 }
79 }
80 }
81
82 function onFilterChange(action)
83 {
84 var type;
85 if (action == "load")
86 {
87 type = "filter";
88 action = "loaded";
89 }
90 else
91 {
92 var parts = action.split(".");
93 type = parts[0];
94 action = parts[1];
95 }
96
97 if (!(type in messageTypes))
98 return; 69 return;
99 70
100 var args = []; 71 var args = [];
101 for (var i = 1; i < arguments.length; i++) 72 for (var i = 2; i < arguments.length; i++)
102 { 73 {
103 var arg = arguments[i]; 74 var arg = arguments[i];
104 if (arg instanceof Subscription) 75 if (arg instanceof Subscription)
105 args.push(convertSubscription(arg)); 76 args.push(convertSubscription(arg));
106 else if (arg instanceof Filter) 77 else if (arg instanceof Filter)
107 args.push(convertFilter(arg)); 78 args.push(convertFilter(arg));
108 else 79 else
109 args.push(arg); 80 args.push(arg);
110 } 81 }
111 82
112 sendMessage(type, action, args); 83 for (var j = 0; j < pages.length; j++)
84 {
85 var page = pages[j];
86 var filters = changeListeners.get(page);
87 var actions = filters[type];
88 if (actions && actions.indexOf(action) != -1)
89 {
90 page.sendMessage({
91 type: messageTypes[type],
92 action: action,
93 args: args
94 });
95 }
96 }
97 }
98
99 function addFilterListeners(type, actions)
Sebastian Noack 2016/03/23 23:15:51 Same pattern as for Prefs.on(), the first UI that
100 {
101 actions.forEach(function(action)
102 {
103 var name;
104 if (type == "filter" && action == "loaded")
105 name = "load";
106 else
107 name = type + "." + action;
108
109 if (listenedFilterChanges.indexOf(name) == -1)
110 {
111 listenedFilterChanges.push(name);
112 FilterNotifier.on(name, function()
113 {
114 var args = [type, action];
115 for (var i = 0; i < arguments.length; i++)
Sebastian Noack 2016/03/23 23:15:51 For the record, simply passing the arguments objec
116 args.push(arguments[i]);
117 sendMessage.apply(null, args);
118 });
119 }
120 });
113 } 121 }
114 122
115 function getListenerFilters(page) 123 function getListenerFilters(page)
116 { 124 {
117 var listenerFilters = changeListeners.get(page); 125 var listenerFilters = changeListeners.get(page);
118 if (!listenerFilters) 126 if (!listenerFilters)
119 { 127 {
120 listenerFilters = Object.create(null); 128 listenerFilters = Object.create(null);
121 changeListeners.set(page, listenerFilters); 129 changeListeners.set(page, listenerFilters);
122 } 130 }
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 282
275 if (!(filter.text in seenFilter)) 283 if (!(filter.text in seenFilter))
276 FilterStorage.removeFilter(filter); 284 FilterStorage.removeFilter(filter);
277 } 285 }
278 } 286 }
279 break; 287 break;
280 case "filters.listen": 288 case "filters.listen":
281 var listenerFilters = getListenerFilters(sender.page); 289 var listenerFilters = getListenerFilters(sender.page);
282 if (message.filter) 290 if (message.filter)
283 { 291 {
284 FilterNotifier.addListener(onFilterChange); 292 addFilterListeners("filter", message.filter);
285 listenerFilters.filter = message.filter; 293 listenerFilters.filter = message.filter;
286 } 294 }
287 else 295 else
Sebastian Noack 2016/03/23 23:15:51 I wonder whether we should prehaps unsupport calli
Thomas Greiner 2016/03/24 14:22:35 Sorry, missed this comment. Yeah, in one of the p
Sebastian Noack 2016/03/24 14:40:35 So I read that as you are fine with me simplifying
288 delete listenerFilters.filter; 296 delete listenerFilters.filter;
289 break; 297 break;
290 case "filters.remove": 298 case "filters.remove":
291 var filter = Filter.fromText(message.text); 299 var filter = Filter.fromText(message.text);
292 var subscription = null; 300 var subscription = null;
293 if (message.subscriptionUrl) 301 if (message.subscriptionUrl)
294 subscription = Subscription.fromURL(message.subscriptionUrl); 302 subscription = Subscription.fromURL(message.subscriptionUrl);
295 303
296 if (!subscription) 304 if (!subscription)
297 FilterStorage.removeFilter(filter); 305 FilterStorage.removeFilter(filter);
298 else 306 else
299 FilterStorage.removeFilter(filter, subscription, message.index); 307 FilterStorage.removeFilter(filter, subscription, message.index);
300 break; 308 break;
301 case "prefs.get": 309 case "prefs.get":
302 callback(Prefs[message.key]); 310 callback(Prefs[message.key]);
303 break; 311 break;
304 case "prefs.listen": 312 case "prefs.listen":
305 var listenerFilters = getListenerFilters(sender.page); 313 var listenerFilters = getListenerFilters(sender.page);
306 if (message.filter) 314 if (message.filter)
307 { 315 {
308 message.filter.forEach(function(preference) 316 message.filter.forEach(function(preference)
309 { 317 {
310 if (listenedPreferences.indexOf(preference) == -1) 318 if (listenedPreferences.indexOf(preference) == -1)
311 { 319 {
312 listenedPreferences.push(preference); 320 listenedPreferences.push(preference);
313 Prefs.on(preference, function() 321 Prefs.on(preference, function()
314 { 322 {
315 sendMessage("pref", preference, [Prefs[preference]]); 323 sendMessage("pref", preference, Prefs[preference]);
316 }); 324 });
317 } 325 }
318 }); 326 });
319 listenerFilters.pref = message.filter; 327 listenerFilters.pref = message.filter;
320 } 328 }
321 else 329 else
322 delete listenerFilters.pref; 330 delete listenerFilters.pref;
323 break; 331 break;
324 case "prefs.toggle": 332 case "prefs.toggle":
325 if (message.key == "notifications_ignoredcategories") 333 if (message.key == "notifications_ignoredcategories")
326 NotificationStorage.toggleIgnoreCategory("*"); 334 NotificationStorage.toggleIgnoreCategory("*");
327 else 335 else
328 Prefs[message.key] = !Prefs[message.key]; 336 Prefs[message.key] = !Prefs[message.key];
329 break; 337 break;
330 case "subscriptions.add": 338 case "subscriptions.add":
331 var subscription = Subscription.fromURL(message.url); 339 var subscription = Subscription.fromURL(message.url);
332 if ("title" in message) 340 if ("title" in message)
333 subscription.title = message.title; 341 subscription.title = message.title;
334 if ("homepage" in message) 342 if ("homepage" in message)
335 subscription.homepage = message.homepage; 343 subscription.homepage = message.homepage;
336 344
337 if (message.confirm) 345 if (message.confirm)
338 { 346 {
339 ext.showOptions(function() 347 ext.showOptions(function()
340 { 348 {
341 sendMessage("app", "addSubscription", [convertSubscription(subscript ion)]); 349 sendMessage("app", "addSubscription", subscription);
342 }); 350 });
343 } 351 }
344 else 352 else
345 { 353 {
346 subscription.disabled = false; 354 subscription.disabled = false;
347 FilterStorage.addSubscription(subscription); 355 FilterStorage.addSubscription(subscription);
348 356
349 if (subscription instanceof DownloadableSubscription && !subscription. lastDownload) 357 if (subscription instanceof DownloadableSubscription && !subscription. lastDownload)
350 Synchronizer.execute(subscription); 358 Synchronizer.execute(subscription);
351 } 359 }
352 break; 360 break;
353 case "subscriptions.get": 361 case "subscriptions.get":
354 var subscriptions = FilterStorage.subscriptions.filter(function(s) 362 var subscriptions = FilterStorage.subscriptions.filter(function(s)
355 { 363 {
356 if (message.ignoreDisabled && s.disabled) 364 if (message.ignoreDisabled && s.disabled)
357 return false; 365 return false;
358 if (s instanceof DownloadableSubscription && message.downloadable) 366 if (s instanceof DownloadableSubscription && message.downloadable)
359 return true; 367 return true;
360 if (s instanceof SpecialSubscription && message.special) 368 if (s instanceof SpecialSubscription && message.special)
361 return true; 369 return true;
362 return false; 370 return false;
363 }); 371 });
364 callback(subscriptions.map(convertSubscription)); 372 callback(subscriptions.map(convertSubscription));
365 break; 373 break;
366 case "subscriptions.listen": 374 case "subscriptions.listen":
367 var listenerFilters = getListenerFilters(sender.page); 375 var listenerFilters = getListenerFilters(sender.page);
368 if (message.filter) 376 if (message.filter)
369 { 377 {
370 FilterNotifier.addListener(onFilterChange); 378 addFilterListeners("subscription", message.filter);
371 listenerFilters.subscription = message.filter; 379 listenerFilters.subscription = message.filter;
372 } 380 }
373 else 381 else
374 delete listenerFilters.subscription; 382 delete listenerFilters.subscription;
375 break; 383 break;
376 case "subscriptions.remove": 384 case "subscriptions.remove":
377 var subscription = Subscription.fromURL(message.url); 385 var subscription = Subscription.fromURL(message.url);
378 if (subscription.url in FilterStorage.knownSubscriptions) 386 if (subscription.url in FilterStorage.knownSubscriptions)
379 FilterStorage.removeSubscription(subscription); 387 FilterStorage.removeSubscription(subscription);
380 break; 388 break;
381 case "subscriptions.toggle": 389 case "subscriptions.toggle":
382 var subscription = Subscription.fromURL(message.url); 390 var subscription = Subscription.fromURL(message.url);
383 if (subscription.url in FilterStorage.knownSubscriptions) 391 if (subscription.url in FilterStorage.knownSubscriptions)
384 { 392 {
385 if (subscription.disabled || message.keepInstalled) 393 if (subscription.disabled || message.keepInstalled)
386 {
387 subscription.disabled = !subscription.disabled; 394 subscription.disabled = !subscription.disabled;
388 FilterNotifier.triggerListeners("subscription.disabled",
Sebastian Noack 2016/03/23 23:15:51 This belongs into the mock implementation. Otherwi
Thomas Greiner 2016/03/24 14:12:16 I agree. Well spotted.
389 subscription);
390 }
391 else 395 else
392 FilterStorage.removeSubscription(subscription); 396 FilterStorage.removeSubscription(subscription);
393 } 397 }
394 else 398 else
395 { 399 {
396 subscription.disabled = false; 400 subscription.disabled = false;
397 subscription.title = message.title; 401 subscription.title = message.title;
398 subscription.homepage = message.homepage; 402 subscription.homepage = message.homepage;
399 FilterStorage.addSubscription(subscription); 403 FilterStorage.addSubscription(subscription);
400 if (!subscription.lastDownload) 404 if (!subscription.lastDownload)
401 Synchronizer.execute(subscription); 405 Synchronizer.execute(subscription);
402 } 406 }
403 break; 407 break;
404 case "subscriptions.update": 408 case "subscriptions.update":
405 var subscriptions = message.url ? [Subscription.fromURL(message.url)] : 409 var subscriptions = message.url ? [Subscription.fromURL(message.url)] :
406 FilterStorage.subscriptions; 410 FilterStorage.subscriptions;
407 for (var i = 0; i < subscriptions.length; i++) 411 for (var i = 0; i < subscriptions.length; i++)
408 { 412 {
409 var subscription = subscriptions[i]; 413 var subscription = subscriptions[i];
410 if (subscription instanceof DownloadableSubscription) 414 if (subscription instanceof DownloadableSubscription)
411 Synchronizer.execute(subscription, true); 415 Synchronizer.execute(subscription, true);
412 } 416 }
413 break; 417 break;
414 case "subscriptions.isDownloading": 418 case "subscriptions.isDownloading":
415 callback(Synchronizer.isExecuting(message.url)); 419 callback(Synchronizer.isExecuting(message.url));
416 break; 420 break;
417 } 421 }
418 }); 422 });
419 })(this); 423 })(this);
OLDNEW
« no previous file with comments | « background.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld