1 # -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 # ***** BEGIN LICENSE BLOCK *****
3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 # The contents of this file are subject to the Mozilla Public License Version
6 # 1.1 (the "License"); you may not use this file except in compliance with
7 # the License. You may obtain a copy of the License at
8 # http://www.mozilla.org/MPL/
10 # Software distributed under the License is distributed on an "AS IS" basis,
11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 # for the specific language governing rights and limitations under the
15 # The Original Code is the Firefox Sanitizer.
17 # The Initial Developer of the Original Code is
19 # Portions created by the Initial Developer are Copyright (C) 2005
20 # the Initial Developer. All Rights Reserved.
23 # Ben Goodger <ben@mozilla.org>
24 # Giorgio Maone <g.maone@informaction.com>
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
38 # ***** END LICENSE BLOCK *****
40 function Sanitizer() {}
41 Sanitizer.prototype = {
42 // warning to the caller: this one may raise an exception (e.g. bug #265028)
43 clearItem: function (aItemName)
45 if (this.items[aItemName].canClear)
46 this.items[aItemName].clear();
49 canClearItem: function (aItemName)
51 return this.items[aItemName].canClear;
54 _prefDomain: "privacy.item.",
55 getNameFromPreference: function (aPreferenceName)
57 return aPreferenceName.substr(this._prefDomain.length);
61 * Deletes privacy sensitive data in a batch, according to user preferences
63 * @returns null if everything's fine; an object in the form
64 * { itemName: error, ... } on (partial) failure
68 var psvc = Components.classes["@mozilla.org/preferences-service;1"]
69 .getService(Components.interfaces.nsIPrefService);
70 var branch = psvc.getBranch(this._prefDomain);
72 for (var itemName in this.items) {
73 var item = this.items[itemName];
74 if ("clear" in item && item.canClear && branch.getBoolPref(itemName)) {
75 // Some of these clear() may raise exceptions (see bug #265028)
76 // to sanitize as much as possible, we catch and store them,
77 // rather than fail fast.
78 // Callers should check returned errors and give user feedback
79 // about items that could not be sanitized
85 errors[itemName] = er;
86 dump("Error sanitizing " + itemName + ": " + er + "\n");
97 const cc = Components.classes;
98 const ci = Components.interfaces;
99 var cacheService = cc["@mozilla.org/network/cache-service;1"]
100 .getService(ci.nsICacheService);
102 cacheService.evictEntries(ci.nsICache.STORE_ANYWHERE);
115 var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"]
116 .getService(Components.interfaces.nsICookieManager);
117 cookieMgr.removeAll();
129 var globalHistory = Components.classes["@mozilla.org/browser/global-history;2"]
130 .getService(Components.interfaces.nsIBrowserHistory);
131 globalHistory.removeAllPages();
134 var os = Components.classes["@mozilla.org/observer-service;1"]
135 .getService(Components.interfaces.nsIObserverService);
136 os.notifyObservers(null, "browser:purge-session-history", "");
140 // Clear last URL of the Open Web Location dialog
141 var prefs = Components.classes["@mozilla.org/preferences-service;1"]
142 .getService(Components.interfaces.nsIPrefBranch2);
144 prefs.clearUserPref("general.open_location.last_url");
151 // bug 347231: Always allow clearing history due to dependencies on
152 // the browser:purge-session-history notification. (like error console)
160 //Clear undo history of all searchBars
161 var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
162 var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
163 var windows = windowManagerInterface.getEnumerator("navigator:browser");
164 while (windows.hasMoreElements()) {
165 var searchBar = windows.getNext().document.getElementById("searchbar");
167 searchBar.value = "";
168 searchBar.textbox.editor.transactionManager.clear();
172 var formHistory = Components.classes["@mozilla.org/satchel/form-history;1"]
173 .getService(Components.interfaces.nsIFormHistory2);
174 formHistory.removeAllEntries();
179 var formHistory = Components.classes["@mozilla.org/satchel/form-history;1"]
180 .getService(Components.interfaces.nsIFormHistory2);
181 return formHistory.hasEntries;
188 var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
189 .getService(Components.interfaces.nsIDownloadManager);
195 var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
196 .getService(Components.interfaces.nsIDownloadManager);
197 return dlMgr.canCleanUp;
204 var pwmgr = Components.classes["@mozilla.org/login-manager;1"]
205 .getService(Components.interfaces.nsILoginManager);
206 pwmgr.removeAllLogins();
211 var pwmgr = Components.classes["@mozilla.org/login-manager;1"]
212 .getService(Components.interfaces.nsILoginManager);
213 var count = pwmgr.countLogins("", "", ""); // count all logins
221 // clear all auth tokens
222 var sdr = Components.classes["@mozilla.org/security/sdr;1"]
223 .getService(Components.interfaces.nsISecretDecoderRing);
224 sdr.logoutAndTeardown();
226 // clear plain HTTP auth sessions
227 var authMgr = Components.classes['@mozilla.org/network/http-auth-manager;1']
228 .getService(Components.interfaces.nsIHttpAuthManager);
243 Sanitizer.prefDomain = "privacy.sanitize.";
244 Sanitizer.prefPrompt = "promptOnSanitize";
245 Sanitizer.prefShutdown = "sanitizeOnShutdown";
246 Sanitizer.prefDidShutdown = "didShutdownSanitize";
248 Sanitizer._prefs = null;
249 Sanitizer.__defineGetter__("prefs", function()
251 return Sanitizer._prefs ? Sanitizer._prefs
252 : Sanitizer._prefs = Components.classes["@mozilla.org/preferences-service;1"]
253 .getService(Components.interfaces.nsIPrefService)
254 .getBranch(Sanitizer.prefDomain);
257 // Shows sanitization UI
258 Sanitizer.showUI = function(aParentWindow)
260 var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
261 .getService(Components.interfaces.nsIWindowWatcher);
263 ww.openWindow(null, // make this an app-modal window on Mac
265 ww.openWindow(aParentWindow,
267 "chrome://browser/content/sanitize.xul",
269 "chrome,titlebar,centerscreen,modal",
274 * Deletes privacy sensitive data in a batch, optionally showing the
275 * sanitize UI, according to user preferences
277 * @returns null if everything's fine (no error or displayed UI, which
278 * should handle errors);
279 * an object in the form { itemName: error, ... } on (partial) failure
281 Sanitizer.sanitize = function(aParentWindow)
283 if (Sanitizer.prefs.getBoolPref(Sanitizer.prefPrompt)) {
284 Sanitizer.showUI(aParentWindow);
287 return new Sanitizer().sanitize();
290 Sanitizer.onStartup = function()
292 // we check for unclean exit with pending sanitization
293 Sanitizer._checkAndSanitize();
296 Sanitizer.onShutdown = function()
298 // we check if sanitization is needed and perform it
299 Sanitizer._checkAndSanitize();
302 // this is called on startup and shutdown, to perform pending sanitizations
303 Sanitizer._checkAndSanitize = function()
305 const prefs = Sanitizer.prefs;
306 if (prefs.getBoolPref(Sanitizer.prefShutdown) &&
307 !prefs.prefHasUserValue(Sanitizer.prefDidShutdown)) {
308 // this is a shutdown or a startup after an unclean exit
309 Sanitizer.sanitize(null) || // sanitize() returns null on full success
310 prefs.setBoolPref(Sanitizer.prefDidShutdown, true);