mobile/android/modules/Sanitizer.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/modules/Sanitizer.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,233 @@
     1.4 +// -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +let Cc = Components.classes;
    1.10 +let Ci = Components.interfaces;
    1.11 +let Cu = Components.utils;
    1.12 +
    1.13 +Cu.import("resource://gre/modules/Services.jsm");
    1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +Cu.import("resource://gre/modules/LoadContextInfo.jsm");
    1.16 +Cu.import("resource://gre/modules/FormHistory.jsm");
    1.17 +Cu.import("resource://gre/modules/Messaging.jsm");
    1.18 +
    1.19 +function dump(a) {
    1.20 +  Services.console.logStringMessage(a);
    1.21 +}
    1.22 +
    1.23 +this.EXPORTED_SYMBOLS = ["Sanitizer"];
    1.24 +
    1.25 +let downloads = {
    1.26 +  dlmgr: Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager),
    1.27 +
    1.28 +  iterate: function (aCallback) {
    1.29 +    let dlmgr = downloads.dlmgr;
    1.30 +    let dbConn = dlmgr.DBConnection;
    1.31 +    let stmt = dbConn.createStatement("SELECT id FROM moz_downloads WHERE " +
    1.32 +        "state = ? OR state = ? OR state = ? OR state = ? OR state = ? OR state = ?");
    1.33 +    stmt.bindInt32Parameter(0, Ci.nsIDownloadManager.DOWNLOAD_FINISHED);
    1.34 +    stmt.bindInt32Parameter(1, Ci.nsIDownloadManager.DOWNLOAD_FAILED);
    1.35 +    stmt.bindInt32Parameter(2, Ci.nsIDownloadManager.DOWNLOAD_CANCELED);
    1.36 +    stmt.bindInt32Parameter(3, Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_PARENTAL);
    1.37 +    stmt.bindInt32Parameter(4, Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_POLICY);
    1.38 +    stmt.bindInt32Parameter(5, Ci.nsIDownloadManager.DOWNLOAD_DIRTY);
    1.39 +    while (stmt.executeStep()) {
    1.40 +      aCallback(dlmgr.getDownload(stmt.row.id));
    1.41 +    }
    1.42 +    stmt.finalize();
    1.43 +  },
    1.44 +
    1.45 +  get canClear() {
    1.46 +    return this.dlmgr.canCleanUp;
    1.47 +  }
    1.48 +};
    1.49 +
    1.50 +function Sanitizer() {}
    1.51 +Sanitizer.prototype = {
    1.52 +  clearItem: function (aItemName)
    1.53 +  {
    1.54 +    let item = this.items[aItemName];
    1.55 +    let canClear = item.canClear;
    1.56 +    if (typeof canClear == "function") {
    1.57 +      canClear(function clearCallback(aCanClear) {
    1.58 +        if (aCanClear)
    1.59 +          item.clear();
    1.60 +      });
    1.61 +    } else if (canClear) {
    1.62 +      item.clear();
    1.63 +    }
    1.64 +  },
    1.65 +
    1.66 +  items: {
    1.67 +    cache: {
    1.68 +      clear: function ()
    1.69 +      {
    1.70 +        var cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService);
    1.71 +        try {
    1.72 +          cache.clear();
    1.73 +        } catch(er) {}
    1.74 +
    1.75 +        let imageCache = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
    1.76 +                                                         .getImgCacheForDocument(null);
    1.77 +        try {
    1.78 +          imageCache.clearCache(false); // true=chrome, false=content
    1.79 +        } catch(er) {}
    1.80 +      },
    1.81 +
    1.82 +      get canClear()
    1.83 +      {
    1.84 +        return true;
    1.85 +      }
    1.86 +    },
    1.87 +
    1.88 +    cookies: {
    1.89 +      clear: function ()
    1.90 +      {
    1.91 +        Services.cookies.removeAll();
    1.92 +      },
    1.93 +
    1.94 +      get canClear()
    1.95 +      {
    1.96 +        return true;
    1.97 +      }
    1.98 +    },
    1.99 +
   1.100 +    siteSettings: {
   1.101 +      clear: function ()
   1.102 +      {
   1.103 +        // Clear site-specific permissions like "Allow this site to open popups"
   1.104 +        Services.perms.removeAll();
   1.105 +
   1.106 +        // Clear site-specific settings like page-zoom level
   1.107 +        Cc["@mozilla.org/content-pref/service;1"]
   1.108 +          .getService(Ci.nsIContentPrefService2)
   1.109 +          .removeAllDomains(null);
   1.110 +
   1.111 +        // Clear "Never remember passwords for this site", which is not handled by
   1.112 +        // the permission manager
   1.113 +        var hosts = Services.logins.getAllDisabledHosts({})
   1.114 +        for (var host of hosts) {
   1.115 +          Services.logins.setLoginSavingEnabled(host, true);
   1.116 +        }
   1.117 +      },
   1.118 +
   1.119 +      get canClear()
   1.120 +      {
   1.121 +        return true;
   1.122 +      }
   1.123 +    },
   1.124 +
   1.125 +    offlineApps: {
   1.126 +      clear: function ()
   1.127 +      {
   1.128 +        var cacheService = Cc["@mozilla.org/netwerk/cache-storage-service;1"].getService(Ci.nsICacheStorageService);
   1.129 +        var appCacheStorage = cacheService.appCacheStorage(LoadContextInfo.default, null);
   1.130 +        try {
   1.131 +          appCacheStorage.asyncEvictStorage(null);
   1.132 +        } catch(er) {}
   1.133 +      },
   1.134 +
   1.135 +      get canClear()
   1.136 +      {
   1.137 +          return true;
   1.138 +      }
   1.139 +    },
   1.140 +
   1.141 +    history: {
   1.142 +      clear: function ()
   1.143 +      {
   1.144 +        sendMessageToJava({ type: "Sanitize:ClearHistory" });
   1.145 +
   1.146 +        try {
   1.147 +          Services.obs.notifyObservers(null, "browser:purge-session-history", "");
   1.148 +        }
   1.149 +        catch (e) { }
   1.150 +
   1.151 +        try {
   1.152 +          var seer = Cc["@mozilla.org/network/seer;1"].getService(Ci.nsINetworkSeer);
   1.153 +          seer.reset();
   1.154 +        } catch (e) { }
   1.155 +      },
   1.156 +
   1.157 +      get canClear()
   1.158 +      {
   1.159 +        // bug 347231: Always allow clearing history due to dependencies on
   1.160 +        // the browser:purge-session-history notification. (like error console)
   1.161 +        return true;
   1.162 +      }
   1.163 +    },
   1.164 +
   1.165 +    formdata: {
   1.166 +      clear: function ()
   1.167 +      {
   1.168 +        FormHistory.update({ op: "remove" });
   1.169 +      },
   1.170 +
   1.171 +      canClear: function (aCallback)
   1.172 +      {
   1.173 +        let count = 0;
   1.174 +        let countDone = {
   1.175 +          handleResult: function(aResult) { count = aResult; },
   1.176 +          handleError: function(aError) { Cu.reportError(aError); },
   1.177 +          handleCompletion: function(aReason) { aCallback(aReason == 0 && count > 0); }
   1.178 +        };
   1.179 +        FormHistory.count({}, countDone);
   1.180 +      }
   1.181 +    },
   1.182 +
   1.183 +    downloadFiles: {
   1.184 +      clear: function ()
   1.185 +      {
   1.186 +        downloads.iterate(function (dl) {
   1.187 +          // Delete the downloaded files themselves
   1.188 +          let f = dl.targetFile;
   1.189 +          if (f.exists()) {
   1.190 +            f.remove(false);
   1.191 +          }
   1.192 +
   1.193 +          // Also delete downloads from history
   1.194 +          dl.remove();
   1.195 +        });
   1.196 +      },
   1.197 +
   1.198 +      get canClear()
   1.199 +      {
   1.200 +        return downloads.canClear;
   1.201 +      }
   1.202 +    },
   1.203 +
   1.204 +    passwords: {
   1.205 +      clear: function ()
   1.206 +      {
   1.207 +        Services.logins.removeAllLogins();
   1.208 +      },
   1.209 +
   1.210 +      get canClear()
   1.211 +      {
   1.212 +        let count = Services.logins.countLogins("", "", ""); // count all logins
   1.213 +        return (count > 0);
   1.214 +      }
   1.215 +    },
   1.216 +
   1.217 +    sessions: {
   1.218 +      clear: function ()
   1.219 +      {
   1.220 +        // clear all auth tokens
   1.221 +        var sdr = Cc["@mozilla.org/security/sdr;1"].getService(Ci.nsISecretDecoderRing);
   1.222 +        sdr.logoutAndTeardown();
   1.223 +
   1.224 +        // clear FTP and plain HTTP auth sessions
   1.225 +        Services.obs.notifyObservers(null, "net:clear-active-logins", null);
   1.226 +      },
   1.227 +
   1.228 +      get canClear()
   1.229 +      {
   1.230 +        return true;
   1.231 +      }
   1.232 +    }
   1.233 +  }
   1.234 +};
   1.235 +
   1.236 +this.Sanitizer = new Sanitizer();

mercurial