browser/metro/components/SafeBrowsing.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/components/SafeBrowsing.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,168 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Cc = Components.classes;
     1.9 +const Ci = Components.interfaces;
    1.10 +const Cu = Components.utils;
    1.11 +const Cr = Components.results;
    1.12 +
    1.13 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.14 +Cu.import("resource://gre/modules/Services.jsm");
    1.15 +
    1.16 +const kPhishWardenEnabledPref = "browser.safebrowsing.enabled";
    1.17 +const kMalwareWardenEnabledPref = "browser.safebrowsing.malware.enabled";
    1.18 +
    1.19 +// This XPCOM object doesn't have a public interface. It just works quietly in the background
    1.20 +function SafeBrowsing() {
    1.21 +  this.listManager = null;
    1.22 +
    1.23 +  // Once we register tables, their respective names will be listed here.
    1.24 +  this.phishing = {
    1.25 +    pref: kPhishWardenEnabledPref,
    1.26 +    blackTables: [],
    1.27 +    whiteTables: []
    1.28 +  };
    1.29 +  this.malware = {
    1.30 +    pref: kMalwareWardenEnabledPref,
    1.31 +    blackTables: [],
    1.32 +    whiteTables: []
    1.33 +  };
    1.34 +
    1.35 +  // Get notifications when the phishing or malware warden enabled pref changes
    1.36 +  Services.prefs.addObserver(kPhishWardenEnabledPref, this, true);
    1.37 +  Services.prefs.addObserver(kMalwareWardenEnabledPref, this, true);
    1.38 +}
    1.39 +
    1.40 +SafeBrowsing.prototype = {
    1.41 +  classID: Components.ID("{aadaed90-6c03-42d0-924a-fc61198ff283}"),
    1.42 +
    1.43 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsISessionStore,
    1.44 +                                         Ci.nsIDOMEventListener,
    1.45 +                                         Ci.nsIObserver,
    1.46 +                                         Ci.nsISupportsWeakReference]),
    1.47 +
    1.48 +  observe: function sb_observe(aSubject, aTopic, aData) {
    1.49 +    switch (aTopic) {
    1.50 +      case "app-startup":
    1.51 +        Services.obs.addObserver(this, "final-ui-startup", true);
    1.52 +        Services.obs.addObserver(this, "xpcom-shutdown", true);
    1.53 +        break;
    1.54 +      case "final-ui-startup":
    1.55 +        Services.obs.removeObserver(this, "final-ui-startup");
    1.56 +        this._startup();
    1.57 +        break;
    1.58 +      case "xpcom-shutdown":
    1.59 +        Services.obs.removeObserver(this, "xpcom-shutdown");
    1.60 +        this._shutdown();
    1.61 +        break;
    1.62 +      case "nsPref:changed":
    1.63 +        if (aData == kPhishWardenEnabledPref)
    1.64 +          this.maybeToggleUpdateChecking(this.phishing);
    1.65 +        else if (aData == kMalwareWardenEnabledPref)
    1.66 +          this.maybeToggleUpdateChecking(this.malware);
    1.67 +        break;
    1.68 +    }
    1.69 +  },
    1.70 +
    1.71 +  _startup: function sb_startup() {
    1.72 +    this.listManager = Cc["@mozilla.org/url-classifier/listmanager;1"].getService(Ci.nsIUrlListManager);
    1.73 +
    1.74 +    // Add a test chunk to the database
    1.75 +    let testData = "itisatrap.org/firefox/its-an-attack.html";
    1.76 +    let testUpdate =
    1.77 +      "n:1000\ni:test-malware-simple\nad:1\n" +
    1.78 +      "a:1:32:" + testData.length + "\n" +
    1.79 +      testData;
    1.80 +
    1.81 +    testData = "itisatrap.org/firefox/its-a-trap.html";
    1.82 +    testUpdate +=
    1.83 +      "n:1000\ni:test-phish-simple\nad:1\n" +
    1.84 +      "a:1:32:" + testData.length + "\n" +
    1.85 +      testData;
    1.86 +
    1.87 +    let dbService = Cc["@mozilla.org/url-classifier/dbservice;1"].getService(Ci.nsIUrlClassifierDBService);
    1.88 +
    1.89 +    let listener = {
    1.90 +      QueryInterface: function(aIID) {
    1.91 +        if (aIID.equals(Ci.nsISupports) || aIID.equals(Ci.nsIUrlClassifierUpdateObserver))
    1.92 +          return this;
    1.93 +        throw Cr.NS_ERROR_NO_INTERFACE;
    1.94 +      },
    1.95 +
    1.96 +      updateUrlRequested: function(aURL) { },
    1.97 +      streamFinished: function(aStatus) { },
    1.98 +      updateError: function(aErrorCode) { },
    1.99 +      updateSuccess: function(aRequestedTimeout) { }
   1.100 +    };
   1.101 +
   1.102 +    try {
   1.103 +      dbService.beginUpdate(listener, "test-malware-simple,test-phish-simple", "");
   1.104 +      dbService.beginStream("", "");
   1.105 +      dbService.updateStream(testUpdate);
   1.106 +      dbService.finishStream();
   1.107 +      dbService.finishUpdate();
   1.108 +    } catch(ex) {}
   1.109 +
   1.110 +    this.registerBlackTable("goog-malware-shavar", this.malware);
   1.111 +    this.maybeToggleUpdateChecking(this.malware);
   1.112 +
   1.113 +    this.registerBlackTable("goog-phish-shavar", this.phishing);
   1.114 +    this.maybeToggleUpdateChecking(this.phishing);
   1.115 +  },
   1.116 +
   1.117 +  _shutdown: function sb_shutdown() {
   1.118 +    Services.prefs.removeObserver(kPhishWardenEnabledPref, this);
   1.119 +    Services.prefs.removeObserver(kMalwareWardenEnabledPref, this);
   1.120 +
   1.121 +    this.listManager = null;
   1.122 +  },
   1.123 +
   1.124 +  enableBlacklistTableUpdates: function sb_enableBlacklistTableUpdates(aWarden) {
   1.125 +    for (let i = 0; i < aWarden.blackTables.length; ++i) {
   1.126 +      this.listManager.enableUpdate(aWarden.blackTables[i]);
   1.127 +    }
   1.128 +  },
   1.129 +
   1.130 +  disableBlacklistTableUpdates: function sb_disableBlacklistTableUpdates(aWarden) {
   1.131 +    for (let i = 0; i < aWarden.blackTables.length; ++i) {
   1.132 +      this.listManager.disableUpdate(aWarden.blackTables[i]);
   1.133 +    }
   1.134 +  },
   1.135 +
   1.136 +  enableWhitelistTableUpdates: function sb_enableWhitelistTableUpdates(aWarden) {
   1.137 +    for (let i = 0; i < this.whiteTables.length; ++i) {
   1.138 +      this.listManager.enableUpdate(this.whiteTables[i]);
   1.139 +    }
   1.140 +  },
   1.141 +
   1.142 +  disableWhitelistTableUpdates: function sb_disableWhitelistTableUpdates(aWarden) {
   1.143 +    for (let i = 0; i < aWarden.whiteTables.length; ++i) {
   1.144 +      this.listManager.disableUpdate(aWarden.whiteTables[i]);
   1.145 +    }
   1.146 +  },
   1.147 +
   1.148 +  registerBlackTable: function sb_registerBlackTable(aTableName, aWarden) {
   1.149 +    let result = this.listManager.registerTable(aTableName, false);
   1.150 +    if (result)
   1.151 +      aWarden.blackTables.push(aTableName);
   1.152 +    return result;
   1.153 +  },
   1.154 +
   1.155 +  registerWhiteTable: function sb_registerWhiteTable(aTableName, aWarden) {
   1.156 +    let result = this.listManager.registerTable(aTableName, false);
   1.157 +    if (result)
   1.158 +      aWarden.whiteTables.push(aTableName);
   1.159 +    return result;
   1.160 +  },
   1.161 +
   1.162 +  maybeToggleUpdateChecking: function sb_maybeToggleUpdateChecking(aWarden) {
   1.163 +    let enabled = Services.prefs.getBoolPref(aWarden.pref);
   1.164 +    if (enabled)
   1.165 +      this.enableBlacklistTableUpdates(aWarden);
   1.166 +    else
   1.167 +      this.disableBlacklistTableUpdates(aWarden);
   1.168 +  }
   1.169 +}
   1.170 +
   1.171 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SafeBrowsing]);

mercurial