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
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
"resource://gre/modules/PlacesUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Preferences",
"resource://gre/modules/Preferences.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Sanitizer",
"resource:///modules/Sanitizer.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Services",
"resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "setTimeout",
"resource://gre/modules/Timer.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "serviceWorkerManager",
"@mozilla.org/serviceworkers/manager;1",
"nsIServiceWorkerManager");
/**
* A number of iterations after which to yield time back
* to the system.
*/
const YIELD_PERIOD = 10;
const PREF_DOMAIN = "privacy.cpd.";
XPCOMUtils.defineLazyGetter(this, "sanitizer", () => {
let sanitizer = new Sanitizer();
sanitizer.prefDomain = PREF_DOMAIN;
return sanitizer;
});
function makeRange(options) {
return (options.since == null) ?
null :
[PlacesUtils.toPRTime(options.since), PlacesUtils.toPRTime(Date.now())];
}
function clearCache() {
// Clearing the cache does not support timestamps.
return sanitizer.items.cache.clear();
}
let clearCookies = Task.async(function* (options) {
let cookieMgr = Services.cookies;
// This code has been borrowed from sanitize.js.
let yieldCounter = 0;
if (options.since) {
// Iterate through the cookies and delete any created after our cutoff.
let cookiesEnum = cookieMgr.enumerator;
while (cookiesEnum.hasMoreElements()) {
let cookie = cookiesEnum.getNext().QueryInterface(Ci.nsICookie2);
if (cookie.creationTime >= PlacesUtils.toPRTime(options.since)) {
// This cookie was created after our cutoff, clear it.
cookieMgr.remove(cookie.host, cookie.name, cookie.path,
false, cookie.originAttributes);
if (++yieldCounter % YIELD_PERIOD == 0) {
yield new Promise(resolve => setTimeout(resolve, 0)); // Don't block the main thread too long.
}
}
}
} else {
// Remove everything.
cookieMgr.removeAll();
}
});
function clearDownloads(options) {
return sanitizer.items.downloads.clear(makeRange(options));
}
function clearFormData(options) {
return sanitizer.items.formdata.clear(makeRange(options));
}
function clearHistory(options) {
return sanitizer.items.history.clear(makeRange(options));
}
let clearPasswords = Task.async(function* (options) {
let loginManager = Services.logins;
let yieldCounter = 0;
if (options.since) {
// Iterate through the logins and delete any updated after our cutoff.
let logins = loginManager.getAllLogins();
for (let login of logins) {
login.QueryInterface(Ci.nsILoginMetaInfo);
if (login.timePasswordChanged >= options.since) {
loginManager.removeLogin(login);
if (++yieldCounter % YIELD_PERIOD == 0) {
yield new Promise(resolve => setTimeout(resolve, 0)); // Don't block the main thread too long.
}
}
}
} else {
// Remove everything.
loginManager.removeAllLogins();
}
});
function clearPluginData(options) {
return sanitizer.items.pluginData.clear(makeRange(options));
}
let clearServiceWorkers = Task.async(function* () {
// Clearing service workers does not support timestamps.
let yieldCounter = 0;
// Iterate through the service workers and remove them.
let serviceWorkers = serviceWorkerManager.getAllRegistrations();
for (let i = 0; i < serviceWorkers.length; i++) {
let sw = serviceWorkers.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo);
let host = sw.principal.URI.host;
serviceWorkerManager.removeAndPropagate(host);
if (++yieldCounter % YIELD_PERIOD == 0) {
yield new Promise(resolve => setTimeout(resolve, 0)); // Don't block the main thread too long.
}
}
});
function doRemoval(options, dataToRemove, extension) {
if (options.originTypes &&
(options.originTypes.protectedWeb || options.originTypes.extension)) {
return Promise.reject(
{message: "Firefox does not support protectedWeb or extension as originTypes."});
}
let removalPromises = [];
let invalidDataTypes = [];
for (let dataType in dataToRemove) {
if (dataToRemove[dataType]) {
switch (dataType) {
case "cache":
removalPromises.push(clearCache());
break;
case "cookies":
removalPromises.push(clearCookies(options));
break;
case "downloads":
removalPromises.push(clearDownloads(options));
break;
case "formData":
removalPromises.push(clearFormData(options));
break;
case "history":
removalPromises.push(clearHistory(options));
break;
case "passwords":
removalPromises.push(clearPasswords(options));
break;
case "pluginData":
removalPromises.push(clearPluginData(options));
break;
case "serviceWorkers":
removalPromises.push(clearServiceWorkers());
break;
default:
invalidDataTypes.push(dataType);
}
}
}
if (extension && invalidDataTypes.length) {
extension.logger.warn(
`Firefox does not support dataTypes: ${invalidDataTypes.toString()}.`);
}
return Promise.all(removalPromises);
}
extensions.registerSchemaAPI("browsingData", "addon_parent", context => {
let {extension} = context;
return {
browsingData: {
settings() {
const PREF_DOMAIN = "privacy.cpd.";
// The following prefs are the only ones in Firefox that match corresponding
// values used by Chrome when rerturning settings.
const PREF_LIST = ["cache", "cookies", "history", "formdata", "downloads"];
// since will be the start of what is returned by Sanitizer.getClearRange
// divided by 1000 to convert to ms.
// If Sanitizer.getClearRange returns undefined that means the range is
// currently "Everything", so we should set since to 0.
let clearRange = Sanitizer.getClearRange();
let since = clearRange ? clearRange[0] / 1000 : 0;
let options = {since};
let dataToRemove = {};
let dataRemovalPermitted = {};
for (let item of PREF_LIST) {
// The property formData needs a different case than the
// formdata preference.
const name = item === "formdata" ? "formData" : item;
dataToRemove[name] = Preferences.get(`${PREF_DOMAIN}${item}`);
// Firefox doesn't have the same concept of dataRemovalPermitted
// as Chrome, so it will always be true.
dataRemovalPermitted[name] = true;
}
return Promise.resolve({options, dataToRemove, dataRemovalPermitted});
},
remove(options, dataToRemove) {
return doRemoval(options, dataToRemove, extension);
},
removeCache(options) {
return doRemoval(options, {cache: true});
},
removeCookies(options) {
return doRemoval(options, {cookies: true});
},
removeDownloads(options) {
return doRemoval(options, {downloads: true});
},
removeFormData(options) {
return doRemoval(options, {formData: true});
},
removeHistory(options) {
return doRemoval(options, {history: true});
},
removePasswords(options) {
return doRemoval(options, {passwords: true});
},
removePluginData(options) {
return doRemoval(options, {pluginData: true});
},
},
};
});
|