mobile/android/modules/Sanitizer.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 // -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 let Cc = Components.classes;
     7 let Ci = Components.interfaces;
     8 let Cu = Components.utils;
    10 Cu.import("resource://gre/modules/Services.jsm");
    11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    12 Cu.import("resource://gre/modules/LoadContextInfo.jsm");
    13 Cu.import("resource://gre/modules/FormHistory.jsm");
    14 Cu.import("resource://gre/modules/Messaging.jsm");
    16 function dump(a) {
    17   Services.console.logStringMessage(a);
    18 }
    20 this.EXPORTED_SYMBOLS = ["Sanitizer"];
    22 let downloads = {
    23   dlmgr: Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager),
    25   iterate: function (aCallback) {
    26     let dlmgr = downloads.dlmgr;
    27     let dbConn = dlmgr.DBConnection;
    28     let stmt = dbConn.createStatement("SELECT id FROM moz_downloads WHERE " +
    29         "state = ? OR state = ? OR state = ? OR state = ? OR state = ? OR state = ?");
    30     stmt.bindInt32Parameter(0, Ci.nsIDownloadManager.DOWNLOAD_FINISHED);
    31     stmt.bindInt32Parameter(1, Ci.nsIDownloadManager.DOWNLOAD_FAILED);
    32     stmt.bindInt32Parameter(2, Ci.nsIDownloadManager.DOWNLOAD_CANCELED);
    33     stmt.bindInt32Parameter(3, Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_PARENTAL);
    34     stmt.bindInt32Parameter(4, Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_POLICY);
    35     stmt.bindInt32Parameter(5, Ci.nsIDownloadManager.DOWNLOAD_DIRTY);
    36     while (stmt.executeStep()) {
    37       aCallback(dlmgr.getDownload(stmt.row.id));
    38     }
    39     stmt.finalize();
    40   },
    42   get canClear() {
    43     return this.dlmgr.canCleanUp;
    44   }
    45 };
    47 function Sanitizer() {}
    48 Sanitizer.prototype = {
    49   clearItem: function (aItemName)
    50   {
    51     let item = this.items[aItemName];
    52     let canClear = item.canClear;
    53     if (typeof canClear == "function") {
    54       canClear(function clearCallback(aCanClear) {
    55         if (aCanClear)
    56           item.clear();
    57       });
    58     } else if (canClear) {
    59       item.clear();
    60     }
    61   },
    63   items: {
    64     cache: {
    65       clear: function ()
    66       {
    67         var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService);
    68         try {
    69           cache.clear();
    70         } catch(er) {}
    72         let imageCache = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
    73                                                          .getImgCacheForDocument(null);
    74         try {
    75           imageCache.clearCache(false); // true=chrome, false=content
    76         } catch(er) {}
    77       },
    79       get canClear()
    80       {
    81         return true;
    82       }
    83     },
    85     cookies: {
    86       clear: function ()
    87       {
    88         Services.cookies.removeAll();
    89       },
    91       get canClear()
    92       {
    93         return true;
    94       }
    95     },
    97     siteSettings: {
    98       clear: function ()
    99       {
   100         // Clear site-specific permissions like "Allow this site to open popups"
   101         Services.perms.removeAll();
   103         // Clear site-specific settings like page-zoom level
   104         Cc["@mozilla.org/content-pref/service;1"]
   105           .getService(Ci.nsIContentPrefService2)
   106           .removeAllDomains(null);
   108         // Clear "Never remember passwords for this site", which is not handled by
   109         // the permission manager
   110         var hosts = Services.logins.getAllDisabledHosts({})
   111         for (var host of hosts) {
   112           Services.logins.setLoginSavingEnabled(host, true);
   113         }
   114       },
   116       get canClear()
   117       {
   118         return true;
   119       }
   120     },
   122     offlineApps: {
   123       clear: function ()
   124       {
   125         var cacheService = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService);
   126         var appCacheStorage = cacheService.appCacheStorage(LoadContextInfo.default, null);
   127         try {
   128           appCacheStorage.asyncEvictStorage(null);
   129         } catch(er) {}
   130       },
   132       get canClear()
   133       {
   134           return true;
   135       }
   136     },
   138     history: {
   139       clear: function ()
   140       {
   141         sendMessageToJava({ type: "Sanitize:ClearHistory" });
   143         try {
   144           Services.obs.notifyObservers(null, "browser:purge-session-history", "");
   145         }
   146         catch (e) { }
   148         try {
   149           var seer = Cc["@mozilla.org/network/seer;1"].getService(Ci.nsINetworkSeer);
   150           seer.reset();
   151         } catch (e) { }
   152       },
   154       get canClear()
   155       {
   156         // bug 347231: Always allow clearing history due to dependencies on
   157         // the browser:purge-session-history notification. (like error console)
   158         return true;
   159       }
   160     },
   162     formdata: {
   163       clear: function ()
   164       {
   165         FormHistory.update({ op: "remove" });
   166       },
   168       canClear: function (aCallback)
   169       {
   170         let count = 0;
   171         let countDone = {
   172           handleResult: function(aResult) { count = aResult; },
   173           handleError: function(aError) { Cu.reportError(aError); },
   174           handleCompletion: function(aReason) { aCallback(aReason == 0 && count > 0); }
   175         };
   176         FormHistory.count({}, countDone);
   177       }
   178     },
   180     downloadFiles: {
   181       clear: function ()
   182       {
   183         downloads.iterate(function (dl) {
   184           // Delete the downloaded files themselves
   185           let f = dl.targetFile;
   186           if (f.exists()) {
   187             f.remove(false);
   188           }
   190           // Also delete downloads from history
   191           dl.remove();
   192         });
   193       },
   195       get canClear()
   196       {
   197         return downloads.canClear;
   198       }
   199     },
   201     passwords: {
   202       clear: function ()
   203       {
   204         Services.logins.removeAllLogins();
   205       },
   207       get canClear()
   208       {
   209         let count = Services.logins.countLogins("", "", ""); // count all logins
   210         return (count > 0);
   211       }
   212     },
   214     sessions: {
   215       clear: function ()
   216       {
   217         // clear all auth tokens
   218         var sdr = Cc["@mozilla.org/security/sdr;1"].getService(Ci.nsISecretDecoderRing);
   219         sdr.logoutAndTeardown();
   221         // clear FTP and plain HTTP auth sessions
   222         Services.obs.notifyObservers(null, "net:clear-active-logins", null);
   223       },
   225       get canClear()
   226       {
   227         return true;
   228       }
   229     }
   230   }
   231 };
   233 this.Sanitizer = new Sanitizer();

mercurial