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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 (function(global) | 18 (function(global) |
19 { | 19 { |
20 if (!global.ext) | 20 if (!global.ext) |
21 global.ext = require("ext_background"); | 21 global.ext = require("ext_background"); |
22 | 22 |
| 23 var Prefs = require("prefs").Prefs; |
23 var Utils = require("utils").Utils; | 24 var Utils = require("utils").Utils; |
24 var FilterStorage = require("filterStorage").FilterStorage; | 25 var FilterStorage = require("filterStorage").FilterStorage; |
25 var FilterNotifier = require("filterNotifier").FilterNotifier; | 26 var FilterNotifier = require("filterNotifier").FilterNotifier; |
26 var defaultMatcher = require("matcher").defaultMatcher; | 27 var defaultMatcher = require("matcher").defaultMatcher; |
27 var BlockingFilter = require("filterClasses").BlockingFilter; | 28 |
| 29 var filterClasses = require("filterClasses"); |
| 30 var Filter = filterClasses.Filter; |
| 31 var BlockingFilter = filterClasses.BlockingFilter; |
28 var Synchronizer = require("synchronizer").Synchronizer; | 32 var Synchronizer = require("synchronizer").Synchronizer; |
29 | 33 |
30 var subscriptionClasses = require("subscriptionClasses"); | 34 var subscriptionClasses = require("subscriptionClasses"); |
31 var Subscription = subscriptionClasses.Subscription; | 35 var Subscription = subscriptionClasses.Subscription; |
32 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; | 36 var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; |
33 var SpecialSubscription = subscriptionClasses.SpecialSubscription; | 37 var SpecialSubscription = subscriptionClasses.SpecialSubscription; |
34 | 38 |
35 var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url",
"downloadStatus"]; | 39 function convertObject(keys, obj) |
36 function convertSubscription(subscription) | |
37 { | 40 { |
38 var result = {}; | 41 var result = {}; |
39 for (var i = 0; i < subscriptionKeys.length; i++) | 42 for (var i = 0; i < keys.length; i++) |
40 result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]] | 43 result[keys[i]] = obj[keys[i]]; |
41 return result; | 44 return result; |
42 } | 45 } |
43 | 46 |
| 47 var convertSubscription = convertObject.bind(null, ["disabled", "downloadStatu
s", |
| 48 "homepage", "lastSuccess", "title", "url"]); |
| 49 var convertFilter = convertObject.bind(null, ["text"]); |
| 50 |
44 var changeListeners = null; | 51 var changeListeners = null; |
45 var messageTypes = { | 52 var messageTypes = { |
46 "app": "app.listen", | 53 "app": "app.listen", |
47 "filter": "filters.listen", | 54 "filter": "filters.listen", |
48 "subscription": "subscriptions.listen" | 55 "subscription": "subscriptions.listen" |
49 }; | 56 }; |
50 | 57 |
51 function onFilterChange(action) | 58 function onFilterChange(action) |
52 { | 59 { |
| 60 if (action == "load") |
| 61 action = "filter.loaded"; |
| 62 |
53 var parts = action.split(".", 2); | 63 var parts = action.split(".", 2); |
54 var type; | 64 var type; |
55 if (parts.length == 1) | 65 if (parts.length == 1) |
56 { | 66 { |
57 type = "app"; | 67 type = "app"; |
58 action = parts[0]; | 68 action = parts[0]; |
59 } | 69 } |
60 else | 70 else |
61 { | 71 { |
62 type = parts[0]; | 72 type = parts[0]; |
63 action = parts[1]; | 73 action = parts[1]; |
64 } | 74 } |
65 | 75 |
66 if (!messageTypes.hasOwnProperty(type)) | 76 if (!messageTypes.hasOwnProperty(type)) |
67 return; | 77 return; |
68 | 78 |
69 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) | 79 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) |
70 { | 80 { |
71 if (arg instanceof Subscription) | 81 if (arg instanceof Subscription) |
72 return convertSubscription(arg); | 82 return convertSubscription(arg); |
| 83 else if (arg instanceof Filter) |
| 84 return convertFilter(arg); |
73 else | 85 else |
74 return arg; | 86 return arg; |
75 }); | 87 }); |
76 | 88 |
77 var pages = changeListeners.keys(); | 89 var pages = changeListeners.keys(); |
78 for (var i = 0; i < pages.length; i++) | 90 for (var i = 0; i < pages.length; i++) |
79 { | 91 { |
80 var filters = changeListeners.get(pages[i]); | 92 var filters = changeListeners.get(pages[i]); |
81 if (filters[type] && filters[type].indexOf(action) >= 0) | 93 if (filters[type] && filters[type].indexOf(action) >= 0) |
82 { | 94 { |
83 pages[i].sendMessage({ | 95 pages[i].sendMessage({ |
84 type: messageTypes[type], | 96 type: messageTypes[type], |
85 action: action, | 97 action: action, |
86 args: args | 98 args: args |
87 }); | 99 }); |
88 } | 100 } |
89 } | 101 } |
90 }; | 102 }; |
91 | 103 |
92 global.ext.onMessage.addListener(function(message, sender, callback) | 104 global.ext.onMessage.addListener(function(message, sender, callback) |
93 { | 105 { |
| 106 var listenerFilters = null; |
94 switch (message.type) | 107 switch (message.type) |
95 { | 108 { |
| 109 case "app.listen": |
| 110 case "filters.listen": |
| 111 case "subscriptions.listen": |
| 112 if (!changeListeners) |
| 113 { |
| 114 changeListeners = new global.ext.PageMap(); |
| 115 FilterNotifier.addListener(onFilterChange); |
| 116 } |
| 117 |
| 118 listenerFilters = changeListeners.get(sender.page); |
| 119 if (!listenerFilters) |
| 120 { |
| 121 listenerFilters = Object.create(null); |
| 122 changeListeners.set(sender.page, listenerFilters); |
| 123 } |
| 124 break; |
| 125 } |
| 126 |
| 127 switch (message.type) |
| 128 { |
| 129 case "add-subscription": |
| 130 ext.showOptions(function() |
| 131 { |
| 132 var subscription = Subscription.fromURL(message.url); |
| 133 subscription.title = message.title; |
| 134 onFilterChange("addSubscription", subscription); |
| 135 }); |
| 136 break; |
96 case "app.get": | 137 case "app.get": |
97 if (message.what == "issues") | 138 if (message.what == "issues") |
98 { | 139 { |
99 var info = require("info"); | 140 var info = require("info"); |
100 callback({ | 141 callback({ |
101 seenDataCorruption: "seenDataCorruption" in global ? global.seenData
Corruption : false, | 142 seenDataCorruption: "seenDataCorruption" in global ? global.seenData
Corruption : false, |
102 filterlistsReinitialized: "filterlistsReinitialized" in global ? glo
bal.filterlistsReinitialized : false, | 143 filterlistsReinitialized: "filterlistsReinitialized" in global ? glo
bal.filterlistsReinitialized : false, |
103 legacySafariVersion: (info.platform == "safari" && ( | 144 legacySafariVersion: (info.platform == "safari" && ( |
104 Services.vc.compare(info.platformVersion, "6.0") < 0 || // bef
oreload breaks websites in Safari 5 | 145 Services.vc.compare(info.platformVersion, "6.0") < 0 || // bef
oreload breaks websites in Safari 5 |
105 Services.vc.compare(info.platformVersion, "6.1") == 0 || // ext
ensions are broken in 6.1 and 7.0 | 146 Services.vc.compare(info.platformVersion, "6.1") == 0 || // ext
ensions are broken in 6.1 and 7.0 |
106 Services.vc.compare(info.platformVersion, "7.0") == 0)) | 147 Services.vc.compare(info.platformVersion, "7.0") == 0)) |
107 }); | 148 }); |
108 } | 149 } |
109 else if (message.what == "doclink") | 150 else if (message.what == "doclink") |
110 callback(Utils.getDocLink(message.link)); | 151 callback(Utils.getDocLink(message.link)); |
111 else if (message.what == "localeInfo") | 152 else if (message.what == "localeInfo") |
112 { | 153 { |
113 callback({ | 154 callback({ |
114 locale: Utils.appLocale, | 155 locale: Utils.appLocale, |
115 isRTL: Utils.chromeRegistry.isLocaleRTL("adblockplus") | 156 isRTL: Utils.chromeRegistry.isLocaleRTL("adblockplus") |
116 }); | 157 }); |
117 } | 158 } |
| 159 else if (message.what == "addonVersion") |
| 160 { |
| 161 callback(require("info").addonVersion); |
| 162 } |
118 else | 163 else |
119 callback(null); | 164 callback(null); |
120 break; | 165 break; |
| 166 case "app.listen": |
| 167 if (message.filter) |
| 168 listenerFilters.app = message.filter; |
| 169 else |
| 170 delete listenerFilters.app; |
| 171 break; |
121 case "app.open": | 172 case "app.open": |
122 if (message.what == "options") | 173 if (message.what == "options") |
123 ext.showOptions(); | 174 ext.showOptions(); |
124 break; | 175 break; |
| 176 case "filters.add": |
| 177 var filter = Filter.fromText(message.text); |
| 178 FilterStorage.addFilter(filter); |
| 179 break; |
| 180 case "filters.blocked": |
| 181 var filter = defaultMatcher.matchesAny(message.url, message.requestType, |
| 182 message.docDomain, message.thirdParty); |
| 183 callback(filter instanceof BlockingFilter); |
| 184 break; |
| 185 case "filters.get": |
| 186 var subscription = Subscription.fromURL(message.subscriptionUrl); |
| 187 if (!subscription) |
| 188 { |
| 189 callback([]); |
| 190 break; |
| 191 } |
| 192 |
| 193 callback(subscription.filters.map(convertFilter)); |
| 194 break; |
| 195 case "filters.listen": |
| 196 if (message.filter) |
| 197 listenerFilters.filter = message.filter; |
| 198 else |
| 199 delete listenerFilters.filter; |
| 200 break; |
| 201 case "filters.remove": |
| 202 var filter = Filter.fromText(message.text); |
| 203 var subscription = null; |
| 204 if (message.subscriptionUrl) |
| 205 subscription = Subscription.fromURL(message.subscriptionUrl); |
| 206 |
| 207 if (!subscription) |
| 208 FilterStorage.removeFilter(filter); |
| 209 else |
| 210 FilterStorage.removeFilter(filter, subscription, message.index); |
| 211 break; |
| 212 case "prefs.get": |
| 213 callback(Prefs[message.key]); |
| 214 break; |
| 215 case "subscriptions.add": |
| 216 if (message.url in FilterStorage.knownSubscriptions) |
| 217 return; |
| 218 |
| 219 var subscription = Subscription.fromURL(message.url); |
| 220 if (!subscription) |
| 221 return; |
| 222 |
| 223 subscription.disabled = false; |
| 224 if ("title" in message) |
| 225 subscription.title = message.title; |
| 226 if ("homepage" in message) |
| 227 subscription.homepage = message.homepage; |
| 228 FilterStorage.addSubscription(subscription); |
| 229 |
| 230 if (subscription instanceof DownloadableSubscription && !subscription.la
stDownload) |
| 231 Synchronizer.execute(subscription); |
| 232 break; |
125 case "subscriptions.get": | 233 case "subscriptions.get": |
126 var subscriptions = FilterStorage.subscriptions.filter(function(s) | 234 var subscriptions = FilterStorage.subscriptions.filter(function(s) |
127 { | 235 { |
128 if (message.ignoreDisabled && s.disabled) | 236 if (message.ignoreDisabled && s.disabled) |
129 return false; | 237 return false; |
130 if (s instanceof DownloadableSubscription && message.downloadable) | 238 if (s instanceof DownloadableSubscription && message.downloadable) |
131 return true; | 239 return true; |
132 if (s instanceof SpecialSubscription && message.special) | 240 if (s instanceof SpecialSubscription && message.special) |
133 return true; | 241 return true; |
134 return false; | 242 return false; |
135 }); | 243 }); |
136 callback(subscriptions.map(convertSubscription)); | 244 callback(subscriptions.map(convertSubscription)); |
137 break; | 245 break; |
138 case "filters.blocked": | 246 case "subscriptions.listen": |
139 var filter = defaultMatcher.matchesAny(message.url, message.requestType,
message.docDomain, message.thirdParty); | 247 if (message.filter) |
140 callback(filter instanceof BlockingFilter); | 248 listenerFilters.subscription = message.filter; |
| 249 else |
| 250 delete listenerFilters.subscription; |
| 251 break; |
| 252 case "subscriptions.remove": |
| 253 var subscription = Subscription.fromURL(message.url); |
| 254 if (subscription.url in FilterStorage.knownSubscriptions) |
| 255 FilterStorage.removeSubscription(subscription); |
141 break; | 256 break; |
142 case "subscriptions.toggle": | 257 case "subscriptions.toggle": |
143 var subscription = Subscription.fromURL(message.url); | 258 var subscription = Subscription.fromURL(message.url); |
144 if (subscription.url in FilterStorage.knownSubscriptions && !subscriptio
n.disabled) | 259 if (subscription.url in FilterStorage.knownSubscriptions && !subscriptio
n.disabled) |
145 FilterStorage.removeSubscription(subscription); | 260 FilterStorage.removeSubscription(subscription); |
146 else | 261 else |
147 { | 262 { |
148 subscription.disabled = false; | 263 subscription.disabled = false; |
149 subscription.title = message.title; | 264 subscription.title = message.title; |
150 subscription.homepage = message.homepage; | 265 subscription.homepage = message.homepage; |
151 FilterStorage.addSubscription(subscription); | 266 FilterStorage.addSubscription(subscription); |
152 if (!subscription.lastDownload) | 267 if (!subscription.lastDownload) |
153 Synchronizer.execute(subscription); | 268 Synchronizer.execute(subscription); |
154 } | 269 } |
155 break; | 270 break; |
156 case "subscriptions.listen": | |
157 if (!changeListeners) | |
158 { | |
159 changeListeners = new global.ext.PageMap(); | |
160 FilterNotifier.addListener(onFilterChange); | |
161 } | |
162 | |
163 var filters = changeListeners.get(sender.page); | |
164 if (!filters) | |
165 { | |
166 filters = Object.create(null); | |
167 changeListeners.set(sender.page, filters); | |
168 } | |
169 | |
170 if (message.filter) | |
171 filters.subscription = message.filter; | |
172 else | |
173 delete filters.subscription; | |
174 break; | |
175 } | 271 } |
176 }); | 272 }); |
177 })(this); | 273 })(this); |
OLD | NEW |