OLD | NEW |
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 Loading... |
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} = require("contentPolicy"); |
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 Loading... |
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 let useNew = ('preloadSheet' in Utils.styleService); |
| 70 |
| 71 /** |
63 * Element hiding component | 72 * Element hiding component |
64 * @class | 73 * @class |
65 */ | 74 */ |
66 let ElemHide = exports.ElemHide = | 75 let ElemHide = exports.ElemHide = |
67 { | 76 { |
| 77 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakRefer
ence]), |
| 78 |
68 /** | 79 /** |
69 * 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. |
70 * @type Boolean | 81 * @type Boolean |
71 */ | 82 */ |
72 isDirty: false, | 83 isDirty: false, |
73 | 84 |
74 /** | 85 /** |
75 * Inidicates whether the element hiding stylesheet is currently applied. | 86 * Inidicates whether the element hiding stylesheet is currently applied. |
76 * @type Boolean | 87 * @type Boolean |
77 */ | 88 */ |
78 applied: false, | 89 applied: false, |
79 | 90 |
80 /** | 91 /** |
81 * Called on module startup. | 92 * Called on module startup. |
82 */ | 93 */ |
83 init: function() | 94 init: function() |
84 { | 95 { |
85 TimeLine.enter("Entered ElemHide.init()"); | 96 TimeLine.enter("Entered ElemHide.init()"); |
86 Prefs.addListener(function(name) | 97 Prefs.addListener(function(name) |
87 { | 98 { |
88 if (name == "enabled") | 99 if (name == "enabled") |
89 ElemHide.apply(); | 100 ElemHide.apply(); |
90 }); | 101 }); |
91 onShutdown.add(function() | 102 |
| 103 if (useNew) |
| 104 Services.obs.addObserver(this, "content-document-global-created", true); |
| 105 |
| 106 onShutdown.add(() => |
92 { | 107 { |
93 ElemHide.unapply(); | 108 ElemHide.unapply(); |
| 109 if (useNew) |
| 110 Services.obs.removeObserver(this, "content-document-global-created"); |
94 }); | 111 }); |
95 | 112 |
96 TimeLine.log("done adding prefs listener"); | 113 TimeLine.log("done adding prefs listener"); |
97 | 114 |
98 let styleFile = IO.resolveFilePath(Prefs.data_directory); | 115 let styleFile = IO.resolveFilePath(Prefs.data_directory); |
99 styleFile.append("elemhide.css"); | 116 styleFile.append("elemhide.css"); |
100 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); | 117 styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL); |
101 TimeLine.log("done determining stylesheet URL"); | 118 TimeLine.log("done determining stylesheet URL"); |
102 | 119 |
103 TimeLine.leave("ElemHide.init() done"); | 120 TimeLine.leave("ElemHide.init() done"); |
104 }, | 121 }, |
105 | 122 |
| 123 observe: function (subject, topic, data, additional) |
| 124 { |
| 125 if (topic != "content-document-global-created") |
| 126 return; |
| 127 |
| 128 if (!Prefs.enabled) |
| 129 return; |
| 130 |
| 131 let process = Policy.processWindow(subject); |
| 132 dump(subject.document.documentURIObject.spec + " is okay " + process + "\n"
); |
| 133 if (process) |
| 134 return; |
| 135 |
| 136 // XXX at this point the window might still be whitelisted |
| 137 try |
| 138 { |
| 139 let utils = subject.QueryInterface(Ci.nsIInterfaceRequestor) |
| 140 .getInterface(Ci.nsIDOMWindowUtils); |
| 141 utils.addSheet(styleSheet, Ci.nsIStyleSheetService.USER_SHEET); |
| 142 } |
| 143 catch (e) |
| 144 { |
| 145 Cu.reportError(e); |
| 146 } |
| 147 }, |
| 148 |
106 /** | 149 /** |
107 * Removes all known filters | 150 * Removes all known filters |
108 */ | 151 */ |
109 clear: function() | 152 clear: function() |
110 { | 153 { |
111 filterByKey = Object.create(null); | 154 filterByKey = Object.create(null); |
112 keyByFilter = Object.create(null); | 155 keyByFilter = Object.create(null); |
113 knownExceptions = Object.create(null); | 156 knownExceptions = Object.create(null); |
114 exceptions = Object.create(null); | 157 exceptions = Object.create(null); |
115 ElemHide.isDirty = false; | 158 ElemHide.isDirty = false; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 265 |
223 TimeLine.enter("Entered ElemHide.apply()"); | 266 TimeLine.enter("Entered ElemHide.apply()"); |
224 | 267 |
225 if (!ElemHide.isDirty || !Prefs.enabled) | 268 if (!ElemHide.isDirty || !Prefs.enabled) |
226 { | 269 { |
227 // Nothing changed, looks like we merely got enabled/disabled | 270 // Nothing changed, looks like we merely got enabled/disabled |
228 if (Prefs.enabled && !ElemHide.applied) | 271 if (Prefs.enabled && !ElemHide.applied) |
229 { | 272 { |
230 try | 273 try |
231 { | 274 { |
232 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetServ
ice.USER_SHEET); | 275 if (!useNew) |
| 276 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetSe
rvice.USER_SHEET); |
233 ElemHide.applied = true; | 277 ElemHide.applied = true; |
234 } | 278 } |
235 catch (e) | 279 catch (e) |
236 { | 280 { |
237 Cu.reportError(e); | 281 Cu.reportError(e); |
238 } | 282 } |
239 TimeLine.log("Applying existing stylesheet finished"); | 283 TimeLine.log("Applying existing stylesheet finished"); |
240 } | 284 } |
241 else if (!Prefs.enabled && ElemHide.applied) | 285 else if (!Prefs.enabled && ElemHide.applied) |
242 { | 286 { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 { | 318 { |
275 ElemHide.isDirty = false; | 319 ElemHide.isDirty = false; |
276 | 320 |
277 ElemHide.unapply(); | 321 ElemHide.unapply(); |
278 TimeLine.log("ElemHide.unapply() finished"); | 322 TimeLine.log("ElemHide.unapply() finished"); |
279 | 323 |
280 if (!noFilters) | 324 if (!noFilters) |
281 { | 325 { |
282 try | 326 try |
283 { | 327 { |
284 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheetSe
rvice.USER_SHEET); | 328 if (!useNew) { |
| 329 Utils.styleService.loadAndRegisterSheet(styleURL, Ci.nsIStyleSheet
Service.USER_SHEET); |
| 330 } else { |
| 331 styleSheet = Utils.styleService.preloadSheet(styleURL, Ci.nsIStyle
SheetService.USER_SHEET); |
| 332 } |
285 ElemHide.applied = true; | 333 ElemHide.applied = true; |
286 } | 334 } |
287 catch (e) | 335 catch (e) |
288 { | 336 { |
289 Cu.reportError(e); | 337 Cu.reportError(e); |
290 } | 338 } |
291 TimeLine.log("Applying stylesheet finished"); | 339 TimeLine.log("Applying stylesheet finished"); |
292 } | 340 } |
293 | 341 |
294 FilterNotifier.triggerListeners("elemhideupdate"); | 342 FilterNotifier.triggerListeners("elemhideupdate"); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 406 |
359 /** | 407 /** |
360 * Unapplies current stylesheet URL | 408 * Unapplies current stylesheet URL |
361 */ | 409 */ |
362 unapply: function() | 410 unapply: function() |
363 { | 411 { |
364 if (ElemHide.applied) | 412 if (ElemHide.applied) |
365 { | 413 { |
366 try | 414 try |
367 { | 415 { |
368 Utils.styleService.unregisterSheet(styleURL, Ci.nsIStyleSheetService.USE
R_SHEET); | 416 if (!useNew) |
| 417 Utils.styleService.unregisterSheet(styleURL, Ci.nsIStyleSheetService.U
SER_SHEET); |
369 } | 418 } |
370 catch (e) | 419 catch (e) |
371 { | 420 { |
372 Cu.reportError(e); | 421 Cu.reportError(e); |
373 } | 422 } |
374 ElemHide.applied = false; | 423 ElemHide.applied = false; |
375 } | 424 } |
376 }, | 425 }, |
377 | 426 |
378 /** | 427 /** |
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 | 428 * Retrieves an element hiding filter by the corresponding protocol key |
389 */ | 429 */ |
390 getFilterByKey: function(/**String*/ key) /**Filter*/ | 430 getFilterByKey: function(/**String*/ key) /**Filter*/ |
391 { | 431 { |
392 return (key in filterByKey ? filterByKey[key] : null); | 432 return (key in filterByKey ? filterByKey[key] : null); |
393 }, | 433 }, |
394 | 434 |
395 /** | 435 /** |
396 * Returns a list of all selectors active on a particular domain (currently | 436 * Returns a list of all selectors active on a particular domain (currently |
397 * used only in Chrome, Opera and Safari). | 437 * used only in Chrome, Opera and Safari). |
(...skipping 12 matching lines...) Expand all Loading... |
410 | 450 |
411 if (specificOnly && (!domains || domains[""])) | 451 if (specificOnly && (!domains || domains[""])) |
412 continue; | 452 continue; |
413 | 453 |
414 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) | 454 if (filter.isActiveOnDomain(domain) && !this.getException(filter, domain)) |
415 result.push(filter.selector); | 455 result.push(filter.selector); |
416 } | 456 } |
417 return result; | 457 return result; |
418 } | 458 } |
419 }; | 459 }; |
OLD | NEW |