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: /** michael@0: * This class helps us batch a series of async calls to the db. michael@0: * If any of the tokens is in the database, we fire callback with michael@0: * true as a param. If all the tokens are not in the database, michael@0: * we fire callback with false as a param. michael@0: * This is an "Abstract" base class. Subclasses need to supply michael@0: * the condition_ method. michael@0: * michael@0: * @param tokens Array of strings to lookup in the db michael@0: * @param tableName String name of the table michael@0: * @param callback Function callback function that takes true if the condition michael@0: * passes. michael@0: */ michael@0: function MultiQuerier(tokens, tableName, callback) { michael@0: this.tokens_ = tokens; michael@0: this.tableName_ = tableName; michael@0: this.callback_ = callback; michael@0: this.dbservice_ = Cc["@mozilla.org/url-classifier/dbservice;1"] michael@0: .getService(Ci.nsIUrlClassifierDBService); michael@0: // We put the current token in this variable. michael@0: this.key_ = null; michael@0: } michael@0: michael@0: /** michael@0: * Run the remaining tokens against the db. michael@0: */ michael@0: MultiQuerier.prototype.run = function() { michael@0: if (this.tokens_.length == 0) { michael@0: this.callback_.handleEvent(false); michael@0: this.dbservice_ = null; michael@0: this.callback_ = null; michael@0: return; michael@0: } michael@0: michael@0: this.key_ = this.tokens_.pop(); michael@0: G_Debug(this, "Looking up " + this.key_ + " in " + this.tableName_); michael@0: this.dbservice_.exists(this.tableName_, this.key_, michael@0: BindToObject(this.result_, this)); michael@0: } michael@0: michael@0: /** michael@0: * Callback from the db. If the returned value passes the this.condition_ michael@0: * test, go ahead and call the main callback. michael@0: */ michael@0: MultiQuerier.prototype.result_ = function(value) { michael@0: if (this.condition_(value)) { michael@0: this.callback_.handleEvent(true) michael@0: this.dbservice_ = null; michael@0: this.callback_ = null; michael@0: } else { michael@0: this.run(); michael@0: } michael@0: } michael@0: michael@0: // Subclasses must override this. michael@0: MultiQuerier.prototype.condition_ = function(value) { michael@0: throw "MultiQuerier is an abstract base class"; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Concrete MultiQuerier that stops if the key exists in the db. michael@0: */ michael@0: function ExistsMultiQuerier(tokens, tableName, callback) { michael@0: MultiQuerier.call(this, tokens, tableName, callback); michael@0: this.debugZone = "existsMultiQuerier"; michael@0: } michael@0: ExistsMultiQuerier.inherits(MultiQuerier); michael@0: michael@0: ExistsMultiQuerier.prototype.condition_ = function(value) { michael@0: return value.length > 0; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Concrete MultiQuerier that looks up a key, decrypts it, then michael@0: * checks the the resulting regular expressions for a match. michael@0: * @param tokens Array of hosts michael@0: */ michael@0: function EnchashMultiQuerier(tokens, tableName, callback, url) { michael@0: MultiQuerier.call(this, tokens, tableName, callback); michael@0: this.url_ = url; michael@0: this.enchashDecrypter_ = new PROT_EnchashDecrypter(); michael@0: this.debugZone = "enchashMultiQuerier"; michael@0: } michael@0: EnchashMultiQuerier.inherits(MultiQuerier); michael@0: michael@0: EnchashMultiQuerier.prototype.run = function() { michael@0: if (this.tokens_.length == 0) { michael@0: this.callback_.handleEvent(false); michael@0: this.dbservice_ = null; michael@0: this.callback_ = null; michael@0: return; michael@0: } michael@0: var host = this.tokens_.pop(); michael@0: this.key_ = host; michael@0: var lookupKey = this.enchashDecrypter_.getLookupKey(host); michael@0: this.dbservice_.exists(this.tableName_, lookupKey, michael@0: BindToObject(this.result_, this)); michael@0: } michael@0: michael@0: EnchashMultiQuerier.prototype.condition_ = function(encryptedValue) { michael@0: if (encryptedValue.length > 0) { michael@0: // We have encrypted regular expressions for this host. Let's michael@0: // decrypt them and see if we have a match. michael@0: var decrypted = this.enchashDecrypter_.decryptData(encryptedValue, michael@0: this.key_); michael@0: var res = this.enchashDecrypter_.parseRegExps(decrypted); michael@0: for (var j = 0; j < res.length; j++) { michael@0: if (res[j].test(this.url_)) { michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: }