1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-2017 eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @fileOverview Serves CSS for element hiding and processes hits.
*/
try
{
// Hack: SDK loader masks our Components object with a getter.
let proto = Object.getPrototypeOf(this);
let property = Object.getOwnPropertyDescriptor(proto, "Components");
if (property && property.get)
delete proto.Components;
}
catch (e)
{
Cu.reportError(e);
}
let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
let {shouldAllowAsync} = require("child/contentPolicy");
let {getFrames, isPrivate, getRequestWindow} = require("child/utils");
let {RequestNotifier} = require("child/requestNotifier");
let {port} = require("messaging");
let {Utils} = require("utils");
const notImplemented = () => Cr.NS_ERROR_NOT_IMPLEMENTED;
/**
* about: URL module used to count hits.
* @class
*/
let AboutHandler =
{
classID: Components.ID("{55fb7be0-1dd2-11b2-98e6-9e97caf8ba67}"),
classDescription: "Element hiding hit registration protocol handler",
aboutPrefix: "abp-elemhide",
/**
* Registers handler on startup.
*/
init: function()
{
let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(this.classID, this.classDescription,
"@mozilla.org/network/protocol/about;1?what=" + this.aboutPrefix, this);
onShutdown.add(function()
{
registrar.unregisterFactory(this.classID, this);
}.bind(this));
},
//
// Factory implementation
//
createInstance: function(outer, iid)
{
if (outer != null)
throw Cr.NS_ERROR_NO_AGGREGATION;
return this.QueryInterface(iid);
},
//
// About module implementation
//
getURIFlags: function(uri)
{
return Ci.nsIAboutModule.HIDE_FROM_ABOUTABOUT;
},
newChannel: function(uri, loadInfo)
{
let match = /\?hit(\d+)$/.exec(uri.path);
if (match)
return new HitRegistrationChannel(uri, loadInfo, match[1]);
match = /\?css(?:=(.*?))?(&specificonly)?$/.exec(uri.path);
if (match)
{
return new StyleDataChannel(uri, loadInfo,
match[1] ? decodeURIComponent(match[1]) : null, !!match[2]);
}
throw Cr.NS_ERROR_FAILURE;
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory, Ci.nsIAboutModule])
};
AboutHandler.init();
/**
* Base class for channel implementations, subclasses usually only need to
* override BaseChannel._getResponse() method.
* @constructor
*/
function BaseChannel(uri, loadInfo)
{
this.URI = this.originalURI = uri;
this.loadInfo = loadInfo;
}
BaseChannel.prototype = {
URI: null,
originalURI: null,
contentCharset: "utf-8",
contentLength: 0,
contentType: null,
owner: Utils.systemPrincipal,
securityInfo: null,
notificationCallbacks: null,
loadFlags: 0,
loadGroup: null,
name: null,
status: Cr.NS_OK,
_getResponse: notImplemented,
_checkSecurity: function()
{
if (!this.loadInfo.triggeringPrincipal.equals(Utils.systemPrincipal))
throw Cr.NS_ERROR_FAILURE;
},
asyncOpen: function(listener, context)
{
Promise.resolve(this._getResponse()).then(data =>
{
let stream = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
stream.setData(data, data.length);
try
{
listener.onStartRequest(this, context);
}
catch(e)
{
// Listener failing isn't our problem
}
try
{
listener.onDataAvailable(this, context, stream, 0, stream.available());
}
catch(e)
{
// Listener failing isn't our problem
}
try
{
listener.onStopRequest(this, context, Cr.NS_OK);
}
catch(e)
{
// Listener failing isn't our problem
}
});
},
asyncOpen2: function(listener)
{
this._checkSecurity();
this.asyncOpen(listener, null);
},
open: function()
{
let data = this._getResponse();
if (typeof data.then == "function")
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
let stream = Cc["@mozilla.org/io/string-input-stream;1"]
.createInstance(Ci.nsIStringInputStream);
stream.setData(data, data.length);
return stream;
},
open2: function()
{
this._checkSecurity();
return this.open();
},
isPending: () => false,
cancel: notImplemented,
suspend: notImplemented,
resume: notImplemented,
QueryInterface: XPCOMUtils.generateQI([Ci.nsIChannel, Ci.nsIRequest])
};
/**
* Channel returning CSS data for the global as well as site-specific stylesheet.
* @constructor
*/
function StyleDataChannel(uri, loadInfo, domain, specificOnly)
{
BaseChannel.call(this, uri, loadInfo);
this._domain = domain;
this._specificOnly = specificOnly;
}
StyleDataChannel.prototype = {
__proto__: BaseChannel.prototype,
contentType: "text/css",
_domain: null,
_getResponse: function()
{
function escapeChar(match)
{
return "\\" + match.charCodeAt(0).toString(16) + " ";
}
// Would be great to avoid sync messaging here but nsIStyleSheetService
// insists on opening channels synchronously.
let [selectors, keys] = (this._domain ?
port.emitSync("getSelectorsForDomain", [this._domain, this._specificOnly]) :
port.emitSync("getUnconditionalSelectors"));
let cssPrefix = "{-moz-binding: url(about:abp-elemhide?hit";
let cssSuffix = "#dummy) !important;}\n";
let result = [];
for (let i = 0; i < selectors.length; i++)
{
let selector = selectors[i];
let key = keys[i];
result.push(selector.replace(/[^\x01-\x7F]/g, escapeChar),
cssPrefix, key, cssSuffix);
}
return result.join("");
}
};
/**
* Channel returning data for element hiding hits.
* @constructor
*/
function HitRegistrationChannel(uri, loadInfo, key)
{
BaseChannel.call(this, uri, loadInfo);
this.key = key;
}
HitRegistrationChannel.prototype = {
__proto__: BaseChannel.prototype,
key: null,
contentType: "text/xml",
_getResponse: function()
{
let window = getRequestWindow(this);
port.emitWithResponse("registerElemHideHit", {
key: this.key,
frames: getFrames(window),
isPrivate: isPrivate(window)
}).then(hit =>
{
if (hit)
RequestNotifier.addNodeData(window.document, window.top, hit);
});
return "<bindings xmlns='http://www.mozilla.org/xbl'/>";
}
};
let observer = {
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIObserver, Ci.nsISupportsWeakReference
]),
topic: "document-element-inserted",
styleURL: Utils.makeURI("about:abp-elemhide?css"),
sheet: null,
init: function()
{
Services.obs.addObserver(this, this.topic, true);
onShutdown.add(() =>
{
Services.obs.removeObserver(this, this.topic);
});
port.on("elemhideupdate", () =>
{
this.sheet = null;
});
},
observe: function(subject, topic, data)
{
if (topic != this.topic)
return;
let window = subject.defaultView;
if (!window)
{
// This is typically XBL bindings and SVG images, but also real
// documents occasionally - probably due to speculative loading?
return;
}
let type = window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.itemType;
if (type != Ci.nsIDocShellTreeItem.typeContent)
return;
port.emitWithResponse("elemhideEnabled", {
frames: getFrames(window),
isPrivate: isPrivate(window)
}).then(({
enabled, contentType, docDomain, thirdParty, location, filter,
filterType
}) =>
{
if (Cu.isDeadWrapper(window))
{
// We are too late, the window is gone already.
return;
}
if (enabled)
{
let utils = window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
// If we have a filter hit at this point then it must be a $generichide
// filter - apply only specific element hiding filters.
let specificOnly = !!filter;
if (!specificOnly)
{
if (!this.sheet)
{
this.sheet = Utils.styleService.preloadSheet(this.styleURL,
Ci.nsIStyleSheetService.USER_SHEET);
}
try
{
utils.addSheet(this.sheet, Ci.nsIStyleSheetService.USER_SHEET);
}
catch (e)
{
// Ignore NS_ERROR_ILLEGAL_VALUE - it will be thrown if we try to add
// the stylesheet multiple times to the same document (the observer
// will be notified twice for some documents).
if (e.result != Cr.NS_ERROR_ILLEGAL_VALUE)
throw e;
}
}
let host = window.location.hostname;
if (host)
{
try
{
let suffix = "=" + encodeURIComponent(host);
if (specificOnly)
suffix += "&specificonly";
utils.loadSheetUsingURIString(this.styleURL.spec + suffix,
Ci.nsIStyleSheetService.USER_SHEET);
}
catch (e)
{
// Ignore NS_ERROR_ILLEGAL_VALUE - it will be thrown if we try to add
// the stylesheet multiple times to the same document (the observer
// will be notified twice for some documents).
if (e.result != Cr.NS_ERROR_ILLEGAL_VALUE)
throw e;
}
}
}
if (filter)
{
RequestNotifier.addNodeData(window.document, window.top, {
contentType, docDomain, thirdParty, location, filter, filterType
});
}
});
}
};
observer.init();
|