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