gecko/mobile/android/chrome/content/sanitize.js

284 lines
7.7 KiB
JavaScript
Raw Normal View History

// -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2012-05-21 04:12:37 -07:00
/* 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/. */
2008-09-06 23:54:06 -07:00
function Sanitizer() {}
Sanitizer.prototype = {
// warning to the caller: this one may raise an exception (e.g. bug #265028)
clearItem: function (aItemName)
{
if (this.items[aItemName].canClear)
this.items[aItemName].clear();
},
canClearItem: function (aItemName)
{
return this.items[aItemName].canClear;
},
2008-09-06 23:54:06 -07:00
_prefDomain: "privacy.item.",
getNameFromPreference: function (aPreferenceName)
{
return aPreferenceName.substr(this._prefDomain.length);
},
2008-09-06 23:54:06 -07:00
/**
* Deletes privacy sensitive data in a batch, according to user preferences
*
* @returns null if everything's fine; an object in the form
* { itemName: error, ... } on (partial) failure
*/
sanitize: function ()
{
var branch = Services.prefs.getBranch(this._prefDomain);
2008-09-06 23:54:06 -07:00
var errors = null;
for (var itemName in this.items) {
var item = this.items[itemName];
if ("clear" in item && item.canClear && branch.getBoolPref(itemName)) {
// Some of these clear() may raise exceptions (see bug #265028)
// to sanitize as much as possible, we catch and store them,
2008-09-06 23:54:06 -07:00
// rather than fail fast.
// Callers should check returned errors and give user feedback
// about items that could not be sanitized
try {
item.clear();
} catch(er) {
if (!errors)
2008-09-06 23:54:06 -07:00
errors = {};
errors[itemName] = er;
dump("Error sanitizing " + itemName + ": " + er + "\n");
}
}
}
return errors;
},
2008-09-06 23:54:06 -07:00
items: {
cache: {
clear: function ()
{
var cacheService = Cc["@mozilla.org/network/cache-service;1"].getService(Ci.nsICacheService);
2008-09-06 23:54:06 -07:00
try {
cacheService.evictEntries(Ci.nsICache.STORE_ANYWHERE);
} catch(er) {}
let imageCache = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
.getImgCacheForDocument(null);
try {
imageCache.clearCache(false); // true=chrome, false=content
2008-09-06 23:54:06 -07:00
} catch(er) {}
},
2008-09-06 23:54:06 -07:00
get canClear()
{
return true;
}
},
2008-09-06 23:54:06 -07:00
cookies: {
clear: function ()
{
Services.cookies.removeAll();
// clear any network geolocation provider sessions
try {
var branch = Services.prefs.getBranch("geo.wifi.access_token.");
branch.deleteBranch("");
} catch (e) {dump(e);}
},
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
Services.contentPrefs.removeGroupedPrefs(null);
// Clear "Never remember passwords for this site", which is not handled by
// the permission manager
var hosts = Services.logins.getAllDisabledHosts({})
for each (var host in hosts) {
Services.logins.setLoginSavingEnabled(host, true);
}
},
get canClear()
{
return true;
}
},
2008-09-06 23:54:06 -07:00
offlineApps: {
clear: function ()
{
var cacheService = Cc["@mozilla.org/network/cache-service;1"].getService(Ci.nsICacheService);
2008-09-06 23:54:06 -07:00
try {
cacheService.evictEntries(Ci.nsICache.STORE_OFFLINE);
} catch(er) {}
},
get canClear()
{
return true;
}
},
history: {
clear: function ()
{
sendMessageToJava({ gecko: { type: "Sanitize:ClearHistory" } });
2008-09-06 23:54:06 -07:00
try {
Services.obs.notifyObservers(null, "browser:purge-session-history", "");
2008-09-06 23:54:06 -07:00
}
catch (e) { }
2008-09-06 23:54:06 -07:00
// Clear last URL of the Open Web Location dialog
try {
Services.prefs.clearUserPref("general.open_location.last_url");
2008-09-06 23:54:06 -07:00
}
catch (e) { }
},
2008-09-06 23:54:06 -07:00
get canClear()
{
// bug 347231: Always allow clearing history due to dependencies on
// the browser:purge-session-history notification. (like error console)
return true;
}
},
2008-09-06 23:54:06 -07:00
formdata: {
clear: function ()
{
//Clear undo history of all searchBars
var windows = Services.wm.getEnumerator("navigator:browser");
2008-09-06 23:54:06 -07:00
while (windows.hasMoreElements()) {
var searchBar = windows.getNext().document.getElementById("searchbar");
if (searchBar) {
searchBar.value = "";
searchBar.textbox.editor.transactionManager.clear();
}
}
var formHistory = Cc["@mozilla.org/satchel/form-history;1"].getService(Ci.nsIFormHistory2);
2008-09-06 23:54:06 -07:00
formHistory.removeAllEntries();
},
2008-09-06 23:54:06 -07:00
get canClear()
{
var formHistory = Cc["@mozilla.org/satchel/form-history;1"].getService(Ci.nsIFormHistory2);
2008-09-06 23:54:06 -07:00
return formHistory.hasEntries;
}
},
2008-09-06 23:54:06 -07:00
downloads: {
clear: function ()
{
var dlMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
2008-09-06 23:54:06 -07:00
dlMgr.cleanUp();
},
get canClear()
{
var dlMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
2008-09-06 23:54:06 -07:00
return dlMgr.canCleanUp;
}
},
2008-09-06 23:54:06 -07:00
passwords: {
clear: function ()
{
Services.logins.removeAllLogins();
2008-09-06 23:54:06 -07:00
},
2008-09-06 23:54:06 -07:00
get canClear()
{
let count = Services.logins.countLogins("", "", ""); // count all logins
2008-09-06 23:54:06 -07:00
return (count > 0);
}
},
2008-09-06 23:54:06 -07:00
sessions: {
clear: function ()
{
// clear all auth tokens
var sdr = Cc["@mozilla.org/security/sdr;1"].getService(Ci.nsISecretDecoderRing);
2008-09-06 23:54:06 -07:00
sdr.logoutAndTeardown();
// clear FTP and plain HTTP auth sessions
var os = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
os.notifyObservers(null, "net:clear-active-logins", null);
2008-09-06 23:54:06 -07:00
},
2008-09-06 23:54:06 -07:00
get canClear()
{
return true;
}
}
}
};
// "Static" members
Sanitizer.prefDomain = "privacy.sanitize.";
Sanitizer.prefShutdown = "sanitizeOnShutdown";
Sanitizer.prefDidShutdown = "didShutdownSanitize";
Sanitizer._prefs = null;
Sanitizer.__defineGetter__("prefs", function()
2008-09-06 23:54:06 -07:00
{
return Sanitizer._prefs ? Sanitizer._prefs
: Sanitizer._prefs = Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefService)
2008-09-06 23:54:06 -07:00
.getBranch(Sanitizer.prefDomain);
});
/**
* Deletes privacy sensitive data in a batch, optionally showing the
2008-09-06 23:54:06 -07:00
* sanitize UI, according to user preferences
*
* @returns null if everything's fine
2008-09-06 23:54:06 -07:00
* an object in the form { itemName: error, ... } on (partial) failure
*/
Sanitizer.sanitize = function()
2008-09-06 23:54:06 -07:00
{
return new Sanitizer().sanitize();
};
Sanitizer.onStartup = function()
2008-09-06 23:54:06 -07:00
{
// we check for unclean exit with pending sanitization
Sanitizer._checkAndSanitize();
};
Sanitizer.onShutdown = function()
2008-09-06 23:54:06 -07:00
{
// we check if sanitization is needed and perform it
Sanitizer._checkAndSanitize();
};
// this is called on startup and shutdown, to perform pending sanitizations
Sanitizer._checkAndSanitize = function()
2008-09-06 23:54:06 -07:00
{
const prefs = Sanitizer.prefs;
if (prefs.getBoolPref(Sanitizer.prefShutdown) &&
2008-09-06 23:54:06 -07:00
!prefs.prefHasUserValue(Sanitizer.prefDidShutdown)) {
// this is a shutdown or a startup after an unclean exit
Sanitizer.sanitize() || // sanitize() returns null on full success
2008-09-06 23:54:06 -07:00
prefs.setBoolPref(Sanitizer.prefDidShutdown, true);
}
};