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

Side by Side Diff: lib/elemHide.js

Issue 6201308310667264: Issue 521- Inject our stylesheet on per-site basis rather than globally (Closed)
Patch Set: Wladimir what do you think? I think I have seen the case happen once where we observe 'content-docu… Created July 18, 2014, 3:47 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/contentPolicy.js ('k') | no next file » | 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-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 10 matching lines...) Expand all
21 21
22 Cu.import("resource://gre/modules/Services.jsm"); 22 Cu.import("resource://gre/modules/Services.jsm");
23 23
24 let {Utils} = require("utils"); 24 let {Utils} = require("utils");
25 let {IO} = require("io"); 25 let {IO} = require("io");
26 let {Prefs} = require("prefs"); 26 let {Prefs} = require("prefs");
27 let {ElemHideException} = require("filterClasses"); 27 let {ElemHideException} = require("filterClasses");
28 let {FilterNotifier} = require("filterNotifier"); 28 let {FilterNotifier} = require("filterNotifier");
29 let {AboutHandler} = require("elemHideHitRegistration"); 29 let {AboutHandler} = require("elemHideHitRegistration");
30 let {TimeLine} = require("timeline"); 30 let {TimeLine} = require("timeline");
31 let Policy = null;
31 32
32 /** 33 /**
33 * Lookup table, filters by their associated key 34 * Lookup table, filters by their associated key
34 * @type Object 35 * @type Object
35 */ 36 */
36 let filterByKey = Object.create(null); 37 let filterByKey = Object.create(null);
37 38
38 /** 39 /**
39 * Lookup table, keys of the filters by filter text 40 * Lookup table, keys of the filters by filter text
40 * @type Object 41 * @type Object
(...skipping 12 matching lines...) Expand all
53 */ 54 */
54 let exceptions = Object.create(null); 55 let exceptions = Object.create(null);
55 56
56 /** 57 /**
57 * Currently applied stylesheet URL 58 * Currently applied stylesheet URL
58 * @type nsIURI 59 * @type nsIURI
59 */ 60 */
60 let styleURL = null; 61 let styleURL = null;
61 62
62 /** 63 /**
64 * Global stylesheet that should be loaded into content windows.
65 * @type nsIStyleSheet
66 */
67 let styleSheet = null;
68
69 /**
70 * Use new way of injecting styles per Window that exists since Firefox 33.
71 * @type boolean
72 */
73 let useNew = ('preloadSheet' in Utils.styleService);
74
75 /**
63 * Element hiding component 76 * Element hiding component
64 * @class 77 * @class
65 */ 78 */
66 let ElemHide = exports.ElemHide = 79 let ElemHide = exports.ElemHide =
67 { 80 {
81 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakRefer ence]),
82
68 /** 83 /**
69 * Indicates whether filters have been added or removed since the last apply() call. 84 * Indicates whether filters have been added or removed since the last apply() call.
70 * @type Boolean 85 * @type Boolean
71 */ 86 */
72 isDirty: false, 87 isDirty: false,
73 88
74 /** 89 /**
75 * Inidicates whether the element hiding stylesheet is currently applied. 90 * Inidicates whether the element hiding stylesheet is currently applied.
76 * @type Boolean 91 * @type Boolean
77 */ 92 */
78 applied: false, 93 applied: false,
79 94
80 /** 95 /**
81 * Called on module startup. 96 * Called on module startup.
82 */ 97 */
83 init: function() 98 init: function()
84 { 99 {
85 TimeLine.enter("Entered ElemHide.init()"); 100 TimeLine.enter("Entered ElemHide.init()");
101
102 if (useNew) {
103 // Avoid dependency issue.
104 Policy = require("contentPolicy").Policy;
105 }
106
86 Prefs.addListener(function(name) 107 Prefs.addListener(function(name)
87 { 108 {
88 if (name == "enabled") 109 if (name == "enabled")
89 ElemHide.apply(); 110 ElemHide.apply();
90 }); 111 });
91 onShutdown.add(function() 112
113 if (useNew)
114 Services.obs.addObserver(this, "content-document-global-created", true);
115
116 onShutdown.add(() =>
92 { 117 {
93 ElemHide.unapply(); 118 ElemHide.unapply();
119 if (useNew)
120 Services.obs.removeObserver(this, "content-document-global-created");
94 }); 121 });
95 122
96 TimeLine.log("done adding prefs listener"); 123 TimeLine.log("done adding prefs listener");
97 124
98 let styleFile = IO.resolveFilePath(Prefs.data_directory); 125 let styleFile = IO.resolveFilePath(Prefs.data_directory);
99 styleFile.append("elemhide.css"); 126 styleFile.append("elemhide.css");
100 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); 127 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL);
101 TimeLine.log("done determining stylesheet URL"); 128 TimeLine.log("done determining stylesheet URL");
102 129
103 TimeLine.leave("ElemHide.init() done"); 130 TimeLine.leave("ElemHide.init() done");
104 }, 131 },
105 132
133 observe: function (subject, topic, data, additional)
134 {
135 if (topic != "content-document-global-created")
136 return;
137
138 if (!Prefs.enabled)
139 return;
140
141 if (Policy.shouldNeverBlockWindow(subject))
142 return;
143
144 try
145 {
146 let utils = subject.QueryInterface(Ci.nsIInterfaceRequestor)
147 .getInterface(Ci.nsIDOMWindowUtils);
148 utils.addSheet(styleSheet, Ci.nsIStyleSheetService.USER_SHEET);
149 }
150 catch (e)
151 {
152 Cu.reportError(e);
153 }
154 },
155
106 /** 156 /**
107 * Removes all known filters 157 * Removes all known filters
108 */ 158 */
109 clear: function() 159 clear: function()
110 { 160 {
111 filterByKey = Object.create(null); 161 filterByKey = Object.create(null);
112 keyByFilter = Object.create(null); 162 keyByFilter = Object.create(null);
113 knownExceptions = Object.create(null); 163 knownExceptions = Object.create(null);
114 exceptions = Object.create(null); 164 exceptions = Object.create(null);
115 ElemHide.isDirty = false; 165 ElemHide.isDirty = false;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 272
223 TimeLine.enter("Entered ElemHide.apply()"); 273 TimeLine.enter("Entered ElemHide.apply()");
224 274
225 if (!ElemHide.isDirty || !Prefs.enabled) 275 if (!ElemHide.isDirty || !Prefs.enabled)
226 { 276 {
227 // Nothing changed, looks like we merely got enabled/disabled 277 // Nothing changed, looks like we merely got enabled/disabled
228 if (Prefs.enabled && !ElemHide.applied) 278 if (Prefs.enabled && !ElemHide.applied)
229 { 279 {
230 try 280 try
231 { 281 {
232 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetServ ice.USER_SHEET); 282 if (!useNew)
283 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetSe rvice.USER_SHEET);
233 ElemHide.applied = true; 284 ElemHide.applied = true;
234 } 285 }
235 catch (e) 286 catch (e)
236 { 287 {
237 Cu.reportError(e); 288 Cu.reportError(e);
238 } 289 }
239 TimeLine.log("Applying existing stylesheet finished"); 290 TimeLine.log("Applying existing stylesheet finished");
240 } 291 }
241 else if (!Prefs.enabled && ElemHide.applied) 292 else if (!Prefs.enabled && ElemHide.applied)
242 { 293 {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 { 325 {
275 ElemHide.isDirty = false; 326 ElemHide.isDirty = false;
276 327
277 ElemHide.unapply(); 328 ElemHide.unapply();
278 TimeLine.log("ElemHide.unapply() finished"); 329 TimeLine.log("ElemHide.unapply() finished");
279 330
280 if (!noFilters) 331 if (!noFilters)
281 { 332 {
282 try 333 try
283 { 334 {
284 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetSe rvice.USER_SHEET); 335 if (!useNew) {
336 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheet Service.USER_SHEET);
337 } else {
338 styleSheet = Utils.styleService.preloadSheet(styleURL, Ci.nsIStyle SheetService.USER_SHEET);
339 }
285 ElemHide.applied = true; 340 ElemHide.applied = true;
286 } 341 }
287 catch (e) 342 catch (e)
288 { 343 {
289 Cu.reportError(e); 344 Cu.reportError(e);
290 } 345 }
291 TimeLine.log("Applying stylesheet finished"); 346 TimeLine.log("Applying stylesheet finished");
292 } 347 }
293 348
294 FilterNotifier.triggerListeners("elemhideupdate"); 349 FilterNotifier.triggerListeners("elemhideupdate");
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 413
359 /** 414 /**
360 * Unapplies current stylesheet URL 415 * Unapplies current stylesheet URL
361 */ 416 */
362 unapply: function() 417 unapply: function()
363 { 418 {
364 if (ElemHide.applied) 419 if (ElemHide.applied)
365 { 420 {
366 try 421 try
367 { 422 {
368 Utils.styleService.unregisterSheet(styleURL, Ci.nsIStyleSheetService.USE R_SHEET); 423 if (!useNew)
424 Utils.styleService.unregisterSheet(styleURL, Ci.nsIStyleSheetService.U SER_SHEET);
369 } 425 }
370 catch (e) 426 catch (e)
371 { 427 {
372 Cu.reportError(e); 428 Cu.reportError(e);
373 } 429 }
374 ElemHide.applied = false; 430 ElemHide.applied = false;
375 } 431 }
376 }, 432 },
377 433
378 /** 434 /**
379 * Retrieves the currently applied stylesheet URL
380 * @type String
381 */
382 get styleURL()
383 {
384 return ElemHide.applied ? styleURL.spec : null;
385 },
386
387 /**
388 * Retrieves an element hiding filter by the corresponding protocol key 435 * Retrieves an element hiding filter by the corresponding protocol key
389 */ 436 */
390 getFilterByKey: function(/**String*/ key) /**Filter*/ 437 getFilterByKey: function(/**String*/ key) /**Filter*/
391 { 438 {
392 return (key in filterByKey ? filterByKey[key] : null); 439 return (key in filterByKey ? filterByKey[key] : null);
393 }, 440 },
394 441
395 /** 442 /**
396 * Returns a list of all selectors active on a particular domain (currently 443 * Returns a list of all selectors active on a particular domain (currently
397 * used only in Chrome, Opera and Safari). 444 * used only in Chrome, Opera and Safari).
(...skipping 12 matching lines...) Expand all
410 457
411 if (specificOnly && (!domains || domains[""])) 458 if (specificOnly && (!domains || domains[""]))
412 continue; 459 continue;
413 460
414 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) 461 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain))
415 result.push(filter.selector); 462 result.push(filter.selector);
416 } 463 }
417 return result; 464 return result;
418 } 465 }
419 }; 466 };
OLDNEW
« no previous file with comments | « lib/contentPolicy.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld