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: Unsupport *.listen with null Created March 24, 2016, 2:33 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)
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)
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++)
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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 devToolsPanel: info.platform == "chromium", 178 devToolsPanel: info.platform == "chromium",
171 safariContentBlocker: "safari" in global 179 safariContentBlocker: "safari" in global
172 && "extension" in global.safari 180 && "extension" in global.safari
173 && "setContentBlocker" in global.safari.extension 181 && "setContentBlocker" in global.safari.extension
174 }); 182 });
175 } 183 }
176 else 184 else
177 callback(null); 185 callback(null);
178 break; 186 break;
179 case "app.listen": 187 case "app.listen":
180 var listenerFilters = getListenerFilters(sender.page); 188 getListenerFilters(sender.page).app = message.filter;
181 if (message.filter)
182 listenerFilters.app = message.filter;
183 else
184 delete listenerFilters.app;
185 break; 189 break;
186 case "app.open": 190 case "app.open":
187 if (message.what == "options") 191 if (message.what == "options")
188 ext.showOptions(); 192 ext.showOptions();
189 break; 193 break;
190 case "filters.add": 194 case "filters.add":
191 var result = require("filterValidation").parseFilter(message.text); 195 var result = require("filterValidation").parseFilter(message.text);
192 var errors = []; 196 var errors = [];
193 if (result.error) 197 if (result.error)
194 errors.push(result.error.toString()); 198 errors.push(result.error.toString());
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 var filter = subscription.filters[j]; 275 var filter = subscription.filters[j];
272 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) 276 if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
273 continue; 277 continue;
274 278
275 if (!(filter.text in seenFilter)) 279 if (!(filter.text in seenFilter))
276 FilterStorage.removeFilter(filter); 280 FilterStorage.removeFilter(filter);
277 } 281 }
278 } 282 }
279 break; 283 break;
280 case "filters.listen": 284 case "filters.listen":
281 var listenerFilters = getListenerFilters(sender.page); 285 getListenerFilters(sender.page).filter = message.filter;
282 if (message.filter) 286 addFilterListeners("filter", message.filter);
283 {
284 FilterNotifier.addListener(onFilterChange);
285 listenerFilters.filter = message.filter;
286 }
287 else
288 delete listenerFilters.filter;
289 break; 287 break;
290 case "filters.remove": 288 case "filters.remove":
291 var filter = Filter.fromText(message.text); 289 var filter = Filter.fromText(message.text);
292 var subscription = null; 290 var subscription = null;
293 if (message.subscriptionUrl) 291 if (message.subscriptionUrl)
294 subscription = Subscription.fromURL(message.subscriptionUrl); 292 subscription = Subscription.fromURL(message.subscriptionUrl);
295 293
296 if (!subscription) 294 if (!subscription)
297 FilterStorage.removeFilter(filter); 295 FilterStorage.removeFilter(filter);
298 else 296 else
299 FilterStorage.removeFilter(filter, subscription, message.index); 297 FilterStorage.removeFilter(filter, subscription, message.index);
300 break; 298 break;
301 case "prefs.get": 299 case "prefs.get":
302 callback(Prefs[message.key]); 300 callback(Prefs[message.key]);
303 break; 301 break;
304 case "prefs.listen": 302 case "prefs.listen":
305 var listenerFilters = getListenerFilters(sender.page); 303 getListenerFilters(sender.page).pref = message.filter;
306 if (message.filter) 304 message.filter.forEach(function(preference)
307 { 305 {
308 message.filter.forEach(function(preference) 306 if (listenedPreferences.indexOf(preference) == -1)
309 { 307 {
310 if (listenedPreferences.indexOf(preference) == -1) 308 listenedPreferences.push(preference);
309 Prefs.on(preference, function()
311 { 310 {
312 listenedPreferences.push(preference); 311 sendMessage("pref", preference, Prefs[preference]);
313 Prefs.on(preference, function() 312 });
314 { 313 }
315 sendMessage("pref", preference, [Prefs[preference]]); 314 });
316 });
317 }
318 });
319 listenerFilters.pref = message.filter;
320 }
321 else
322 delete listenerFilters.pref;
323 break; 315 break;
324 case "prefs.toggle": 316 case "prefs.toggle":
325 if (message.key == "notifications_ignoredcategories") 317 if (message.key == "notifications_ignoredcategories")
326 NotificationStorage.toggleIgnoreCategory("*"); 318 NotificationStorage.toggleIgnoreCategory("*");
327 else 319 else
328 Prefs[message.key] = !Prefs[message.key]; 320 Prefs[message.key] = !Prefs[message.key];
329 break; 321 break;
330 case "subscriptions.add": 322 case "subscriptions.add":
331 var subscription = Subscription.fromURL(message.url); 323 var subscription = Subscription.fromURL(message.url);
332 if ("title" in message) 324 if ("title" in message)
333 subscription.title = message.title; 325 subscription.title = message.title;
334 if ("homepage" in message) 326 if ("homepage" in message)
335 subscription.homepage = message.homepage; 327 subscription.homepage = message.homepage;
336 328
337 if (message.confirm) 329 if (message.confirm)
338 { 330 {
339 ext.showOptions(function() 331 ext.showOptions(function()
340 { 332 {
341 sendMessage("app", "addSubscription", [convertSubscription(subscript ion)]); 333 sendMessage("app", "addSubscription", subscription);
342 }); 334 });
343 } 335 }
344 else 336 else
345 { 337 {
346 subscription.disabled = false; 338 subscription.disabled = false;
347 FilterStorage.addSubscription(subscription); 339 FilterStorage.addSubscription(subscription);
348 340
349 if (subscription instanceof DownloadableSubscription && !subscription. lastDownload) 341 if (subscription instanceof DownloadableSubscription && !subscription. lastDownload)
350 Synchronizer.execute(subscription); 342 Synchronizer.execute(subscription);
351 } 343 }
352 break; 344 break;
353 case "subscriptions.get": 345 case "subscriptions.get":
354 var subscriptions = FilterStorage.subscriptions.filter(function(s) 346 var subscriptions = FilterStorage.subscriptions.filter(function(s)
355 { 347 {
356 if (message.ignoreDisabled && s.disabled) 348 if (message.ignoreDisabled && s.disabled)
357 return false; 349 return false;
358 if (s instanceof DownloadableSubscription && message.downloadable) 350 if (s instanceof DownloadableSubscription && message.downloadable)
359 return true; 351 return true;
360 if (s instanceof SpecialSubscription && message.special) 352 if (s instanceof SpecialSubscription && message.special)
361 return true; 353 return true;
362 return false; 354 return false;
363 }); 355 });
364 callback(subscriptions.map(convertSubscription)); 356 callback(subscriptions.map(convertSubscription));
365 break; 357 break;
366 case "subscriptions.listen": 358 case "subscriptions.listen":
367 var listenerFilters = getListenerFilters(sender.page); 359 getListenerFilters(sender.page).subscription = message.filter;
368 if (message.filter) 360 addFilterListeners("subscription", message.filter);
369 {
370 FilterNotifier.addListener(onFilterChange);
371 listenerFilters.subscription = message.filter;
372 }
373 else
374 delete listenerFilters.subscription;
375 break; 361 break;
376 case "subscriptions.remove": 362 case "subscriptions.remove":
377 var subscription = Subscription.fromURL(message.url); 363 var subscription = Subscription.fromURL(message.url);
378 if (subscription.url in FilterStorage.knownSubscriptions) 364 if (subscription.url in FilterStorage.knownSubscriptions)
379 FilterStorage.removeSubscription(subscription); 365 FilterStorage.removeSubscription(subscription);
380 break; 366 break;
381 case "subscriptions.toggle": 367 case "subscriptions.toggle":
382 var subscription = Subscription.fromURL(message.url); 368 var subscription = Subscription.fromURL(message.url);
383 if (subscription.url in FilterStorage.knownSubscriptions) 369 if (subscription.url in FilterStorage.knownSubscriptions)
384 { 370 {
385 if (subscription.disabled || message.keepInstalled) 371 if (subscription.disabled || message.keepInstalled)
386 {
387 subscription.disabled = !subscription.disabled; 372 subscription.disabled = !subscription.disabled;
388 FilterNotifier.triggerListeners("subscription.disabled",
389 subscription);
390 }
391 else 373 else
392 FilterStorage.removeSubscription(subscription); 374 FilterStorage.removeSubscription(subscription);
393 } 375 }
394 else 376 else
395 { 377 {
396 subscription.disabled = false; 378 subscription.disabled = false;
397 subscription.title = message.title; 379 subscription.title = message.title;
398 subscription.homepage = message.homepage; 380 subscription.homepage = message.homepage;
399 FilterStorage.addSubscription(subscription); 381 FilterStorage.addSubscription(subscription);
400 if (!subscription.lastDownload) 382 if (!subscription.lastDownload)
401 Synchronizer.execute(subscription); 383 Synchronizer.execute(subscription);
402 } 384 }
403 break; 385 break;
404 case "subscriptions.update": 386 case "subscriptions.update":
405 var subscriptions = message.url ? [Subscription.fromURL(message.url)] : 387 var subscriptions = message.url ? [Subscription.fromURL(message.url)] :
406 FilterStorage.subscriptions; 388 FilterStorage.subscriptions;
407 for (var i = 0; i < subscriptions.length; i++) 389 for (var i = 0; i < subscriptions.length; i++)
408 { 390 {
409 var subscription = subscriptions[i]; 391 var subscription = subscriptions[i];
410 if (subscription instanceof DownloadableSubscription) 392 if (subscription instanceof DownloadableSubscription)
411 Synchronizer.execute(subscription, true); 393 Synchronizer.execute(subscription, true);
412 } 394 }
413 break; 395 break;
414 case "subscriptions.isDownloading": 396 case "subscriptions.isDownloading":
415 callback(Synchronizer.isExecuting(message.url)); 397 callback(Synchronizer.isExecuting(message.url));
416 break; 398 break;
417 } 399 }
418 }); 400 });
419 })(this); 401 })(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