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: Version 1.0 Created Aug. 6, 2014, 4:30 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 the 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 if (styleSheet)
145 {
146 try
147 {
148 let utils = subject.QueryInterface(Ci.nsIInterfaceRequestor)
149 .getInterface(Ci.nsIDOMWindowUtils);
150 utils.addSheet(styleSheet, Ci.nsIStyleSheetService.USER_SHEET);
151 }
152 catch (e)
153 {
154 Cu.reportError(e);
155 }
156 }
157 },
158
106 /** 159 /**
107 * Removes all known filters 160 * Removes all known filters
108 */ 161 */
109 clear: function() 162 clear: function()
110 { 163 {
111 filterByKey = Object.create(null); 164 filterByKey = Object.create(null);
112 keyByFilter = Object.create(null); 165 keyByFilter = Object.create(null);
113 knownExceptions = Object.create(null); 166 knownExceptions = Object.create(null);
114 exceptions = Object.create(null); 167 exceptions = Object.create(null);
115 ElemHide.isDirty = false; 168 ElemHide.isDirty = false;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 275
223 TimeLine.enter("Entered ElemHide.apply()"); 276 TimeLine.enter("Entered ElemHide.apply()");
224 277
225 if (!ElemHide.isDirty || !Prefs.enabled) 278 if (!ElemHide.isDirty || !Prefs.enabled)
226 { 279 {
227 // Nothing changed, looks like we merely got enabled/disabled 280 // Nothing changed, looks like we merely got enabled/disabled
228 if (Prefs.enabled && !ElemHide.applied) 281 if (Prefs.enabled && !ElemHide.applied)
229 { 282 {
230 try 283 try
231 { 284 {
232 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetServ ice.USER_SHEET); 285 if (!useNew)
286 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetSe rvice.USER_SHEET);
233 ElemHide.applied = true; 287 ElemHide.applied = true;
234 } 288 }
235 catch (e) 289 catch (e)
236 { 290 {
237 Cu.reportError(e); 291 Cu.reportError(e);
238 } 292 }
239 TimeLine.log("Applying existing stylesheet finished"); 293 TimeLine.log("Applying existing stylesheet finished");
240 } 294 }
241 else if (!Prefs.enabled && ElemHide.applied) 295 else if (!Prefs.enabled && ElemHide.applied)
242 { 296 {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 { 328 {
275 ElemHide.isDirty = false; 329 ElemHide.isDirty = false;
276 330
277 ElemHide.unapply(); 331 ElemHide.unapply();
278 TimeLine.log("ElemHide.unapply() finished"); 332 TimeLine.log("ElemHide.unapply() finished");
279 333
280 if (!noFilters) 334 if (!noFilters)
281 { 335 {
282 try 336 try
283 { 337 {
284 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetSe rvice.USER_SHEET); 338 if (!useNew)
339 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheet Service.USER_SHEET);
340 else
341 styleSheet = Utils.styleService.preloadSheet(styleURL, Ci.nsIStyle SheetService.USER_SHEET);
285 ElemHide.applied = true; 342 ElemHide.applied = true;
286 } 343 }
287 catch (e) 344 catch (e)
288 { 345 {
289 Cu.reportError(e); 346 Cu.reportError(e);
290 } 347 }
291 TimeLine.log("Applying stylesheet finished"); 348 TimeLine.log("Applying stylesheet finished");
292 } 349 }
293 350
294 FilterNotifier.triggerListeners("elemhideupdate"); 351 FilterNotifier.triggerListeners("elemhideupdate");
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 415
359 /** 416 /**
360 * Unapplies current stylesheet URL 417 * Unapplies current stylesheet URL
361 */ 418 */
362 unapply: function() 419 unapply: function()
363 { 420 {
364 if (ElemHide.applied) 421 if (ElemHide.applied)
365 { 422 {
366 try 423 try
367 { 424 {
368 Utils.styleService.unregisterSheet(styleURL, Ci.nsIStyleSheetService.USE R_SHEET); 425 if (!useNew)
426 Utils.styleService.unregisterSheet(styleURL, Ci.nsIStyleSheetService.U SER_SHEET);
369 } 427 }
370 catch (e) 428 catch (e)
371 { 429 {
372 Cu.reportError(e); 430 Cu.reportError(e);
373 } 431 }
374 ElemHide.applied = false; 432 ElemHide.applied = false;
375 } 433 }
376 }, 434 },
377 435
378 /** 436 /**
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 437 * Retrieves an element hiding filter by the corresponding protocol key
389 */ 438 */
390 getFilterByKey: function(/**String*/ key) /**Filter*/ 439 getFilterByKey: function(/**String*/ key) /**Filter*/
391 { 440 {
392 return (key in filterByKey ? filterByKey[key] : null); 441 return (key in filterByKey ? filterByKey[key] : null);
393 }, 442 },
394 443
395 /** 444 /**
396 * Returns a list of all selectors active on a particular domain (currently 445 * Returns a list of all selectors active on a particular domain (currently
397 * used only in Chrome, Opera and Safari). 446 * used only in Chrome, Opera and Safari).
(...skipping 12 matching lines...) Expand all
410 459
411 if (specificOnly && (!domains || domains[""])) 460 if (specificOnly && (!domains || domains[""]))
412 continue; 461 continue;
413 462
414 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) 463 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain))
415 result.push(filter.selector); 464 result.push(filter.selector);
416 } 465 }
417 return result; 466 return result;
418 } 467 }
419 }; 468 };
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