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