browser/metro/components/SafeBrowsing.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const Cc = Components.classes;
     6 const Ci = Components.interfaces;
     7 const Cu = Components.utils;
     8 const Cr = Components.results;
    10 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    11 Cu.import("resource://gre/modules/Services.jsm");
    13 const kPhishWardenEnabledPref = "browser.safebrowsing.enabled";
    14 const kMalwareWardenEnabledPref = "browser.safebrowsing.malware.enabled";
    16 // This XPCOM object doesn't have a public interface. It just works quietly in the background
    17 function SafeBrowsing() {
    18   this.listManager = null;
    20   // Once we register tables, their respective names will be listed here.
    21   this.phishing = {
    22     pref: kPhishWardenEnabledPref,
    23     blackTables: [],
    24     whiteTables: []
    25   };
    26   this.malware = {
    27     pref: kMalwareWardenEnabledPref,
    28     blackTables: [],
    29     whiteTables: []
    30   };
    32   // Get notifications when the phishing or malware warden enabled pref changes
    33   Services.prefs.addObserver(kPhishWardenEnabledPref, this, true);
    34   Services.prefs.addObserver(kMalwareWardenEnabledPref, this, true);
    35 }
    37 SafeBrowsing.prototype = {
    38   classID: Components.ID("{aadaed90-6c03-42d0-924a-fc61198ff283}"),
    40   QueryInterface: XPCOMUtils.generateQI([Ci.nsISessionStore,
    41                                          Ci.nsIDOMEventListener,
    42                                          Ci.nsIObserver,
    43                                          Ci.nsISupportsWeakReference]),
    45   observe: function sb_observe(aSubject, aTopic, aData) {
    46     switch (aTopic) {
    47       case "app-startup":
    48         Services.obs.addObserver(this, "final-ui-startup", true);
    49         Services.obs.addObserver(this, "xpcom-shutdown", true);
    50         break;
    51       case "final-ui-startup":
    52         Services.obs.removeObserver(this, "final-ui-startup");
    53         this._startup();
    54         break;
    55       case "xpcom-shutdown":
    56         Services.obs.removeObserver(this, "xpcom-shutdown");
    57         this._shutdown();
    58         break;
    59       case "nsPref:changed":
    60         if (aData == kPhishWardenEnabledPref)
    61           this.maybeToggleUpdateChecking(this.phishing);
    62         else if (aData == kMalwareWardenEnabledPref)
    63           this.maybeToggleUpdateChecking(this.malware);
    64         break;
    65     }
    66   },
    68   _startup: function sb_startup() {
    69     this.listManager = Cc["@mozilla.org/url-classifier/listmanager;1"].getService(Ci.nsIUrlListManager);
    71     // Add a test chunk to the database
    72     let testData = "itisatrap.org/firefox/its-an-attack.html";
    73     let testUpdate =
    74       "n:1000\ni:test-malware-simple\nad:1\n" +
    75       "a:1:32:" + testData.length + "\n" +
    76       testData;
    78     testData = "itisatrap.org/firefox/its-a-trap.html";
    79     testUpdate +=
    80       "n:1000\ni:test-phish-simple\nad:1\n" +
    81       "a:1:32:" + testData.length + "\n" +
    82       testData;
    84     let dbService = Cc["@mozilla.org/url-classifier/dbservice;1"].getService(Ci.nsIUrlClassifierDBService);
    86     let listener = {
    87       QueryInterface: function(aIID) {
    88         if (aIID.equals(Ci.nsISupports) || aIID.equals(Ci.nsIUrlClassifierUpdateObserver))
    89           return this;
    90         throw Cr.NS_ERROR_NO_INTERFACE;
    91       },
    93       updateUrlRequested: function(aURL) { },
    94       streamFinished: function(aStatus) { },
    95       updateError: function(aErrorCode) { },
    96       updateSuccess: function(aRequestedTimeout) { }
    97     };
    99     try {
   100       dbService.beginUpdate(listener, "test-malware-simple,test-phish-simple", "");
   101       dbService.beginStream("", "");
   102       dbService.updateStream(testUpdate);
   103       dbService.finishStream();
   104       dbService.finishUpdate();
   105     } catch(ex) {}
   107     this.registerBlackTable("goog-malware-shavar", this.malware);
   108     this.maybeToggleUpdateChecking(this.malware);
   110     this.registerBlackTable("goog-phish-shavar", this.phishing);
   111     this.maybeToggleUpdateChecking(this.phishing);
   112   },
   114   _shutdown: function sb_shutdown() {
   115     Services.prefs.removeObserver(kPhishWardenEnabledPref, this);
   116     Services.prefs.removeObserver(kMalwareWardenEnabledPref, this);
   118     this.listManager = null;
   119   },
   121   enableBlacklistTableUpdates: function sb_enableBlacklistTableUpdates(aWarden) {
   122     for (let i = 0; i < aWarden.blackTables.length; ++i) {
   123       this.listManager.enableUpdate(aWarden.blackTables[i]);
   124     }
   125   },
   127   disableBlacklistTableUpdates: function sb_disableBlacklistTableUpdates(aWarden) {
   128     for (let i = 0; i < aWarden.blackTables.length; ++i) {
   129       this.listManager.disableUpdate(aWarden.blackTables[i]);
   130     }
   131   },
   133   enableWhitelistTableUpdates: function sb_enableWhitelistTableUpdates(aWarden) {
   134     for (let i = 0; i < this.whiteTables.length; ++i) {
   135       this.listManager.enableUpdate(this.whiteTables[i]);
   136     }
   137   },
   139   disableWhitelistTableUpdates: function sb_disableWhitelistTableUpdates(aWarden) {
   140     for (let i = 0; i < aWarden.whiteTables.length; ++i) {
   141       this.listManager.disableUpdate(aWarden.whiteTables[i]);
   142     }
   143   },
   145   registerBlackTable: function sb_registerBlackTable(aTableName, aWarden) {
   146     let result = this.listManager.registerTable(aTableName, false);
   147     if (result)
   148       aWarden.blackTables.push(aTableName);
   149     return result;
   150   },
   152   registerWhiteTable: function sb_registerWhiteTable(aTableName, aWarden) {
   153     let result = this.listManager.registerTable(aTableName, false);
   154     if (result)
   155       aWarden.whiteTables.push(aTableName);
   156     return result;
   157   },
   159   maybeToggleUpdateChecking: function sb_maybeToggleUpdateChecking(aWarden) {
   160     let enabled = Services.prefs.getBoolPref(aWarden.pref);
   161     if (enabled)
   162       this.enableBlacklistTableUpdates(aWarden);
   163     else
   164       this.disableBlacklistTableUpdates(aWarden);
   165   }
   166 }
   168 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SafeBrowsing]);

mercurial