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

Side by Side Diff: lib/subscriptionClasses.js

Issue 29336491: Issue 3660 - Remove locale dependency from subscription classes (Closed)
Patch Set: Created Feb. 16, 2016, 12:23 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 | « no previous file | test/subscriptionClasses.js » ('j') | 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
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 /** 18 /**
19 * @fileOverview Definition of Subscription class and its subclasses. 19 * @fileOverview Definition of Subscription class and its subclasses.
20 */ 20 */
21 21
22 Cu.import("resource://gre/modules/Services.jsm");
23
24 let {ActiveFilter, BlockingFilter, WhitelistFilter, ElemHideBase} = require("fil terClasses"); 22 let {ActiveFilter, BlockingFilter, WhitelistFilter, ElemHideBase} = require("fil terClasses");
25 let {FilterNotifier} = require("filterNotifier"); 23 let {FilterNotifier} = require("filterNotifier");
26 24
27 /** 25 /**
28 * Abstract base class for filter subscriptions 26 * Abstract base class for filter subscriptions
29 * 27 *
30 * @param {String} url download location of the subscription 28 * @param {String} url download location of the subscription
31 * @param {String} [title] title of the filter subscription 29 * @param {String} [title] title of the filter subscription
32 * @constructor 30 * @constructor
33 */ 31 */
34 function Subscription(url, title) 32 function Subscription(url, title)
35 { 33 {
36 this.url = url; 34 this.url = url;
37 this.filters = []; 35 this.filters = [];
38 if (title) 36 if (title)
39 this._title = title; 37 this._title = title;
40 else
41 {
42 let {Utils} = require("utils");
43 this._title = Utils.getString("newGroup_title");
44 }
45 Subscription.knownSubscriptions[url] = this; 38 Subscription.knownSubscriptions[url] = this;
46 } 39 }
47 exports.Subscription = Subscription; 40 exports.Subscription = Subscription;
48 41
49 Subscription.prototype = 42 Subscription.prototype =
50 { 43 {
51 /** 44 /**
52 * Download location of the subscription 45 * Download location of the subscription
53 * @type String 46 * @type String
54 */ 47 */
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 }, 115 },
123 116
124 /** 117 /**
125 * Serializes the subscription to an array of strings for writing out on the d isk. 118 * Serializes the subscription to an array of strings for writing out on the d isk.
126 * @param {string[]} buffer buffer to push the serialization results into 119 * @param {string[]} buffer buffer to push the serialization results into
127 */ 120 */
128 serialize: function(buffer) 121 serialize: function(buffer)
129 { 122 {
130 buffer.push("[Subscription]"); 123 buffer.push("[Subscription]");
131 buffer.push("url=" + this.url); 124 buffer.push("url=" + this.url);
132 buffer.push("title=" + this._title); 125 if (this._title)
126 buffer.push("title=" + this._title);
133 if (this._fixedTitle) 127 if (this._fixedTitle)
134 buffer.push("fixedTitle=true"); 128 buffer.push("fixedTitle=true");
135 if (this._disabled) 129 if (this._disabled)
136 buffer.push("disabled=true"); 130 buffer.push("disabled=true");
137 }, 131 },
138 132
139 serializeFilters: function(buffer) 133 serializeFilters: function(buffer)
140 { 134 {
141 for (let filter of this.filters) 135 for (let filter of this.filters)
142 buffer.push(filter.text.replace(/\[/g, "\\[")); 136 buffer.push(filter.text.replace(/\[/g, "\\["));
(...skipping 16 matching lines...) Expand all
159 /** 153 /**
160 * Returns a subscription from its URL, creates a new one if necessary. 154 * Returns a subscription from its URL, creates a new one if necessary.
161 * @param {String} url URL of the subscription 155 * @param {String} url URL of the subscription
162 * @return {Subscription} subscription or null if the subscription couldn't be c reated 156 * @return {Subscription} subscription or null if the subscription couldn't be c reated
163 */ 157 */
164 Subscription.fromURL = function(url) 158 Subscription.fromURL = function(url)
165 { 159 {
166 if (url in Subscription.knownSubscriptions) 160 if (url in Subscription.knownSubscriptions)
167 return Subscription.knownSubscriptions[url]; 161 return Subscription.knownSubscriptions[url];
168 162
169 try 163 if (url[0] != "~")
170 {
171 // Test URL for validity
172 url = Services.io.newURI(url, null, null).spec;
173 return new DownloadableSubscription(url, null); 164 return new DownloadableSubscription(url, null);
174 } 165 else
175 catch (e)
176 {
177 return new SpecialSubscription(url); 166 return new SpecialSubscription(url);
178 }
179 }; 167 };
180 168
181 /** 169 /**
182 * Deserializes a subscription 170 * Deserializes a subscription
183 * 171 *
184 * @param {Object} obj map of serialized properties and their values 172 * @param {Object} obj map of serialized properties and their values
185 * @return {Subscription} subscription or null if the subscription couldn't be c reated 173 * @return {Subscription} subscription or null if the subscription couldn't be c reated
186 */ 174 */
187 Subscription.fromObject = function(obj) 175 Subscription.fromObject = function(obj)
188 { 176 {
189 let result; 177 let result;
190 try 178 if (obj.url[0] != "~")
191 { 179 {
192 obj.url = Services.io.newURI(obj.url, null, null).spec;
193
194 // URL is valid - this is a downloadable subscription 180 // URL is valid - this is a downloadable subscription
195 result = new DownloadableSubscription(obj.url, obj.title); 181 result = new DownloadableSubscription(obj.url, obj.title);
196 if ("downloadStatus" in obj) 182 if ("downloadStatus" in obj)
197 result._downloadStatus = obj.downloadStatus; 183 result._downloadStatus = obj.downloadStatus;
198 if ("lastSuccess" in obj) 184 if ("lastSuccess" in obj)
199 result.lastSuccess = parseInt(obj.lastSuccess, 10) || 0; 185 result.lastSuccess = parseInt(obj.lastSuccess, 10) || 0;
200 if ("lastCheck" in obj) 186 if ("lastCheck" in obj)
201 result._lastCheck = parseInt(obj.lastCheck, 10) || 0; 187 result._lastCheck = parseInt(obj.lastCheck, 10) || 0;
202 if ("expires" in obj) 188 if ("expires" in obj)
203 result.expires = parseInt(obj.expires, 10) || 0; 189 result.expires = parseInt(obj.expires, 10) || 0;
204 if ("softExpiration" in obj) 190 if ("softExpiration" in obj)
205 result.softExpiration = parseInt(obj.softExpiration, 10) || 0; 191 result.softExpiration = parseInt(obj.softExpiration, 10) || 0;
206 if ("errors" in obj) 192 if ("errors" in obj)
207 result._errors = parseInt(obj.errors, 10) || 0; 193 result._errors = parseInt(obj.errors, 10) || 0;
208 if ("version" in obj) 194 if ("version" in obj)
209 result.version = parseInt(obj.version, 10) || 0; 195 result.version = parseInt(obj.version, 10) || 0;
210 if ("requiredVersion" in obj) 196 if ("requiredVersion" in obj)
211 {
212 let {addonVersion} = require("info");
213 result.requiredVersion = obj.requiredVersion; 197 result.requiredVersion = obj.requiredVersion;
214 if (Services.vc.compare(result.requiredVersion, addonVersion) > 0)
215 result.upgradeRequired = true;
216 }
217 if ("homepage" in obj) 198 if ("homepage" in obj)
218 result._homepage = obj.homepage; 199 result._homepage = obj.homepage;
219 if ("lastDownload" in obj) 200 if ("lastDownload" in obj)
220 result._lastDownload = parseInt(obj.lastDownload, 10) || 0; 201 result._lastDownload = parseInt(obj.lastDownload, 10) || 0;
221 if ("downloadCount" in obj) 202 if ("downloadCount" in obj)
222 result.downloadCount = parseInt(obj.downloadCount, 10) || 0; 203 result.downloadCount = parseInt(obj.downloadCount, 10) || 0;
223 } 204 }
224 catch (e) 205 else
225 { 206 {
226 // Invalid URL - custom filter group
227 if (!("title" in obj))
228 {
229 // Backwards compatibility - titles and filter types were originally
230 // determined by group identifier.
231 if (obj.url == "~wl~")
232 obj.defaults = "whitelist";
233 else if (obj.url == "~fl~")
234 obj.defaults = "blocking";
235 else if (obj.url == "~eh~")
236 obj.defaults = "elemhide";
237 if ("defaults" in obj)
238 {
239 let {Utils} = require("utils");
240 obj.title = Utils.getString(obj.defaults + "Group_title");
241 }
242 }
243 result = new SpecialSubscription(obj.url, obj.title); 207 result = new SpecialSubscription(obj.url, obj.title);
244 if ("defaults" in obj) 208 if ("defaults" in obj)
245 result.defaults = obj.defaults.split(" "); 209 result.defaults = obj.defaults.split(" ");
246 } 210 }
247 if ("fixedTitle" in obj) 211 if ("fixedTitle" in obj)
248 result._fixedTitle = (obj.fixedTitle == "true"); 212 result._fixedTitle = (obj.fixedTitle == "true");
249 if ("disabled" in obj) 213 if ("disabled" in obj)
250 result._disabled = (obj.disabled == "true"); 214 result._disabled = (obj.disabled == "true");
251 215
252 return result; 216 return result;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 { 304 {
341 let subscription = SpecialSubscription.create(); 305 let subscription = SpecialSubscription.create();
342 subscription.filters.push(filter); 306 subscription.filters.push(filter);
343 for (let type in SpecialSubscription.defaultsMap) 307 for (let type in SpecialSubscription.defaultsMap)
344 { 308 {
345 if (filter instanceof SpecialSubscription.defaultsMap[type]) 309 if (filter instanceof SpecialSubscription.defaultsMap[type])
346 subscription.defaults = [type]; 310 subscription.defaults = [type];
347 } 311 }
348 if (!subscription.defaults) 312 if (!subscription.defaults)
349 subscription.defaults = ["blocking"]; 313 subscription.defaults = ["blocking"];
350
351 let {Utils} = require("utils");
352 subscription.title = Utils.getString(subscription.defaults[0] + "Group_title") ;
353 return subscription; 314 return subscription;
354 }; 315 };
355 316
356 /** 317 /**
357 * Abstract base class for regular filter subscriptions (both internally and ext ernally updated) 318 * Abstract base class for regular filter subscriptions (both internally and ext ernally updated)
358 * @param {String} url see Subscription() 319 * @param {String} url see Subscription()
359 * @param {String} [title] see Subscription() 320 * @param {String} [title] see Subscription()
360 * @constructor 321 * @constructor
361 * @augments Subscription 322 * @augments Subscription
362 */ 323 */
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 */ 512 */
552 version: 0, 513 version: 0,
553 514
554 /** 515 /**
555 * Minimal Adblock Plus version required for this subscription 516 * Minimal Adblock Plus version required for this subscription
556 * @type String 517 * @type String
557 */ 518 */
558 requiredVersion: null, 519 requiredVersion: null,
559 520
560 /** 521 /**
561 * Should be true if requiredVersion is higher than current Adblock Plus versi on
562 * @type Boolean
563 */
564 upgradeRequired: false,
565
566 /**
567 * Number indicating how often the object was downloaded. 522 * Number indicating how often the object was downloaded.
568 * @type Number 523 * @type Number
569 */ 524 */
570 downloadCount: 0, 525 downloadCount: 0,
571 526
572 /** 527 /**
573 * See Subscription.serialize() 528 * See Subscription.serialize()
574 */ 529 */
575 serialize: function(buffer) 530 serialize: function(buffer)
576 { 531 {
(...skipping 11 matching lines...) Expand all
588 if (this.errors) 543 if (this.errors)
589 buffer.push("errors=" + this.errors); 544 buffer.push("errors=" + this.errors);
590 if (this.version) 545 if (this.version)
591 buffer.push("version=" + this.version); 546 buffer.push("version=" + this.version);
592 if (this.requiredVersion) 547 if (this.requiredVersion)
593 buffer.push("requiredVersion=" + this.requiredVersion); 548 buffer.push("requiredVersion=" + this.requiredVersion);
594 if (this.downloadCount) 549 if (this.downloadCount)
595 buffer.push("downloadCount=" + this.downloadCount); 550 buffer.push("downloadCount=" + this.downloadCount);
596 } 551 }
597 }; 552 };
OLDNEW
« no previous file with comments | « no previous file | test/subscriptionClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld