Backed out changeset db55605b2a4c (relanding bug 121341)
[wine-gecko.git] / browser / modules / Sanitizer.jsm
blob279597aae7553854e5a08c3870908cfdc4e7f351
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
13 # License.
15 # The Original Code is the Firefox Sanitizer.
17 # The Initial Developer of the Original Code is
18 # Ben Goodger.
19 # Portions created by the Initial Developer are Copyright (C) 2005
20 # the Initial Developer. All Rights Reserved.
22 # Contributor(s):
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)
44   {
45     if (this.items[aItemName].canClear)
46       this.items[aItemName].clear();
47   },
49   canClearItem: function (aItemName)
50   {
51     return this.items[aItemName].canClear;
52   },
53   
54   _prefDomain: "privacy.item.",
55   getNameFromPreference: function (aPreferenceName)
56   {
57     return aPreferenceName.substr(this._prefDomain.length);
58   },
59   
60   /**
61    * Deletes privacy sensitive data in a batch, according to user preferences
62    *
63    * @returns  null if everything's fine;  an object in the form
64    *           { itemName: error, ... } on (partial) failure
65    */
66   sanitize: function ()
67   {
68     var psvc = Components.classes["@mozilla.org/preferences-service;1"]
69                          .getService(Components.interfaces.nsIPrefService);
70     var branch = psvc.getBranch(this._prefDomain);
71     var errors = null;
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
80         try {
81           item.clear();
82         } catch(er) {
83           if (!errors) 
84             errors = {};
85           errors[itemName] = er;
86           dump("Error sanitizing " + itemName + ": " + er + "\n");
87         }
88       }
89     }
90     return errors;
91   },
92   
93   items: {
94     cache: {
95       clear: function ()
96       {
97         const cc = Components.classes;
98         const ci = Components.interfaces;
99         var cacheService = cc["@mozilla.org/network/cache-service;1"]
100                              .getService(ci.nsICacheService);
101         try {
102           cacheService.evictEntries(ci.nsICache.STORE_ANYWHERE);
103         } catch(er) {}
104       },
105       
106       get canClear()
107       {
108         return true;
109       }
110     },
111     
112     cookies: {
113       clear: function ()
114       {
115         var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"]
116                                   .getService(Components.interfaces.nsICookieManager);
117         cookieMgr.removeAll();
118       },
119       
120       get canClear()
121       {
122         return true;
123       }
124     },
125     
126     history: {
127       clear: function ()
128       {
129         var globalHistory = Components.classes["@mozilla.org/browser/global-history;2"]
130                                       .getService(Components.interfaces.nsIBrowserHistory);
131         globalHistory.removeAllPages();
132         
133         try {
134           var os = Components.classes["@mozilla.org/observer-service;1"]
135                              .getService(Components.interfaces.nsIObserverService);
136           os.notifyObservers(null, "browser:purge-session-history", "");
137         }
138         catch (e) { }
139         
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);
143         try {
144           prefs.clearUserPref("general.open_location.last_url");
145         }
146         catch (e) { }
147       },
148       
149       get canClear()
150       {
151         // bug 347231: Always allow clearing history due to dependencies on
152         // the browser:purge-session-history notification. (like error console)
153         return true;
154       }
155     },
156     
157     formdata: {
158       clear: function ()
159       {
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");
166           if (searchBar) {
167             searchBar.value = "";
168             searchBar.textbox.editor.transactionManager.clear();
169           }
170         }
172         var formHistory = Components.classes["@mozilla.org/satchel/form-history;1"]
173                                     .getService(Components.interfaces.nsIFormHistory2);
174         formHistory.removeAllEntries();
175       },
176       
177       get canClear()
178       {
179         var formHistory = Components.classes["@mozilla.org/satchel/form-history;1"]
180                                     .getService(Components.interfaces.nsIFormHistory2);
181         return formHistory.hasEntries;
182       }
183     },
184     
185     downloads: {
186       clear: function ()
187       {
188         var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
189                               .getService(Components.interfaces.nsIDownloadManager);
190         dlMgr.cleanUp();
191       },
193       get canClear()
194       {
195         var dlMgr = Components.classes["@mozilla.org/download-manager;1"]
196                               .getService(Components.interfaces.nsIDownloadManager);
197         return dlMgr.canCleanUp;
198       }
199     },
200     
201     passwords: {
202       clear: function ()
203       {
204         var pwmgr = Components.classes["@mozilla.org/login-manager;1"]
205                               .getService(Components.interfaces.nsILoginManager);
206         pwmgr.removeAllLogins();
207       },
208       
209       get canClear()
210       {
211         var pwmgr = Components.classes["@mozilla.org/login-manager;1"]
212                               .getService(Components.interfaces.nsILoginManager);
213         var count = pwmgr.countLogins("", "", ""); // count all logins
214         return (count > 0);
215       }
216     },
217     
218     sessions: {
219       clear: function ()
220       {
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);
229         authMgr.clearAll();
230       },
231       
232       get canClear()
233       {
234         return true;
235       }
236     }
237   }
242 // "Static" members
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);
262 #ifdef XP_MACOSX
263   ww.openWindow(null, // make this an app-modal window on Mac
264 #else
265   ww.openWindow(aParentWindow,
266 #endif
267                 "chrome://browser/content/sanitize.xul",
268                 "Sanitize",
269                 "chrome,titlebar,centerscreen,modal",
270                 null);
273 /** 
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
280  */
281 Sanitizer.sanitize = function(aParentWindow) 
283   if (Sanitizer.prefs.getBoolPref(Sanitizer.prefPrompt)) {
284     Sanitizer.showUI(aParentWindow);
285     return null;
286   }
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);
311   }