1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
14 # The Original Code is Url Classifier code
16 # The Initial Developer of the Original Code is
18 # Portions created by the Initial Developer are Copyright (C) 2006
19 # the Initial Developer. All Rights Reserved.
22 # Tony Chang <tony@ponderer.org>
24 # Alternatively, the contents of this file may be used under the terms of
25 # either the GNU General Public License Version 2 or later (the "GPL"), or
26 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 # in which case the provisions of the GPL or the LGPL are applicable instead
28 # of those above. If you wish to allow use of your version of this file only
29 # under the terms of either the GPL or the LGPL, and not to allow others to
30 # use your version of this file under the terms of the MPL, indicate your
31 # decision by deleting the provisions above and replace them with the notice
32 # and other provisions required by the GPL or the LGPL. If you do not delete
33 # the provisions above, a recipient may use your version of this file under
34 # the terms of any one of the MPL, the GPL or the LGPL.
36 # ***** END LICENSE BLOCK *****
38 // XXX: This should all be moved into the dbservice class so it happens
39 // in the background thread.
42 * Abstract base class for a lookup table.
45 function UrlClassifierTable() {
46 this.debugZone = "urlclassifier-table";
48 this.needsUpdate = false;
49 this.enchashDecrypter_ = new PROT_EnchashDecrypter();
50 this.wrappedJSObject = this;
53 UrlClassifierTable.prototype.QueryInterface = function(iid) {
54 if (iid.equals(Components.interfaces.nsISupports) ||
55 iid.equals(Components.interfaces.nsIUrlClassifierTable))
57 Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
62 * Subclasses need to implment this method.
64 UrlClassifierTable.prototype.exists = function(url, callback) {
65 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
68 /////////////////////////////////////////////////////////////////////
69 // Url table implementation
70 function UrlClassifierTableUrl() {
71 UrlClassifierTable.call(this);
73 UrlClassifierTableUrl.inherits(UrlClassifierTable);
76 * Look up a URL in a URL table
78 UrlClassifierTableUrl.prototype.exists = function(url, callback) {
79 // nsIUrlClassifierUtils.canonicalizeURL is the old way of canonicalizing a
80 // URL. Unfortunately, it doesn't normalize numeric domains so alternate IP
81 // formats (hex, octal, etc) won't trigger a match.
82 // this.enchashDecrypter_.getCanonicalUrl does the right thing and
83 // normalizes a URL to 4 decimal numbers, but the update server may still be
84 // giving us encoded IP addresses. So to be safe, we check both cases.
85 var urlUtils = Cc["@mozilla.org/url-classifier/utils;1"]
86 .getService(Ci.nsIUrlClassifierUtils);
87 var oldCanonicalized = urlUtils.canonicalizeURL(url);
88 var canonicalized = this.enchashDecrypter_.getCanonicalUrl(url);
89 G_Debug(this, "Looking up: " + url + " (" + oldCanonicalized + " and " +
91 (new ExistsMultiQuerier([oldCanonicalized, canonicalized],
96 /////////////////////////////////////////////////////////////////////
97 // Domain table implementation
99 function UrlClassifierTableDomain() {
100 UrlClassifierTable.call(this);
101 this.debugZone = "urlclassifier-table-domain";
102 this.ioService_ = Cc["@mozilla.org/network/io-service;1"]
103 .getService(Ci.nsIIOService);
105 UrlClassifierTableDomain.inherits(UrlClassifierTable);
108 * Look up a URL in a domain table
109 * We also try to lookup domain + first path component (e.g.,
110 * www.mozilla.org/products).
112 * @returns Boolean true if the url domain is in the table
114 UrlClassifierTableDomain.prototype.exists = function(url, callback) {
115 var canonicalized = this.enchashDecrypter_.getCanonicalUrl(url);
116 var urlObj = this.ioService_.newURI(canonicalized, null, null);
121 var hostComponents = host.split(".");
123 // Try to get the path of the URL. Pseudo urls (like wyciwyg:) throw
124 // errors when trying to convert to an nsIURL so we wrap in a try/catch
128 urlObj.QueryInterface(Ci.nsIURL);
129 path = urlObj.filePath;
132 var pathComponents = path.split("/");
134 // We don't have a good way map from hosts to domains, so we instead try
135 // each possibility. Could probably optimize to start at the second dot?
137 for (var i = 0; i < hostComponents.length - 1; i++) {
138 host = hostComponents.slice(i).join(".");
141 // The path starts with a "/", so we are interested in the second path
142 // component if it is available
143 if (pathComponents.length >= 2 && pathComponents[1].length > 0) {
144 host = host + "/" + pathComponents[1];
149 // Run the possible domains against the db.
150 (new ExistsMultiQuerier(possible, this.name, callback)).run();
153 /////////////////////////////////////////////////////////////////////
154 // Enchash table implementation
156 function UrlClassifierTableEnchash() {
157 UrlClassifierTable.call(this);
158 this.debugZone = "urlclassifier-table-enchash";
160 UrlClassifierTableEnchash.inherits(UrlClassifierTable);
163 * Look up a URL in an enchashDB. We try all sub domains (up to MAX_DOTS).
165 UrlClassifierTableEnchash.prototype.exists = function(url, callback) {
166 url = this.enchashDecrypter_.getCanonicalUrl(url);
167 var host = this.enchashDecrypter_.getCanonicalHost(url,
168 PROT_EnchashDecrypter.MAX_DOTS);
171 for (var i = 0; i < PROT_EnchashDecrypter.MAX_DOTS + 1; i++) {
174 var index = host.indexOf(".");
177 host = host.substring(index + 1);
179 // Run the possible domains against the db.
180 (new EnchashMultiQuerier(possible, this.name, callback, url)).run();