1 /* ***** BEGIN LICENSE BLOCK *****
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Weave Sync for Adblock Plus.
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2009
19 * the Initial Developer. All Rights Reserved.
23 * ***** END LICENSE BLOCK ***** */
26 * This file is loaded by Startup.js component.
29 var abp = Components.classes["@mozilla.org/adblockplus;1"].createInstance().wrappedJSObject;
31 Components.utils.import("resource://weave/engines.js");
32 Components.utils.import("resource://weave/stores.js");
33 Components.utils.import("resource://weave/trackers.js");
34 Components.utils.import("resource://weave/base_records/crypto.js");
35 Components.utils.import("resource://weave/async.js");
36 Components.utils.import("resource://weave/util.js");
38 Function.prototype.async = Async.sugar;
40 function ABPRecord(uri)
42 CryptoWrapper.call(this, uri);
47 __proto__: CryptoWrapper.prototype,
48 _logname: "Record.AdblockPlus",
51 get source() this._source,
54 this._source = source;
56 if (source instanceof abp.Filter)
58 this.cleartext.type = "filter";
59 this.cleartext.text = source.text;
60 if (source instanceof abp.ActiveFilter)
61 this.cleartext.disabled = source.disabled;
63 else if (source instanceof abp.Subscription)
65 this.cleartext.type = "subscription";
66 this.cleartext.url = source.url;
67 this.cleartext.disabled = source.disabled;
68 if (source instanceof abp.RegularSubscription)
69 this.cleartext.title = source.title;
70 if (source instanceof abp.DownloadableSubscription)
71 this.cleartext.autoDownload = source.autoDownload;
73 else if (typeof source == "string")
75 this.cleartext.type = "pref";
76 this.cleartext.name = source;
77 this.cleartext.value = abp.prefs[source];
82 this.payload = null; // Weave 0.3 compatibility
88 switch (this.cleartext.type)
91 let filter = abp.Filter.fromText(this.cleartext.text);
95 if (filter instanceof abp.ActiveFilter && this.cleartext.disabled != filter.disabled)
97 filter.disabled = this.cleartext.disabled;
98 abp.filterStorage.triggerFilterObservers(filter.disabled ? "disable" : "enable", [filter]);
101 this._source = filter;
104 let subscription = abp.Subscription.fromURL(this.cleartext.url);
108 if (this.cleartext.disabled != subscription.disabled)
110 subscription.disabled = this.cleartext.disabled;
111 abp.filterStorage.triggerSubscriptionObservers(subscription.disabled ? "disable" : "enable", [subscription]);
115 if (subscription instanceof abp.RegularSubscription && this.cleartext.title != subscription.title)
117 subscription.title = this.cleartext.title;
120 if (subscription instanceof abp.DownloadableSubscription && this.cleartext.autoDownload != subscription.autoDownload)
122 subscription.autoDownload = this.cleartext.autoDownload;
126 abp.filterStorage.triggerSubscriptionObservers("updateinfo", [subscription]);
128 this._source = subscription;
131 let prefName = this.cleartext.name;
132 if (this.cleartext.value != abp.prefs[prefName])
134 abp.prefs[prefName] = this.cleartext.value;
138 this._source = prefName;
150 __proto__: Store.prototype,
151 _logName: "AdblockPlus",
154 createRecord: function(id, cryptoMetaURL)
156 let record = this.cache.get(id);
160 record = new ABPRecord();
162 record.encryption = cryptoMetaURL;
163 if (id in this._abpItems)
164 record.source = this._abpItems[id];
167 record.deleted = true;
168 record.payload = null; // Weave 0.3 compatibility
171 this.cache.put(id, record);
175 itemExists: function(id)
177 return (id in this._abpItems);
180 // This method shouldn't be called because Engine._recordLike() always returns false
181 changeItemID: function(oldId, newId) {},
183 getAllIDs: function()
186 for each (let subscription in abp.filterStorage.subscriptions)
188 if (!(subscription instanceof abp.ExternalSubscription))
189 result[Utils.sha1("subscription " + subscription.url)] = subscription;
191 if (subscription instanceof abp.SpecialSubscription)
193 for each (let filter in subscription.filters)
194 result[Utils.sha1("filter " + filter.text)] = filter;
197 for each (let [prefName, prefType, defaultValue] in abp.prefs.prefList)
198 if (prefName != "currentVersion")
199 result[Utils.sha1("pref " + prefName)] = prefName;
206 this._log.debug("Wiping all client data");
208 let subscriptions = abp.filterStorage.subscriptions.slice();
209 for each (let subscription in subscriptions)
211 if (subscription instanceof abp.SpecialSubscription)
212 abp.filterStorage.updateSubscriptionFilters(subscription, []);
213 else if (!(subscription instanceof abp.ExternalSubscription))
214 abp.filterStorage.removeSubscription(subscription);
217 for each (let [prefName, prefType, defaultValue] in abp.prefs.prefList)
219 if (prefName != "currentVersion")
220 abp.prefs[prefName] = defaultValue;
225 create: function(record)
227 this._log.debug("Got create command for " + record.id);
230 if (record.source instanceof abp.Filter)
231 abp.filterStorage.addFilter(record.source);
232 else if (record.source instanceof abp.DownloadableSubscription)
234 abp.filterStorage.addSubscription(record.source);
235 if (!record.source.lastDownload)
236 synchronizer.execute(record.source);
239 update: function(record)
241 this._log.debug("Got update command for " + record.id);
245 remove: function(record)
247 this._log.debug("Got remove command for " + record.id);
249 if (record.id in this._abpItems)
251 let item = this._abpItems[record.id];
252 if (item instanceof abp.Filter)
253 abp.filterStorage.removeFilter(item);
254 else if (item instanceof abp.DownloadableSubscription)
255 abp.filterStorage.removeSubscription(item);
259 cacheABPItems: function()
261 this._log.debug("Caching all ABP items");
262 this._abpItems = this.getAllIDs();
264 clearABPCache: function()
266 this._log.debug("Clearing ABP item cache");
267 this._abpItems = null;
271 function ABPTracker()
276 abp.prefs.addListener(function() { me.onPrefChange.apply(me, arguments); });
277 abp.filterStorage.addSubscriptionObserver(function() { me.onSubscriptionChange.apply(me, arguments); });
278 abp.filterStorage.addFilterObserver(function() { me.onFilterChange.apply(me, arguments); });
280 ABPTracker.prototype =
282 __proto__: Tracker.prototype,
283 _logName: "Tracker.AdblockPlus",
286 onPrefChange: function()
288 for each (let [prefName, prefType, defaultValue] in abp.prefs.prefList)
289 if (prefName != "currentVersion")
290 this.addChangedID(Utils.sha1("pref " + prefName));
294 onSubscriptionChange: function(action, subscriptions)
296 for each (let subscription in subscriptions)
298 if (!(subscription instanceof abp.ExternalSubscription))
299 this.addChangedID(Utils.sha1("subscription " + subscription.url));
301 if (subscription instanceof abp.SpecialSubscription && action == "update")
303 let oldFilters = {__proto__: null};
304 for each (let filter in subscription.oldFilters)
305 oldFilters[filter.text] = true;
306 let newFilters = {__proto__: null};
307 for each (let filter in subscription.filters)
308 newFilters[filter.text] = true;
310 for (let text in oldFilters)
311 if (!(text in newFilters))
312 this.addChangedID(Utils.sha1("filter " + text));
314 for (let text in newFilters)
315 if (!(text in oldFilters))
316 this.addChangedID(Utils.sha1("filter " + text));
322 onFilterChange: function(action, filters)
327 for each (let filter in filters)
328 this.addChangedID(Utils.sha1("filter " + filter.text));
335 SyncEngine.call(this);
337 ABPEngine.prototype =
339 __proto__: SyncEngine.prototype,
341 displayName: Cc["@mozilla.org/intl/stringbundle;1"]
342 .getService(Ci.nsIStringBundleService)
343 .createBundle("chrome://abpweavesync/locale/global.properties")
344 .GetStringFromName("dataProvider_name"),
345 logName: "AdblockPlus",
347 _trackerObj: ABPTracker,
348 _recordObj: ABPRecord,
350 _recordLike: function() false,
352 // Work-around for https://bugzilla.mozilla.org/show_bug.cgi?id=493256
353 _isEqual: function(item)
355 if (item.deleted || item.payload === null /* Weave 0.3 compatibility */)
358 return this.__proto__.__proto__._isEqual.apply(this, arguments);
361 _syncStartup: function()
364 this._store.cacheABPItems();
365 yield SyncEngine.prototype._syncStartup.async(this, self.cb);
367 _syncFinish: function()
370 this._store.clearABPCache();
371 yield SyncEngine.prototype._syncFinish.async(this, self.cb);
375 if ("isArray" in Utils)
376 Engines.register(ABPEngine);
378 Engines.register(new ABPEngine()); /* Weave 0.3 compatibility */