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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var EXPORTED_SYMBOLS = ["ContentBlockingAllowList"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.defineModuleGetter(
this,
"PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm"
);
/**
* A helper module to manage the Content Blocking Allow List.
*
* This module provides a couple of utility APIs for adding or
* removing a given browser object to the Content Blocking allow
* list.
*/
const ContentBlockingAllowList = {
_observingLastPBContext: false,
_maybeSetupLastPBContextObserver() {
if (!this._observingLastPBContext) {
this._observer = {
QueryInterface: ChromeUtils.generateQI([
"nsIObserver",
"nsISupportsWeakReference",
]),
observe(subject, topic, data) {
if (topic == "last-pb-context-exited") {
Services.perms.removeByType("trackingprotection-pb");
}
},
};
Services.obs.addObserver(this._observer, "last-pb-context-exited", true);
this._observingLastPBContext = true;
}
},
_basePrincipalForAntiTrackingCommon(browser) {
let principal =
browser.browsingContext.currentWindowGlobal
?.contentBlockingAllowListPrincipal;
// We can only use content principals for this purpose.
if (!principal || !principal.isContentPrincipal) {
return null;
}
return principal;
},
_permissionTypeFor(browser) {
return PrivateBrowsingUtils.isBrowserPrivate(browser)
? "trackingprotection-pb"
: "trackingprotection";
},
_expiryFor(browser) {
return PrivateBrowsingUtils.isBrowserPrivate(browser)
? Ci.nsIPermissionManager.EXPIRE_SESSION
: Ci.nsIPermissionManager.EXPIRE_NEVER;
},
/**
* Returns false if this module cannot handle the current document loaded in
* the browser object. This can happen for example for about: or file:
* documents.
*/
canHandle(browser) {
return this._basePrincipalForAntiTrackingCommon(browser) != null;
},
/**
* Add the given browser object to the Content Blocking allow list.
*/
add(browser) {
// Start observing PB last-context-exit notification to do the needed cleanup.
this._maybeSetupLastPBContextObserver();
let prin = this._basePrincipalForAntiTrackingCommon(browser);
let type = this._permissionTypeFor(browser);
let expire = this._expiryFor(browser);
Services.perms.addFromPrincipal(
prin,
type,
Services.perms.ALLOW_ACTION,
expire
);
},
/**
* Remove the given browser object from the Content Blocking allow list.
*/
remove(browser) {
let prin = this._basePrincipalForAntiTrackingCommon(browser);
let type = this._permissionTypeFor(browser);
Services.perms.removeFromPrincipal(prin, type);
},
/**
* Remove the given principal from the Content Blocking allow list if present.
*/
removeByPrincipal(principal) {
Services.perms.removeFromPrincipal(principal, "trackingprotection");
Services.perms.removeFromPrincipal(principal, "trackingprotection-pb");
},
/**
* Returns true if the current browser has loaded a document that is on the
* Content Blocking allow list.
*/
includes(browser) {
let prin = this._basePrincipalForAntiTrackingCommon(browser);
let type = this._permissionTypeFor(browser);
return (
Services.perms.testExactPermissionFromPrincipal(prin, type) ==
Services.perms.ALLOW_ACTION
);
},
/**
* Returns a list of all non-private browsing principals that are on the
* content blocking allow list.
*/
getAllowListedPrincipals() {
const exceptions = Services.perms
.getAllWithTypePrefix("trackingprotection")
.filter(
// Only export non-private exceptions for security reasons.
p => p.type == "trackingprotection"
);
return exceptions.map(e => e.principal);
},
/**
* Takes a list of nsIPrincipals and uses it to update the content blocking allow
* list.
*/
addAllowListPrincipals(principals) {
principals.forEach(p =>
Services.perms.addFromPrincipal(
p,
"trackingprotection",
Services.perms.ALLOW_ACTION,
Ci.nsIPermissionManager.EXPIRE_SESSION
)
);
},
/**
* Removes all content blocking exceptions.
*/
wipeLists() {
Services.perms.removeByType("trackingprotection");
Services.perms.removeByType("trackingprotection-pb");
},
};
|