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

Delta Between Two Patch Sets: lib/subscriptionInit.js

Issue 29805597: Issue 6699 - Support the "circumvention" filter list as default subscription (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Created June 13, 2018, 3:33 a.m.
Right Patch Set: Use "==" instead of "in" Created June 19, 2018, 12:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | qunit/subscriptions.xml » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 * @typedef {object} DefaultSubscriptions
Manish Jethani 2018/06/13 08:15:16 "element" and "subscription" should be plural now?
hub 2018/06/13 16:33:36 Done.
91 * @property {?Element} ads
92 * @property {?Element} circumvention
93 */
94 /**
95 * Finds the elements for the default ad blocking filter subscriptions based
91 * on the user's locale. 96 * on the user's locale.
92 * 97 *
93 * @param {HTMLCollection} subscriptions 98 * @param {HTMLCollection} subscriptions
94 * @return {Object} 99 * @return {DefaultSubscriptions}
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 */ 100 */
96 function chooseFilterSubscriptions(subscriptions) 101 function chooseFilterSubscriptions(subscriptions)
97 { 102 {
98 let selectedItem = {}; 103 let selectedItem = {};
99 let selectedPrefix = null; 104 let selectedPrefix = null;
100 let matchCount = 0; 105 let matchCount = 0;
101 for (let subscription of subscriptions) 106 for (let subscription of subscriptions)
102 { 107 {
103 let prefixes = subscription.getAttribute("prefixes"); 108 let prefixes = subscription.getAttribute("prefixes");
104 let prefix = prefixes && prefixes.split(",").find( 109 let prefix = prefixes && prefixes.split(",").find(
105 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale) 110 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale)
106 ); 111 );
107 112
108 let subscriptionType = subscription.getAttribute("type"); 113 let subscriptionType = subscription.getAttribute("type");
109 114
110 if (subscriptionType in ["ads", "cv"] && !selectedItem[subscriptionType]) 115 if ((subscriptionType == "ads" || subscriptionType == "circumvention") &&
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.
116 !selectedItem[subscriptionType])
111 selectedItem[subscriptionType] = subscription; 117 selectedItem[subscriptionType] = subscription;
112 118
113 if (prefix) 119 if (prefix)
114 { 120 {
115 // The "ads" subscription is the one driving the selection. 121 // 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") 122 if (subscriptionType == "ads")
117 { 123 {
118 if (!selectedPrefix || selectedPrefix.length < prefix.length) 124 if (!selectedPrefix || selectedPrefix.length < prefix.length)
119 { 125 {
120 selectedItem[subscriptionType] = subscription; 126 selectedItem[subscriptionType] = subscription;
121 selectedPrefix = prefix; 127 selectedPrefix = prefix;
122 matchCount = 1; 128 matchCount = 1;
123 } 129 }
124 else if (selectedPrefix && selectedPrefix.length == prefix.length) 130 else if (selectedPrefix && selectedPrefix.length == prefix.length)
125 { 131 {
126 matchCount++; 132 matchCount++;
127 133
128 // If multiple items have a matching prefix of the same length: 134 // 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 135 // Select one of the items randomly, probability should be the same
130 // for all items. So we replace the previous match here with 136 // for all items. So we replace the previous match here with
131 // probability 1/N (N being the number of matches). 137 // probability 1/N (N being the number of matches).
132 if (Math.random() * matchCount < 1) 138 if (Math.random() * matchCount < 1)
133 { 139 {
134 selectedItem[subscriptionType] = subscription; 140 selectedItem[subscriptionType] = subscription;
135 selectedPrefix = prefix; 141 selectedPrefix = prefix;
136 } 142 }
137 } 143 }
138 } 144 }
139 else if (subscriptionType == "cv") 145 else if (subscriptionType == "circumvention")
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.
140 { 146 {
141 selectedItem[subscriptionType] = subscription; 147 selectedItem[subscriptionType] = subscription;
142 } 148 }
143 } 149 }
144 } 150 }
145 return selectedItem; 151 return selectedItem;
146 } 152 }
147 153
148 function supportsNotificationsWithButtons() 154 function supportsNotificationsWithButtons()
149 { 155 {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 // Add default ad blocking subscription (e.g. EasyList) 213 // Add default ad blocking subscription (e.g. EasyList)
208 if (shouldAddDefaultSubscription()) 214 if (shouldAddDefaultSubscription())
209 { 215 {
210 return fetch("subscriptions.xml") 216 return fetch("subscriptions.xml")
211 .then(response => response.text()) 217 .then(response => response.text())
212 .then(text => 218 .then(text =>
213 { 219 {
214 let doc = new DOMParser().parseFromString(text, "application/xml"); 220 let doc = new DOMParser().parseFromString(text, "application/xml");
215 let nodes = doc.getElementsByTagName("subscription"); 221 let nodes = doc.getElementsByTagName("subscription");
216 222
217 let subs = chooseFilterSubscriptions(nodes); 223 let defaultSubscriptions = 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.
218 if (subs) 224 if (defaultSubscriptions)
219 { 225 {
220 for (let name in subs) 226 for (let name in defaultSubscriptions)
221 { 227 {
222 let node = subs[name]; 228 let node = defaultSubscriptions[name];
223 if (!node) 229 if (!node)
224 continue; 230 continue;
225 231
226 let url = node.getAttribute("url"); 232 let url = node.getAttribute("url");
227 if (url) 233 if (url)
228 { 234 {
229 let subscription = Subscription.fromURL(url); 235 let subscription = Subscription.fromURL(url);
230 subscription.disabled = false; 236 subscription.disabled = false;
231 subscription.title = node.getAttribute("title"); 237 subscription.title = node.getAttribute("title");
232 subscription.homepage = node.getAttribute("homepage"); 238 subscription.homepage = node.getAttribute("homepage");
233 subscription.type = node.getAttribute("type"); 239 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); 240 subscriptions.push(subscription);
235 } 241 }
236 } 242 }
237 } 243 }
238 244
239 return subscriptions; 245 return subscriptions;
240 }); 246 });
241 } 247 }
242 248
243 return subscriptions; 249 return subscriptions;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 * during initialization. The callback must return an array of subscriptions 322 * during initialization. The callback must return an array of subscriptions
317 * that will effectively be added. 323 * that will effectively be added.
318 * 324 *
319 * @param {function} callback 325 * @param {function} callback
320 */ 326 */
321 exports.setSubscriptionsCallback = callback => 327 exports.setSubscriptionsCallback = callback =>
322 { 328 {
323 subscriptionsCallback = callback; 329 subscriptionsCallback = callback;
324 }; 330 };
325 331
326 332 // Exports for tests only
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; 333 exports.chooseFilterSubscriptions = chooseFilterSubscriptions;
hub 2018/06/13 03:36:24 The only reason I'm exporting this is for the quni
LEFTRIGHT

Powered by Google App Engine
This is Rietveld