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 Google Safe Browsing.
16 # The Initial Developer of the Original Code is Google Inc.
17 # Portions created by the Initial Developer are Copyright (C) 2006
18 # the Initial Developer. All Rights Reserved.
21 # Tony Chang <tony@google.com> (original author)
23 # Alternatively, the contents of this file may be used under the terms of
24 # either the GNU General Public License Version 2 or later (the "GPL"), or
25 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 # in which case the provisions of the GPL or the LGPL are applicable instead
27 # of those above. If you wish to allow use of your version of this file only
28 # under the terms of either the GPL or the LGPL, and not to allow others to
29 # use your version of this file under the terms of the MPL, indicate your
30 # decision by deleting the provisions above and replace them with the notice
31 # and other provisions required by the GPL or the LGPL. If you do not delete
32 # the provisions above, a recipient may use your version of this file under
33 # the terms of any one of the MPL, the GPL or the LGPL.
35 # ***** END LICENSE BLOCK *****
38 * This class helps us batch a series of async calls to the db.
39 * If any of the tokens is in the database, we fire callback with
40 * true as a param. If all the tokens are not in the database,
41 * we fire callback with false as a param.
42 * This is an "Abstract" base class. Subclasses need to supply
43 * the condition_ method.
45 * @param tokens Array of strings to lookup in the db
46 * @param tableName String name of the table
47 * @param callback Function callback function that takes true if the condition
50 function MultiQuerier(tokens, tableName, callback) {
51 this.tokens_ = tokens;
52 this.tableName_ = tableName;
53 this.callback_ = callback;
54 this.dbservice_ = Cc["@mozilla.org/url-classifier/dbservice;1"]
55 .getService(Ci.nsIUrlClassifierDBService);
56 // We put the current token in this variable.
61 * Run the remaining tokens against the db.
63 MultiQuerier.prototype.run = function() {
64 if (this.tokens_.length == 0) {
65 this.callback_.handleEvent(false);
66 this.dbservice_ = null;
67 this.callback_ = null;
71 this.key_ = this.tokens_.pop();
72 G_Debug(this, "Looking up " + this.key_ + " in " + this.tableName_);
73 this.dbservice_.exists(this.tableName_, this.key_,
74 BindToObject(this.result_, this));
78 * Callback from the db. If the returned value passes the this.condition_
79 * test, go ahead and call the main callback.
81 MultiQuerier.prototype.result_ = function(value) {
82 if (this.condition_(value)) {
83 this.callback_.handleEvent(true)
84 this.dbservice_ = null;
85 this.callback_ = null;
91 // Subclasses must override this.
92 MultiQuerier.prototype.condition_ = function(value) {
93 throw "MultiQuerier is an abstract base class";
98 * Concrete MultiQuerier that stops if the key exists in the db.
100 function ExistsMultiQuerier(tokens, tableName, callback) {
101 MultiQuerier.call(this, tokens, tableName, callback);
102 this.debugZone = "existsMultiQuerier";
104 ExistsMultiQuerier.inherits(MultiQuerier);
106 ExistsMultiQuerier.prototype.condition_ = function(value) {
107 return value.length > 0;
112 * Concrete MultiQuerier that looks up a key, decrypts it, then
113 * checks the the resulting regular expressions for a match.
114 * @param tokens Array of hosts
116 function EnchashMultiQuerier(tokens, tableName, callback, url) {
117 MultiQuerier.call(this, tokens, tableName, callback);
119 this.enchashDecrypter_ = new PROT_EnchashDecrypter();
120 this.debugZone = "enchashMultiQuerier";
122 EnchashMultiQuerier.inherits(MultiQuerier);
124 EnchashMultiQuerier.prototype.run = function() {
125 if (this.tokens_.length == 0) {
126 this.callback_.handleEvent(false);
127 this.dbservice_ = null;
128 this.callback_ = null;
131 var host = this.tokens_.pop();
133 var lookupKey = this.enchashDecrypter_.getLookupKey(host);
134 this.dbservice_.exists(this.tableName_, lookupKey,
135 BindToObject(this.result_, this));
138 EnchashMultiQuerier.prototype.condition_ = function(encryptedValue) {
139 if (encryptedValue.length > 0) {
140 // We have encrypted regular expressions for this host. Let's
141 // decrypt them and see if we have a match.
142 var decrypted = this.enchashDecrypter_.decryptData(encryptedValue,
144 var res = this.enchashDecrypter_.parseRegExps(decrypted);
145 for (var j = 0; j < res.length; j++) {
146 if (res[j].test(this.url_)) {