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

Side by Side Diff: lib/subscriptionInit.js

Issue 29805597: Issue 6699 - Support the "circumvention" filter list as default subscription (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created June 13, 2018, 3:33 a.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 | qunit/subscriptions.xml » ('j') | qunit/tests/subscriptionInit.js » ('J')
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 80
81 if (subscription instanceof SpecialSubscription && 81 if (subscription instanceof SpecialSubscription &&
82 subscription.filters.length > 0) 82 subscription.filters.length > 0)
83 return false; 83 return false;
84 } 84 }
85 85
86 return true; 86 return true;
87 } 87 }
88 88
89 /** 89 /**
90 * Finds the element for the default ad blocking filter subscription based 90 * Finds the element for the default ad blocking filter subscription based
Manish Jethani 2018/06/13 08:15:16 "element" and "subscription" should be plural now?
hub 2018/06/13 16:33:36 Done.
91 * on the user's locale. 91 * on the user's locale.
92 * 92 *
93 * @param {HTMLCollection} subscriptions 93 * @param {HTMLCollection} subscriptions
94 * @return {Element} 94 * @return {Object}
Manish Jethani 2018/06/13 08:15:16 We should probably return a Map here, since it is
hub 2018/06/13 16:33:37 Renamed it "DefaultSubscriptions"
95 */ 95 */
96 function chooseFilterSubscription(subscriptions) 96 function chooseFilterSubscriptions(subscriptions)
97 { 97 {
98 let selectedItem = null; 98 let selectedItem = {};
99 let selectedPrefix = null; 99 let selectedPrefix = null;
100 let matchCount = 0; 100 let matchCount = 0;
101 for (let subscription of subscriptions) 101 for (let subscription of subscriptions)
102 { 102 {
103 if (!selectedItem)
104 selectedItem = subscription;
105
106 let prefixes = subscription.getAttribute("prefixes"); 103 let prefixes = subscription.getAttribute("prefixes");
107 let prefix = prefixes && prefixes.split(",").find( 104 let prefix = prefixes && prefixes.split(",").find(
108 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale) 105 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale)
109 ); 106 );
110 107
111 let subscriptionType = subscription.getAttribute("type"); 108 let subscriptionType = subscription.getAttribute("type");
112 109
113 if (prefix && subscriptionType == "ads") 110 if (subscriptionType in ["ads", "cv"] && !selectedItem[subscriptionType])
Manish Jethani 2018/06/13 08:15:16 (This is a change in behavior, just noting. Previo
hub 2018/06/13 16:33:36 There is a guarantee to have an "ads" subscription
Manish Jethani 2018/06/13 16:53:09 Acknowledged.
111 selectedItem[subscriptionType] = subscription;
112
113 if (prefix)
114 { 114 {
115 if (!selectedPrefix || selectedPrefix.length < prefix.length) 115 // The "ads" subscription is the one driving the selection.
Manish Jethani 2018/06/13 08:15:16 Why only "ads", shouldn't this logic apply to all
Manish Jethani 2018/06/13 13:34:02 OK, I guess since the "cv" subscriptions are our o
hub 2018/06/13 16:33:36 Only "ads" and "cv" are relevant here. Also "cv" w
Manish Jethani 2018/06/13 16:53:09 Acknowledged.
116 if (subscriptionType == "ads")
116 { 117 {
117 selectedItem = subscription; 118 if (!selectedPrefix || selectedPrefix.length < prefix.length)
118 selectedPrefix = prefix; 119 {
119 matchCount = 1; 120 selectedItem[subscriptionType] = subscription;
121 selectedPrefix = prefix;
122 matchCount = 1;
123 }
124 else if (selectedPrefix && selectedPrefix.length == prefix.length)
125 {
126 matchCount++;
127
128 // If multiple items have a matching prefix of the same length:
Manish Jethani 2018/06/13 08:15:16 (Note: This comment is a bit misleading, because t
Manish Jethani 2018/06/13 09:42:29 Duh! Sorry, please ignore the above, it's rubbish.
hub 2018/06/13 16:33:37 Acknowledged.
129 // Select one of the items randomly, probability should be the same
130 // for all items. So we replace the previous match here with
131 // probability 1/N (N being the number of matches).
132 if (Math.random() * matchCount < 1)
133 {
134 selectedItem[subscriptionType] = subscription;
135 selectedPrefix = prefix;
136 }
137 }
120 } 138 }
121 else if (selectedPrefix && selectedPrefix.length == prefix.length) 139 else if (subscriptionType == "cv")
Manish Jethani 2018/06/13 08:15:16 (I notice that the XML file uses full names like "
hub 2018/06/13 16:33:36 It is not visible to the user. We have been using
Manish Jethani 2018/06/13 16:53:09 We had this discussion about the label to use on T
hub 2018/06/13 21:58:18 Done.
122 { 140 {
123 matchCount++; 141 selectedItem[subscriptionType] = subscription;
124
125 // If multiple items have a matching prefix of the same length:
126 // Select one of the items randomly, probability should be the same
127 // for all items. So we replace the previous match here with
128 // probability 1/N (N being the number of matches).
129 if (Math.random() * matchCount < 1)
130 {
131 selectedItem = subscription;
132 selectedPrefix = prefix;
133 }
134 } 142 }
135 } 143 }
136 } 144 }
137 return selectedItem; 145 return selectedItem;
138 } 146 }
139 147
140 function supportsNotificationsWithButtons() 148 function supportsNotificationsWithButtons()
141 { 149 {
142 // Microsoft Edge (as of EdgeHTML 16) doesn't have the notifications API. 150 // Microsoft Edge (as of EdgeHTML 16) doesn't have the notifications API.
143 // Opera gives an asynchronous error when buttons are provided (we cannot 151 // Opera gives an asynchronous error when buttons are provided (we cannot
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 // Add default ad blocking subscription (e.g. EasyList) 207 // Add default ad blocking subscription (e.g. EasyList)
200 if (shouldAddDefaultSubscription()) 208 if (shouldAddDefaultSubscription())
201 { 209 {
202 return fetch("subscriptions.xml") 210 return fetch("subscriptions.xml")
203 .then(response => response.text()) 211 .then(response => response.text())
204 .then(text => 212 .then(text =>
205 { 213 {
206 let doc = new DOMParser().parseFromString(text, "application/xml"); 214 let doc = new DOMParser().parseFromString(text, "application/xml");
207 let nodes = doc.getElementsByTagName("subscription"); 215 let nodes = doc.getElementsByTagName("subscription");
208 216
209 let node = chooseFilterSubscription(nodes); 217 let subs = chooseFilterSubscriptions(nodes);
Manish Jethani 2018/06/13 08:15:16 OK, so the return value of chooseFilterSubscriptio
hub 2018/06/13 16:33:37 An Object, not a Map.
Manish Jethani 2018/06/13 16:53:09 Yeah, I mean let's make it a Map. We have to itera
hub 2018/06/13 21:58:18 To iterate over two entries? In a function that mi
Manish Jethani 2018/06/14 05:07:33 OK, sure if you prefer it this way.
210 if (node) 218 if (subs)
211 { 219 {
212 let url = node.getAttribute("url"); 220 for (let name in subs)
213 if (url)
214 { 221 {
215 let subscription = Subscription.fromURL(url); 222 let node = subs[name];
216 subscription.disabled = false; 223 if (!node)
217 subscription.title = node.getAttribute("title"); 224 continue;
218 subscription.homepage = node.getAttribute("homepage"); 225
219 subscriptions.push(subscription); 226 let url = node.getAttribute("url");
227 if (url)
228 {
229 let subscription = Subscription.fromURL(url);
230 subscription.disabled = false;
231 subscription.title = node.getAttribute("title");
232 subscription.homepage = node.getAttribute("homepage");
233 subscription.type = node.getAttribute("type");
Manish Jethani 2018/06/13 08:15:16 Don't we have to add a type property to the Subscr
hub 2018/06/13 16:33:36 Added that to issue #6689 (patch not yet in review
Manish Jethani 2018/06/13 16:53:09 Acknowledged.
234 subscriptions.push(subscription);
235 }
220 } 236 }
221 } 237 }
222 238
223 return subscriptions; 239 return subscriptions;
224 }); 240 });
225 } 241 }
226 242
227 return subscriptions; 243 return subscriptions;
228 } 244 }
229 245
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 * Sets a callback that is called with an array of subscriptions to be added 315 * Sets a callback that is called with an array of subscriptions to be added
300 * during initialization. The callback must return an array of subscriptions 316 * during initialization. The callback must return an array of subscriptions
301 * that will effectively be added. 317 * that will effectively be added.
302 * 318 *
303 * @param {function} callback 319 * @param {function} callback
304 */ 320 */
305 exports.setSubscriptionsCallback = callback => 321 exports.setSubscriptionsCallback = callback =>
306 { 322 {
307 subscriptionsCallback = callback; 323 subscriptionsCallback = callback;
308 }; 324 };
325
326
Manish Jethani 2018/06/13 08:15:16 Let's use "//" for the comment here to clearly dis
hub 2018/06/13 16:33:36 Done.
327 /* Tests only
328 */
329 exports.chooseFilterSubscriptions = chooseFilterSubscriptions;
hub 2018/06/13 03:36:24 The only reason I'm exporting this is for the quni
OLDNEW
« no previous file with comments | « no previous file | qunit/subscriptions.xml » ('j') | qunit/tests/subscriptionInit.js » ('J')

Powered by Google App Engine
This is Rietveld