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

Side by Side Diff: lib/utils.js

Issue 10862008: Automatically add default filter subscription when necessary. (Closed)
Patch Set: Created May 27, 2013, 2:12 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 | « lib/init.js ('k') | shell/src/Main.cpp » ('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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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 13 matching lines...) Expand all
24 runAsync: function(callback, thisPtr) 24 runAsync: function(callback, thisPtr)
25 { 25 {
26 var params = Array.prototype.slice.call(arguments, 2); 26 var params = Array.prototype.slice.call(arguments, 2);
27 window.setTimeout(function() 27 window.setTimeout(function()
28 { 28 {
29 callback.apply(thisPtr, params); 29 callback.apply(thisPtr, params);
30 }, 0); 30 }, 0);
31 }, 31 },
32 get appLocale() 32 get appLocale()
33 { 33 {
34 var locale = chrome.i18n.getMessage("@@ui_locale").replace(/_/g, "-"); 34 return _appInfo.locale;
35 this.__defineGetter__("appLocale", function() {return locale});
36 return this.appLocale;
37 }, 35 },
38 generateChecksum: function(lines) 36 generateChecksum: function(lines)
39 { 37 {
40 // We cannot calculate MD5 checksums yet :-( 38 // We cannot calculate MD5 checksums yet :-(
41 return null; 39 return null;
42 }, 40 },
43 makeURI: function(url) 41 makeURI: function(url)
44 { 42 {
45 return Services.io.newURI(url); 43 return Services.io.newURI(url);
46 }, 44 },
47 45
48 checkLocalePrefixMatch: function(prefixes) 46 checkLocalePrefixMatch: function(prefixes)
49 { 47 {
50 if (!prefixes) 48 if (!prefixes)
51 return null; 49 return null;
52 50
53 var list = prefixes.split(","); 51 let list = prefixes.split(",");
54 for (var i = 0; i < list.length; i++) 52 for each (let prefix in list)
55 if (new RegExp("^" + list[i] + "\\b").test(this.appLocale)) 53 if (new RegExp("^" + prefix + "\\b").test(this.appLocale))
56 return list[i]; 54 return prefix;
57 55
58 return null; 56 return null;
59 }, 57 },
60 58
61 chooseFilterSubscription: function(subscriptions) 59 chooseFilterSubscription: function(subscriptions)
62 { 60 {
63 var selectedItem = null; 61 let selectedItem = null;
64 var selectedPrefix = null; 62 let selectedPrefix = null;
65 var matchCount = 0; 63 let matchCount = 0;
66 for (var i = 0; i < subscriptions.length; i++) 64 for (let i = 0; i < subscriptions.length; i++)
67 { 65 {
68 var subscription = subscriptions[i]; 66 let subscription = subscriptions[i];
69 if (!selectedItem) 67 if (!selectedItem)
70 selectedItem = subscription; 68 selectedItem = subscription;
71 69
72 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes")); 70 let prefix = this.checkLocalePrefixMatch(subscription.prefixes);
73 if (prefix) 71 if (prefix)
74 { 72 {
75 if (!selectedPrefix || selectedPrefix.length < prefix.length) 73 if (!selectedPrefix || selectedPrefix.length < prefix.length)
76 { 74 {
77 selectedItem = subscription; 75 selectedItem = subscription;
78 selectedPrefix = prefix; 76 selectedPrefix = prefix;
79 matchCount = 1; 77 matchCount = 1;
80 } 78 }
81 else if (selectedPrefix && selectedPrefix.length == prefix.length) 79 else if (selectedPrefix && selectedPrefix.length == prefix.length)
82 { 80 {
83 matchCount++; 81 matchCount++;
84 82
85 // If multiple items have a matching prefix of the same length: 83 // If multiple items have a matching prefix of the same length:
86 // Select one of the items randomly, probability should be the same 84 // Select one of the items randomly, probability should be the same
87 // for all items. So we replace the previous match here with 85 // for all items. So we replace the previous match here with
88 // probability 1/N (N being the number of matches). 86 // probability 1/N (N being the number of matches).
89 if (Math.random() * matchCount < 1) 87 if (Math.random() * matchCount < 1)
90 { 88 {
91 selectedItem = subscription; 89 selectedItem = subscription;
92 selectedPrefix = prefix; 90 selectedPrefix = prefix;
93 } 91 }
94 } 92 }
95 } 93 }
96 } 94 }
97 return selectedItem; 95 return selectedItem;
98 } 96 }
99 }; 97 };
OLDNEW
« no previous file with comments | « lib/init.js ('k') | shell/src/Main.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld