michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: // XXX: This should all be moved into the dbservice class so it happens michael@0: // in the background thread. michael@0: michael@0: /** michael@0: * Abstract base class for a lookup table. michael@0: * @construction michael@0: */ michael@0: function UrlClassifierTable() { michael@0: this.debugZone = "urlclassifier-table"; michael@0: this.name = ''; michael@0: this.needsUpdate = false; michael@0: this.enchashDecrypter_ = new PROT_EnchashDecrypter(); michael@0: this.wrappedJSObject = this; michael@0: } michael@0: michael@0: UrlClassifierTable.prototype.QueryInterface = function(iid) { michael@0: if (iid.equals(Components.interfaces.nsISupports) || michael@0: iid.equals(Components.interfaces.nsIUrlClassifierTable)) michael@0: return this; michael@0: michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: michael@0: /** michael@0: * Subclasses need to implement this method. michael@0: */ michael@0: UrlClassifierTable.prototype.exists = function(url, callback) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: // Url table implementation michael@0: function UrlClassifierTableUrl() { michael@0: UrlClassifierTable.call(this); michael@0: } michael@0: UrlClassifierTableUrl.inherits(UrlClassifierTable); michael@0: michael@0: /** michael@0: * Look up a URL in a URL table michael@0: */ michael@0: UrlClassifierTableUrl.prototype.exists = function(url, callback) { michael@0: // nsIUrlClassifierUtils.canonicalizeURL is the old way of canonicalizing a michael@0: // URL. Unfortunately, it doesn't normalize numeric domains so alternate IP michael@0: // formats (hex, octal, etc) won't trigger a match. michael@0: // this.enchashDecrypter_.getCanonicalUrl does the right thing and michael@0: // normalizes a URL to 4 decimal numbers, but the update server may still be michael@0: // giving us encoded IP addresses. So to be safe, we check both cases. michael@0: var urlUtils = Cc["@mozilla.org/url-classifier/utils;1"] michael@0: .getService(Ci.nsIUrlClassifierUtils); michael@0: var oldCanonicalized = urlUtils.canonicalizeURL(url); michael@0: var canonicalized = this.enchashDecrypter_.getCanonicalUrl(url); michael@0: G_Debug(this, "Looking up: " + url + " (" + oldCanonicalized + " and " + michael@0: canonicalized + ")"); michael@0: (new ExistsMultiQuerier([oldCanonicalized, canonicalized], michael@0: this.name, michael@0: callback)).run(); michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: // Domain table implementation michael@0: michael@0: function UrlClassifierTableDomain() { michael@0: UrlClassifierTable.call(this); michael@0: this.debugZone = "urlclassifier-table-domain"; michael@0: this.ioService_ = Cc["@mozilla.org/network/io-service;1"] michael@0: .getService(Ci.nsIIOService); michael@0: } michael@0: UrlClassifierTableDomain.inherits(UrlClassifierTable); michael@0: michael@0: /** michael@0: * Look up a URL in a domain table michael@0: * We also try to lookup domain + first path component (e.g., michael@0: * www.mozilla.org/products). michael@0: * michael@0: * @returns Boolean true if the url domain is in the table michael@0: */ michael@0: UrlClassifierTableDomain.prototype.exists = function(url, callback) { michael@0: var canonicalized = this.enchashDecrypter_.getCanonicalUrl(url); michael@0: var urlObj = this.ioService_.newURI(canonicalized, null, null); michael@0: var host = ''; michael@0: try { michael@0: host = urlObj.host; michael@0: } catch (e) { } michael@0: var hostComponents = host.split("."); michael@0: michael@0: // Try to get the path of the URL. Pseudo urls (like wyciwyg:) throw michael@0: // errors when trying to convert to an nsIURL so we wrap in a try/catch michael@0: // block. michael@0: var path = "" michael@0: try { michael@0: urlObj.QueryInterface(Ci.nsIURL); michael@0: path = urlObj.filePath; michael@0: } catch (e) { } michael@0: michael@0: var pathComponents = path.split("/"); michael@0: michael@0: // We don't have a good way map from hosts to domains, so we instead try michael@0: // each possibility. Could probably optimize to start at the second dot? michael@0: var possible = []; michael@0: for (var i = 0; i < hostComponents.length - 1; i++) { michael@0: host = hostComponents.slice(i).join("."); michael@0: possible.push(host); michael@0: michael@0: // The path starts with a "/", so we are interested in the second path michael@0: // component if it is available michael@0: if (pathComponents.length >= 2 && pathComponents[1].length > 0) { michael@0: host = host + "/" + pathComponents[1]; michael@0: possible.push(host); michael@0: } michael@0: } michael@0: michael@0: // Run the possible domains against the db. michael@0: (new ExistsMultiQuerier(possible, this.name, callback)).run(); michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: // Enchash table implementation michael@0: michael@0: function UrlClassifierTableEnchash() { michael@0: UrlClassifierTable.call(this); michael@0: this.debugZone = "urlclassifier-table-enchash"; michael@0: } michael@0: UrlClassifierTableEnchash.inherits(UrlClassifierTable); michael@0: michael@0: /** michael@0: * Look up a URL in an enchashDB. We try all sub domains (up to MAX_DOTS). michael@0: */ michael@0: UrlClassifierTableEnchash.prototype.exists = function(url, callback) { michael@0: url = this.enchashDecrypter_.getCanonicalUrl(url); michael@0: var host = this.enchashDecrypter_.getCanonicalHost(url, michael@0: PROT_EnchashDecrypter.MAX_DOTS); michael@0: michael@0: var possible = []; michael@0: for (var i = 0; i < PROT_EnchashDecrypter.MAX_DOTS + 1; i++) { michael@0: possible.push(host); michael@0: michael@0: var index = host.indexOf("."); michael@0: if (index == -1) michael@0: break; michael@0: host = host.substring(index + 1); michael@0: } michael@0: // Run the possible domains against the db. michael@0: (new EnchashMultiQuerier(possible, this.name, callback, url)).run(); michael@0: }