toolkit/components/url-classifier/content/trtable.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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 // XXX: This should all be moved into the dbservice class so it happens
michael@0 6 // in the background thread.
michael@0 7
michael@0 8 /**
michael@0 9 * Abstract base class for a lookup table.
michael@0 10 * @construction
michael@0 11 */
michael@0 12 function UrlClassifierTable() {
michael@0 13 this.debugZone = "urlclassifier-table";
michael@0 14 this.name = '';
michael@0 15 this.needsUpdate = false;
michael@0 16 this.enchashDecrypter_ = new PROT_EnchashDecrypter();
michael@0 17 this.wrappedJSObject = this;
michael@0 18 }
michael@0 19
michael@0 20 UrlClassifierTable.prototype.QueryInterface = function(iid) {
michael@0 21 if (iid.equals(Components.interfaces.nsISupports) ||
michael@0 22 iid.equals(Components.interfaces.nsIUrlClassifierTable))
michael@0 23 return this;
michael@0 24
michael@0 25 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 26 }
michael@0 27
michael@0 28 /**
michael@0 29 * Subclasses need to implement this method.
michael@0 30 */
michael@0 31 UrlClassifierTable.prototype.exists = function(url, callback) {
michael@0 32 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
michael@0 33 }
michael@0 34
michael@0 35 /////////////////////////////////////////////////////////////////////
michael@0 36 // Url table implementation
michael@0 37 function UrlClassifierTableUrl() {
michael@0 38 UrlClassifierTable.call(this);
michael@0 39 }
michael@0 40 UrlClassifierTableUrl.inherits(UrlClassifierTable);
michael@0 41
michael@0 42 /**
michael@0 43 * Look up a URL in a URL table
michael@0 44 */
michael@0 45 UrlClassifierTableUrl.prototype.exists = function(url, callback) {
michael@0 46 // nsIUrlClassifierUtils.canonicalizeURL is the old way of canonicalizing a
michael@0 47 // URL. Unfortunately, it doesn't normalize numeric domains so alternate IP
michael@0 48 // formats (hex, octal, etc) won't trigger a match.
michael@0 49 // this.enchashDecrypter_.getCanonicalUrl does the right thing and
michael@0 50 // normalizes a URL to 4 decimal numbers, but the update server may still be
michael@0 51 // giving us encoded IP addresses. So to be safe, we check both cases.
michael@0 52 var urlUtils = Cc["@mozilla.org/url-classifier/utils;1"]
michael@0 53 .getService(Ci.nsIUrlClassifierUtils);
michael@0 54 var oldCanonicalized = urlUtils.canonicalizeURL(url);
michael@0 55 var canonicalized = this.enchashDecrypter_.getCanonicalUrl(url);
michael@0 56 G_Debug(this, "Looking up: " + url + " (" + oldCanonicalized + " and " +
michael@0 57 canonicalized + ")");
michael@0 58 (new ExistsMultiQuerier([oldCanonicalized, canonicalized],
michael@0 59 this.name,
michael@0 60 callback)).run();
michael@0 61 }
michael@0 62
michael@0 63 /////////////////////////////////////////////////////////////////////
michael@0 64 // Domain table implementation
michael@0 65
michael@0 66 function UrlClassifierTableDomain() {
michael@0 67 UrlClassifierTable.call(this);
michael@0 68 this.debugZone = "urlclassifier-table-domain";
michael@0 69 this.ioService_ = Cc["@mozilla.org/network/io-service;1"]
michael@0 70 .getService(Ci.nsIIOService);
michael@0 71 }
michael@0 72 UrlClassifierTableDomain.inherits(UrlClassifierTable);
michael@0 73
michael@0 74 /**
michael@0 75 * Look up a URL in a domain table
michael@0 76 * We also try to lookup domain + first path component (e.g.,
michael@0 77 * www.mozilla.org/products).
michael@0 78 *
michael@0 79 * @returns Boolean true if the url domain is in the table
michael@0 80 */
michael@0 81 UrlClassifierTableDomain.prototype.exists = function(url, callback) {
michael@0 82 var canonicalized = this.enchashDecrypter_.getCanonicalUrl(url);
michael@0 83 var urlObj = this.ioService_.newURI(canonicalized, null, null);
michael@0 84 var host = '';
michael@0 85 try {
michael@0 86 host = urlObj.host;
michael@0 87 } catch (e) { }
michael@0 88 var hostComponents = host.split(".");
michael@0 89
michael@0 90 // Try to get the path of the URL. Pseudo urls (like wyciwyg:) throw
michael@0 91 // errors when trying to convert to an nsIURL so we wrap in a try/catch
michael@0 92 // block.
michael@0 93 var path = ""
michael@0 94 try {
michael@0 95 urlObj.QueryInterface(Ci.nsIURL);
michael@0 96 path = urlObj.filePath;
michael@0 97 } catch (e) { }
michael@0 98
michael@0 99 var pathComponents = path.split("/");
michael@0 100
michael@0 101 // We don't have a good way map from hosts to domains, so we instead try
michael@0 102 // each possibility. Could probably optimize to start at the second dot?
michael@0 103 var possible = [];
michael@0 104 for (var i = 0; i < hostComponents.length - 1; i++) {
michael@0 105 host = hostComponents.slice(i).join(".");
michael@0 106 possible.push(host);
michael@0 107
michael@0 108 // The path starts with a "/", so we are interested in the second path
michael@0 109 // component if it is available
michael@0 110 if (pathComponents.length >= 2 && pathComponents[1].length > 0) {
michael@0 111 host = host + "/" + pathComponents[1];
michael@0 112 possible.push(host);
michael@0 113 }
michael@0 114 }
michael@0 115
michael@0 116 // Run the possible domains against the db.
michael@0 117 (new ExistsMultiQuerier(possible, this.name, callback)).run();
michael@0 118 }
michael@0 119
michael@0 120 /////////////////////////////////////////////////////////////////////
michael@0 121 // Enchash table implementation
michael@0 122
michael@0 123 function UrlClassifierTableEnchash() {
michael@0 124 UrlClassifierTable.call(this);
michael@0 125 this.debugZone = "urlclassifier-table-enchash";
michael@0 126 }
michael@0 127 UrlClassifierTableEnchash.inherits(UrlClassifierTable);
michael@0 128
michael@0 129 /**
michael@0 130 * Look up a URL in an enchashDB. We try all sub domains (up to MAX_DOTS).
michael@0 131 */
michael@0 132 UrlClassifierTableEnchash.prototype.exists = function(url, callback) {
michael@0 133 url = this.enchashDecrypter_.getCanonicalUrl(url);
michael@0 134 var host = this.enchashDecrypter_.getCanonicalHost(url,
michael@0 135 PROT_EnchashDecrypter.MAX_DOTS);
michael@0 136
michael@0 137 var possible = [];
michael@0 138 for (var i = 0; i < PROT_EnchashDecrypter.MAX_DOTS + 1; i++) {
michael@0 139 possible.push(host);
michael@0 140
michael@0 141 var index = host.indexOf(".");
michael@0 142 if (index == -1)
michael@0 143 break;
michael@0 144 host = host.substring(index + 1);
michael@0 145 }
michael@0 146 // Run the possible domains against the db.
michael@0 147 (new EnchashMultiQuerier(possible, this.name, callback, url)).run();
michael@0 148 }

mercurial