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: More review fixes. Created June 14, 2018, 7:59 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 | qunit/subscriptions.xml » ('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-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
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 {Element} 99 * @return {DefaultSubscriptions}
95 */ 100 */
96 function chooseFilterSubscription(subscriptions) 101 function chooseFilterSubscriptions(subscriptions)
97 { 102 {
98 let selectedItem = null; 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 if (!selectedItem)
104 selectedItem = subscription;
105
106 let prefixes = subscription.getAttribute("prefixes"); 108 let prefixes = subscription.getAttribute("prefixes");
107 let prefix = prefixes && prefixes.split(",").find( 109 let prefix = prefixes && prefixes.split(",").find(
108 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale) 110 lang => new RegExp("^" + lang + "\\b").test(Utils.appLocale)
109 ); 111 );
110 112
111 let subscriptionType = subscription.getAttribute("type"); 113 let subscriptionType = subscription.getAttribute("type");
112 114
113 if (prefix && subscriptionType == "ads") 115 if (subscriptionType in ["ads", "circumvention"] &&
Manish Jethani 2018/06/19 09:23:30 We should probably just use == here instead of the
hub 2018/06/19 12:41:14 Done.
116 !selectedItem[subscriptionType])
117 selectedItem[subscriptionType] = subscription;
118
119 if (prefix)
114 { 120 {
115 if (!selectedPrefix || selectedPrefix.length < prefix.length) 121 // The "ads" subscription is the one driving the selection.
122 if (subscriptionType == "ads")
116 { 123 {
117 selectedItem = subscription; 124 if (!selectedPrefix || selectedPrefix.length < prefix.length)
118 selectedPrefix = prefix; 125 {
119 matchCount = 1; 126 selectedItem[subscriptionType] = subscription;
127 selectedPrefix = prefix;
128 matchCount = 1;
129 }
130 else if (selectedPrefix && selectedPrefix.length == prefix.length)
131 {
132 matchCount++;
133
134 // If multiple items have a matching prefix of the same length:
135 // Select one of the items randomly, probability should be the same
136 // for all items. So we replace the previous match here with
137 // probability 1/N (N being the number of matches).
138 if (Math.random() * matchCount < 1)
139 {
140 selectedItem[subscriptionType] = subscription;
141 selectedPrefix = prefix;
142 }
143 }
120 } 144 }
121 else if (selectedPrefix && selectedPrefix.length == prefix.length) 145 else if (subscriptionType == "circumvention")
122 { 146 {
123 matchCount++; 147 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 } 148 }
135 } 149 }
136 } 150 }
137 return selectedItem; 151 return selectedItem;
138 } 152 }
139 153
140 function supportsNotificationsWithButtons() 154 function supportsNotificationsWithButtons()
141 { 155 {
142 // Microsoft Edge (as of EdgeHTML 16) doesn't have the notifications API. 156 // Microsoft Edge (as of EdgeHTML 16) doesn't have the notifications API.
143 // Opera gives an asynchronous error when buttons are provided (we cannot 157 // 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) 213 // Add default ad blocking subscription (e.g. EasyList)
200 if (shouldAddDefaultSubscription()) 214 if (shouldAddDefaultSubscription())
201 { 215 {
202 return fetch("subscriptions.xml") 216 return fetch("subscriptions.xml")
203 .then(response => response.text()) 217 .then(response => response.text())
204 .then(text => 218 .then(text =>
205 { 219 {
206 let doc = new DOMParser().parseFromString(text, "application/xml"); 220 let doc = new DOMParser().parseFromString(text, "application/xml");
207 let nodes = doc.getElementsByTagName("subscription"); 221 let nodes = doc.getElementsByTagName("subscription");
208 222
209 let node = chooseFilterSubscription(nodes); 223 let defaultSubscriptions = chooseFilterSubscriptions(nodes);
210 if (node) 224 if (defaultSubscriptions)
211 { 225 {
212 let url = node.getAttribute("url"); 226 for (let name in defaultSubscriptions)
213 if (url)
214 { 227 {
215 let subscription = Subscription.fromURL(url); 228 let node = defaultSubscriptions[name];
216 subscription.disabled = false; 229 if (!node)
217 subscription.title = node.getAttribute("title"); 230 continue;
218 subscription.homepage = node.getAttribute("homepage"); 231
219 subscriptions.push(subscription); 232 let url = node.getAttribute("url");
233 if (url)
234 {
235 let subscription = Subscription.fromURL(url);
236 subscription.disabled = false;
237 subscription.title = node.getAttribute("title");
238 subscription.homepage = node.getAttribute("homepage");
239 subscription.type = node.getAttribute("type");
240 subscriptions.push(subscription);
241 }
220 } 242 }
221 } 243 }
222 244
223 return subscriptions; 245 return subscriptions;
224 }); 246 });
225 } 247 }
226 248
227 return subscriptions; 249 return subscriptions;
228 } 250 }
229 251
(...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 321 * 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 322 * during initialization. The callback must return an array of subscriptions
301 * that will effectively be added. 323 * that will effectively be added.
302 * 324 *
303 * @param {function} callback 325 * @param {function} callback
304 */ 326 */
305 exports.setSubscriptionsCallback = callback => 327 exports.setSubscriptionsCallback = callback =>
306 { 328 {
307 subscriptionsCallback = callback; 329 subscriptionsCallback = callback;
308 }; 330 };
331
332 // Exports for tests only
333 exports.chooseFilterSubscriptions = chooseFilterSubscriptions;
OLDNEW
« no previous file with comments | « no previous file | qunit/subscriptions.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld