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