forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSanitizer.jsm
250 lines (213 loc) · 6.37 KB
/
Sanitizer.jsm
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
// -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/* 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/. */
let Cc = Components.classes;
let Ci = Components.interfaces;
let Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/LoadContextInfo.jsm");
Cu.import("resource://gre/modules/FormHistory.jsm");
function dump(a) {
Services.console.logStringMessage(a);
}
function sendMessageToJava(aMessage) {
return Services.androidBridge.handleGeckoMessage(JSON.stringify(aMessage));
}
this.EXPORTED_SYMBOLS = ["Sanitizer"];
let downloads = {
dlmgr: Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager),
iterate: function (aCallback) {
let dlmgr = downloads.dlmgr;
let dbConn = dlmgr.DBConnection;
let stmt = dbConn.createStatement("SELECT id FROM moz_downloads WHERE " +
"state = ? OR state = ? OR state = ? OR state = ? OR state = ? OR state = ?");
stmt.bindInt32Parameter(0, Ci.nsIDownloadManager.DOWNLOAD_FINISHED);
stmt.bindInt32Parameter(1, Ci.nsIDownloadManager.DOWNLOAD_FAILED);
stmt.bindInt32Parameter(2, Ci.nsIDownloadManager.DOWNLOAD_CANCELED);
stmt.bindInt32Parameter(3, Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_PARENTAL);
stmt.bindInt32Parameter(4, Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_POLICY);
stmt.bindInt32Parameter(5, Ci.nsIDownloadManager.DOWNLOAD_DIRTY);
while (stmt.executeStep()) {
aCallback(dlmgr.getDownload(stmt.row.id));
}
stmt.finalize();
},
get canClear() {
return this.dlmgr.canCleanUp;
}
};
function Sanitizer() {}
Sanitizer.prototype = {
clearItem: function (aItemName)
{
let item = this.items[aItemName];
let canClear = item.canClear;
if (typeof canClear == "function") {
canClear(function clearCallback(aCanClear) {
if (aCanClear)
item.clear();
});
} else if (canClear) {
item.clear();
}
},
items: {
cache: {
clear: function ()
{
var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService);
try {
cache.clear();
} catch(er) {}
let imageCache = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
.getImgCacheForDocument(null);
try {
imageCache.clearCache(false); // true=chrome, false=content
} catch(er) {}
},
get canClear()
{
return true;
}
},
cookies: {
clear: function ()
{
Services.cookies.removeAll();
},
get canClear()
{
return true;
}
},
siteSettings: {
clear: function ()
{
// Clear site-specific permissions like "Allow this site to open popups"
Services.perms.removeAll();
// Clear site-specific settings like page-zoom level
Cc["@mozilla.org/content-pref/service;1"]
.getService(Ci.nsIContentPrefService2)
.removeAllDomains(null);
// Clear "Never remember passwords for this site", which is not handled by
// the permission manager
var hosts = Services.logins.getAllDisabledHosts({})
for (var host of hosts) {
Services.logins.setLoginSavingEnabled(host, true);
}
},
get canClear()
{
return true;
}
},
offlineApps: {
clear: function ()
{
var cacheService = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService);
var appCacheStorage = cacheService.appCacheStorage(LoadContextInfo.default, null);
try {
appCacheStorage.asyncEvictStorage(null);
} catch(er) {}
},
get canClear()
{
return true;
}
},
history: {
clear: function ()
{
sendMessageToJava({ type: "Sanitize:ClearHistory" });
try {
Services.obs.notifyObservers(null, "browser:purge-session-history", "");
}
catch (e) { }
try {
var seer = Cc["@mozilla.org/network/seer;1"].getService(Ci.nsINetworkSeer);
seer.reset();
} catch (e) { }
},
get canClear()
{
// bug 347231: Always allow clearing history due to dependencies on
// the browser:purge-session-history notification. (like error console)
return true;
}
},
formdata: {
clear: function ()
{
FormHistory.update({ op: "remove" });
},
canClear: function (aCallback)
{
let count = 0;
let countDone = {
handleResult: function(aResult) { count = aResult; },
handleError: function(aError) { Cu.reportError(aError); },
handleCompletion: function(aReason) { aCallback(aReason == 0 && count > 0); }
};
FormHistory.count({}, countDone);
}
},
downloads: {
clear: function ()
{
downloads.iterate(function (dl) {
dl.remove();
});
},
get canClear()
{
return downloads.canClear;
}
},
downloadFiles: {
clear: function ()
{
downloads.iterate(function (dl) {
// Delete the downloaded files themselves
let f = dl.targetFile;
if (f.exists()) {
f.remove(false);
}
// Also delete downloads from history
dl.remove();
});
},
get canClear()
{
return downloads.canClear;
}
},
passwords: {
clear: function ()
{
Services.logins.removeAllLogins();
},
get canClear()
{
let count = Services.logins.countLogins("", "", ""); // count all logins
return (count > 0);
}
},
sessions: {
clear: function ()
{
// clear all auth tokens
var sdr = Cc["@mozilla.org/security/sdr;1"].getService(Ci.nsISecretDecoderRing);
sdr.logoutAndTeardown();
// clear FTP and plain HTTP auth sessions
Services.obs.notifyObservers(null, "net:clear-active-logins", null);
},
get canClear()
{
return true;
}
}
}
};
this.Sanitizer = new Sanitizer();