toolkit/components/url-classifier/content/multi-querier.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 /**
michael@0 6 * This class helps us batch a series of async calls to the db.
michael@0 7 * If any of the tokens is in the database, we fire callback with
michael@0 8 * true as a param. If all the tokens are not in the database,
michael@0 9 * we fire callback with false as a param.
michael@0 10 * This is an "Abstract" base class. Subclasses need to supply
michael@0 11 * the condition_ method.
michael@0 12 *
michael@0 13 * @param tokens Array of strings to lookup in the db
michael@0 14 * @param tableName String name of the table
michael@0 15 * @param callback Function callback function that takes true if the condition
michael@0 16 * passes.
michael@0 17 */
michael@0 18 function MultiQuerier(tokens, tableName, callback) {
michael@0 19 this.tokens_ = tokens;
michael@0 20 this.tableName_ = tableName;
michael@0 21 this.callback_ = callback;
michael@0 22 this.dbservice_ = Cc["@mozilla.org/url-classifier/dbservice;1"]
michael@0 23 .getService(Ci.nsIUrlClassifierDBService);
michael@0 24 // We put the current token in this variable.
michael@0 25 this.key_ = null;
michael@0 26 }
michael@0 27
michael@0 28 /**
michael@0 29 * Run the remaining tokens against the db.
michael@0 30 */
michael@0 31 MultiQuerier.prototype.run = function() {
michael@0 32 if (this.tokens_.length == 0) {
michael@0 33 this.callback_.handleEvent(false);
michael@0 34 this.dbservice_ = null;
michael@0 35 this.callback_ = null;
michael@0 36 return;
michael@0 37 }
michael@0 38
michael@0 39 this.key_ = this.tokens_.pop();
michael@0 40 G_Debug(this, "Looking up " + this.key_ + " in " + this.tableName_);
michael@0 41 this.dbservice_.exists(this.tableName_, this.key_,
michael@0 42 BindToObject(this.result_, this));
michael@0 43 }
michael@0 44
michael@0 45 /**
michael@0 46 * Callback from the db. If the returned value passes the this.condition_
michael@0 47 * test, go ahead and call the main callback.
michael@0 48 */
michael@0 49 MultiQuerier.prototype.result_ = function(value) {
michael@0 50 if (this.condition_(value)) {
michael@0 51 this.callback_.handleEvent(true)
michael@0 52 this.dbservice_ = null;
michael@0 53 this.callback_ = null;
michael@0 54 } else {
michael@0 55 this.run();
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 // Subclasses must override this.
michael@0 60 MultiQuerier.prototype.condition_ = function(value) {
michael@0 61 throw "MultiQuerier is an abstract base class";
michael@0 62 }
michael@0 63
michael@0 64
michael@0 65 /**
michael@0 66 * Concrete MultiQuerier that stops if the key exists in the db.
michael@0 67 */
michael@0 68 function ExistsMultiQuerier(tokens, tableName, callback) {
michael@0 69 MultiQuerier.call(this, tokens, tableName, callback);
michael@0 70 this.debugZone = "existsMultiQuerier";
michael@0 71 }
michael@0 72 ExistsMultiQuerier.inherits(MultiQuerier);
michael@0 73
michael@0 74 ExistsMultiQuerier.prototype.condition_ = function(value) {
michael@0 75 return value.length > 0;
michael@0 76 }
michael@0 77
michael@0 78
michael@0 79 /**
michael@0 80 * Concrete MultiQuerier that looks up a key, decrypts it, then
michael@0 81 * checks the the resulting regular expressions for a match.
michael@0 82 * @param tokens Array of hosts
michael@0 83 */
michael@0 84 function EnchashMultiQuerier(tokens, tableName, callback, url) {
michael@0 85 MultiQuerier.call(this, tokens, tableName, callback);
michael@0 86 this.url_ = url;
michael@0 87 this.enchashDecrypter_ = new PROT_EnchashDecrypter();
michael@0 88 this.debugZone = "enchashMultiQuerier";
michael@0 89 }
michael@0 90 EnchashMultiQuerier.inherits(MultiQuerier);
michael@0 91
michael@0 92 EnchashMultiQuerier.prototype.run = function() {
michael@0 93 if (this.tokens_.length == 0) {
michael@0 94 this.callback_.handleEvent(false);
michael@0 95 this.dbservice_ = null;
michael@0 96 this.callback_ = null;
michael@0 97 return;
michael@0 98 }
michael@0 99 var host = this.tokens_.pop();
michael@0 100 this.key_ = host;
michael@0 101 var lookupKey = this.enchashDecrypter_.getLookupKey(host);
michael@0 102 this.dbservice_.exists(this.tableName_, lookupKey,
michael@0 103 BindToObject(this.result_, this));
michael@0 104 }
michael@0 105
michael@0 106 EnchashMultiQuerier.prototype.condition_ = function(encryptedValue) {
michael@0 107 if (encryptedValue.length > 0) {
michael@0 108 // We have encrypted regular expressions for this host. Let's
michael@0 109 // decrypt them and see if we have a match.
michael@0 110 var decrypted = this.enchashDecrypter_.decryptData(encryptedValue,
michael@0 111 this.key_);
michael@0 112 var res = this.enchashDecrypter_.parseRegExps(decrypted);
michael@0 113 for (var j = 0; j < res.length; j++) {
michael@0 114 if (res[j].test(this.url_)) {
michael@0 115 return true;
michael@0 116 }
michael@0 117 }
michael@0 118 }
michael@0 119 return false;
michael@0 120 }

mercurial