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

Delta Between Two Patch Sets: lib/elemHide.js

Issue 4529242486341632: Issue 235 - Improved performance of ElemHide.getSelectorsForDomain() (Closed)
Left Patch Set: Created Jan. 22, 2015, 1 p.m.
Right Patch Set: Always consider generic filters Created July 2, 2015, 9:22 a.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 | « lib/contentPolicy.js ('k') | no next file » | 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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 */ 52 */
53 let exceptions = Object.create(null); 53 let exceptions = Object.create(null);
54 54
55 /** 55 /**
56 * Currently applied stylesheet URL 56 * Currently applied stylesheet URL
57 * @type nsIURI 57 * @type nsIURI
58 */ 58 */
59 let styleURL = null; 59 let styleURL = null;
60 60
61 /** 61 /**
62 * Lookup table, blocking filters by domain which they are applied to 62 * Lookup table, filter selectors by domain which they are applied to
63 * @type Object 63 * @type Object
64 */ 64 */
65 let filtersByDomain = Object.create(null); 65 let selectorsByDomain = Object.create(null);
66
67 /**
68 * Indicates whether stylesheets can be used for element hiding
69 * @type Boolean
70 */
71 let canUseStylesheets = ("nsIStyleSheetService" in Ci);
66 72
67 /** 73 /**
68 * Element hiding component 74 * Element hiding component
69 * @class 75 * @class
70 */ 76 */
71 let ElemHide = exports.ElemHide = 77 let ElemHide = exports.ElemHide =
72 { 78 {
73 /** 79 /**
74 * Indicates whether filters have been added or removed since the last apply() call. 80 * Indicates whether filters have been added or removed since the last apply() call.
75 * @type Boolean 81 * @type Boolean
(...skipping 28 matching lines...) Expand all
104 110
105 /** 111 /**
106 * Removes all known filters 112 * Removes all known filters
107 */ 113 */
108 clear: function() 114 clear: function()
109 { 115 {
110 filterByKey = Object.create(null); 116 filterByKey = Object.create(null);
111 keyByFilter = Object.create(null); 117 keyByFilter = Object.create(null);
112 knownExceptions = Object.create(null); 118 knownExceptions = Object.create(null);
113 exceptions = Object.create(null); 119 exceptions = Object.create(null);
114 filtersByDomain = Object.create(null); 120 selectorsByDomain = Object.create(null);
115 ElemHide.isDirty = false; 121 ElemHide.isDirty = false;
116 ElemHide.unapply(); 122 ElemHide.unapply();
117 }, 123 },
118 124
119 /** 125 /**
120 * Add a new element hiding filter 126 * Add a new element hiding filter
121 * @param {ElemHideFilter} filter 127 * @param {ElemHideFilter} filter
122 */ 128 */
123 add: function(filter) 129 add: function(filter)
124 { 130 {
125 if (filter instanceof ElemHideException) 131 if (filter instanceof ElemHideException)
126 { 132 {
127 if (filter.text in knownExceptions) 133 if (filter.text in knownExceptions)
128 return; 134 return;
129 135
130 let selector = filter.selector; 136 let selector = filter.selector;
131 if (!(selector in exceptions)) 137 if (!(selector in exceptions))
132 exceptions[selector] = []; 138 exceptions[selector] = [];
133 exceptions[selector].push(filter); 139 exceptions[selector].push(filter);
134 knownExceptions[filter.text] = true; 140 knownExceptions[filter.text] = true;
135 } 141 }
136 else 142 else
137 { 143 {
138 if (filter.text in keyByFilter) 144 if (filter.text in keyByFilter)
139 return; 145 return;
140 146
141 if (!("nsIStyleSheetService" in Ci)) 147 if (!canUseStylesheets)
142 { 148 {
143 let domains = filter.domains; 149 let domains = filter.domains;
144 if (domains === null) 150 if (domains === null)
145 { 151 {
146 domains = Object.create(null); 152 domains = Object.create(null);
147 domains[""] = undefined; 153 domains[""] = true;
148 } 154 }
149 155
150 for (let domain in domains) 156 for (let domain in domains)
151 { 157 {
152 if (!(domain in filtersByDomain)) 158 if (!(domain in selectorsByDomain))
153 filtersByDomain[domain] = Object.create(null); 159 {
154 160 selectorsByDomain[domain] = Object.create(null);
155 filtersByDomain[domain][filter.text] = filter.isActiveOnDomain(domain) ; 161 selectorsByDomain[domain].$length = 0;
162 }
163
164 let selectors = selectorsByDomain[domain];
165 if (!(filter.selector in selectors))
166 {
167 selectors[filter.selector] = {
168 isActive: false,
169 count: 0
170 };
171 }
172 selectors[filter.selector].isActive |= domains[domain];
173 selectors[filter.selector].count++;
174
175 selectors.$length++;
156 } 176 }
157 } 177 }
158 178
159 let key; 179 let key;
160 do { 180 do {
161 key = Math.random().toFixed(15).substr(5); 181 key = Math.random().toFixed(15).substr(5);
162 } while (key in filterByKey); 182 } while (key in filterByKey);
163 183
164 filterByKey[key] = filter; 184 filterByKey[key] = filter;
165 keyByFilter[filter.text] = key; 185 keyByFilter[filter.text] = key;
(...skipping 16 matching lines...) Expand all
182 let index = list.indexOf(filter); 202 let index = list.indexOf(filter);
183 if (index >= 0) 203 if (index >= 0)
184 list.splice(index, 1); 204 list.splice(index, 1);
185 delete knownExceptions[filter.text]; 205 delete knownExceptions[filter.text];
186 } 206 }
187 else 207 else
188 { 208 {
189 if (!(filter.text in keyByFilter)) 209 if (!(filter.text in keyByFilter))
190 return; 210 return;
191 211
192 if (!("nsIStyleSheetService" in Ci)) 212 if (!canUseStylesheets)
193 { 213 {
194 for (let domain in filter.domains) 214 let domains = filter.domains;
195 { 215 if (!domains)
196 if (domain in filtersByDomain && filter.text in filtersByDomain[domain ]) 216 {
197 delete filtersByDomain[domain][filter.text]; 217 domains = Object.create(null);
198 218 domains[""] = true;
199 if (!Object.keys(filtersByDomain[domain]).length) 219 }
200 delete filtersByDomain[domain]; 220
221 for (let domain in domains)
222 {
223 if (domain in selectorsByDomain
224 && filter.selector in selectorsByDomain[domain]
225 && !--selectorsByDomain[domain][filter.selector].$length)
226 {
227 delete selectorsByDomain[domain][filter.selector];
228 if (!--selectorsByDomain[domain].$length)
229 delete selectorsByDomain[domain];
230 }
201 } 231 }
202 } 232 }
203 233
204 let key = keyByFilter[filter.text]; 234 let key = keyByFilter[filter.text];
205 delete filterByKey[key]; 235 delete filterByKey[key];
206 delete keyByFilter[filter.text]; 236 delete keyByFilter[filter.text];
207 ElemHide.isDirty = true; 237 ElemHide.isDirty = true;
208 } 238 }
209 }, 239 },
210 240
211 /** 241 /**
212 * Checks whether an exception rule is registered for a filter on a particular 242 * Checks whether an exception rule is registered for a filter on a particular
213 * domain. 243 * domain.
214 */ 244 */
215 getException: function(/**Filter*/ filter, /**String*/ docDomain) /**ElemHideE xception*/ 245 getException: function(/**String*/ selector, /**String*/ docDomain) /**ElemHid eException*/
216 { 246 {
217 if (!(filter.selector in exceptions)) 247 if (!(selector in exceptions))
218 return null; 248 return null;
219 249
220 let list = exceptions[filter.selector]; 250 let list = exceptions[selector];
221 for (let i = list.length - 1; i >= 0; i--) 251 for (let i = list.length - 1; i >= 0; i--)
222 if (list[i].isActiveOnDomain(docDomain)) 252 if (list[i].isActiveOnDomain(docDomain))
223 return list[i]; 253 return list[i];
224 254
225 return null; 255 return null;
226 }, 256 },
227 257
228 /** 258 /**
229 * Will be set to true if apply() is running (reentrance protection). 259 * Will be set to true if apply() is running (reentrance protection).
230 * @type Boolean 260 * @type Boolean
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 } 342 }
313 } 343 }
314 344
315 FilterNotifier.triggerListeners("elemhideupdate"); 345 FilterNotifier.triggerListeners("elemhideupdate");
316 } 346 }
317 }.bind(this)); 347 }.bind(this));
318 348
319 this._applying = true; 349 this._applying = true;
320 }, 350 },
321 351
322 _generateCSSContent: function() 352 _generateCSSContent: function*()
323 { 353 {
324 // Grouping selectors by domains 354 // Grouping selectors by domains
325 let domains = Object.create(null); 355 let domains = Object.create(null);
326 let hasFilters = false; 356 let hasFilters = false;
327 for (let key in filterByKey) 357 for (let key in filterByKey)
328 { 358 {
329 let filter = filterByKey[key]; 359 let filter = filterByKey[key];
330 let domain = filter.selectorDomain || ""; 360 let domain = filter.selectorDomain || "";
331 361
332 let list; 362 let list;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 432
403 /** 433 /**
404 * Retrieves an element hiding filter by the corresponding protocol key 434 * Retrieves an element hiding filter by the corresponding protocol key
405 */ 435 */
406 getFilterByKey: function(/**String*/ key) /**Filter*/ 436 getFilterByKey: function(/**String*/ key) /**Filter*/
407 { 437 {
408 return (key in filterByKey ? filterByKey[key] : null); 438 return (key in filterByKey ? filterByKey[key] : null);
409 }, 439 },
410 440
411 /** 441 /**
412 * Returns a list of all selectors active on a particular domain (currently 442 * Returns a list of all selectors active on a particular domain (it cannot
413 * used only in Chrome, Opera and Safari). 443 * be used in Firefox).
414 */ 444 */
415 getSelectorsForDomain: function(/**String*/ domain, /**Boolean*/ specificOnly) 445 getSelectorsForDomain: function(/**String*/ docDomain, /**Boolean*/ specificOn ly)
416 { 446 {
417 let selectors = []; 447 if (canUseStylesheets)
418 domain = domain.toUpperCase(); 448 throw new Error("Use of getSelectorsForDomain is limited to platforms whic h cannot inject stylesheets");
419 449
420 if (!specificOnly) 450 let selectors = Object.create(null);
421 { 451 let domain = docDomain.toUpperCase();
422 let filterTexts = filtersByDomain[""];
423 if (filterTexts)
424 {
425 for (let filterText in filterTexts)
426 {
427 if (filterTexts[filterText])
428 {
429 let filter = filterByKey[keyByFilter[filterText]];
430 if (!filter)
431 continue;
432
433 let selector = filter.selector;
434 if (filter.isActiveOnDomain(docDomain) && !this.getException(selecto r, docDomain))
435 selectors[selectors.length] = selector;
436 }
437 }
438 }
439 }
440
441 while (true) 452 while (true)
442 { 453 {
443 if (domain && domain in filtersByDomain) 454 if (domain in selectorsByDomain && (domain != "" || !specificOnly))
444 { 455 {
445 let filterTexts = filtersByDomain[domain]; 456 let domainSelectors = selectorsByDomain[domain];
446 if (filterTexts) 457 let filterSelectors = Object.getOwnPropertyNames(domainSelectors);
447 { 458 for (let filterSelector of filterSelectors)
448 for (let filterText in filterTexts) 459 {
449 { 460 if (filterSelector == "$length"
450 if (filterTexts[filterText]) 461 || filterSelector in selectors
451 { 462 || !domainSelectors[filterSelector].isActive
452 let filter = filterByKey[keyByFilter[filterText]]; 463 || this.getException(filterSelector, docDomain))
453 if (!filter) 464 continue;
Wladimir Palant 2015/07/30 11:26:53 Even with the inconsistent logic changes you are i
454 continue; 465
455 466 selectors[filterSelector] = null;
456 let selector = filter.selector; 467 }
457 if (!this.getException(selector, docDomain)) 468 }
458 selectors[selectors.length] = selector; 469
459 } 470 if (domain == "")
460 } 471 break;
461 }
462 }
463 472
464 let nextDot = domain.indexOf("."); 473 let nextDot = domain.indexOf(".");
465 if (nextDot < 0) 474 domain = (nextDot < 0) ? "" : domain.substr(nextDot + 1);
466 break; 475 }
467 domain = domain.substr(nextDot + 1); 476
468 } 477 return Object.getOwnPropertyNames(selectors);
469
470 return selectors;
471 } 478 }
472 }; 479 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld