Permalink
Browse files

fix a loading problem

  • Loading branch information...
anon21 committed Nov 27, 2011
1 parent 8ff0c75 commit c53130e0d66ab2d15f8ebcb8b8ba8493c423c977
Showing with 47 additions and 22 deletions.
  1. +7 −0 bootstrap.js
  2. +1 −1 install.rdf
  3. +37 −21 modules/request-filter.js
  4. +2 −0 options.xul
View
@@ -13,6 +13,9 @@ var _prefsObserver = {
case "extensions.requestFilter.targets":
RequestFilter.reloadTargets();
break;
+ case "extensions.requestFilter.reportMode":
+ RequestFilter.reloadReportMode();
+ break;
}
}
},
@@ -56,10 +59,14 @@ function install(data, reason) {
if( !branch.prefHasUserValue("targets") )
branch.setCharPref("targets", "");
+
+ if( !branch.prefHasUserValue("reportMode") )
+ branch.setBoolPref("reportMode", false)
}
function uninstall(data, reason) {
// remove preferences
// var branch = Services.prefs.getBranch("extensions.requestFilter.");
// branch.clearUserPref("targets");
+ // branch.clearUserPref("reportMode");
}
View
@@ -5,7 +5,7 @@
<Description about="urn:mozilla:install-manifest">
<em:id>request-filter@anon21.mozilla.org</em:id>
- <em:version>0.1.0</em:version>
+ <em:version>0.1.1</em:version>
<em:type>2</em:type>
<em:name>request-filter</em:name>
<em:description>A HTTP-request filtering extension.</em:description>
View
@@ -1,4 +1,4 @@
-var EXPORTED_SYMBOLS = ["attachFilter", "detachFilter", "reloadTargets"];
+var EXPORTED_SYMBOLS = ["attachFilter", "detachFilter", "reloadTargets", "reloadReportMode"];
const Cc = Components.classes;
const Ci = Components.interfaces;
@@ -9,20 +9,20 @@ Cu.import("resource://gre/modules/Services.jsm");
var _targets;
var _targetsCache;
+var _reportMode;
function _testHostname(hostname) {
if( hostname in _targetsCache )
return _targetsCache[hostname];
- for(var i = 0; i < _targets.length; ++i) {
- var j = hostname.lastIndexOf(".", hostname.length - 1)
+ var i = hostname.lastIndexOf(".", hostname.length - 1);
+
+ while( i != -1 ) {
+ i = hostname.lastIndexOf(".", i - 1);
+ var subpart = hostname.slice(i + 1);
- while( true ) {
- j = hostname.lastIndexOf(".", j - 1);
- if( j == -1 )
- break;
-
- if( hostname.slice(j + 1) == _targets[i] ) {
+ for(var j = 0; j < _targets.length; ++j) {
+ if( subpart == _targets[j] ) {
_targetsCache[hostname] = true;
return true;
}
@@ -34,15 +34,21 @@ function _testHostname(hostname) {
}
function _filterHttpRequest(httpChannel) {
- if( _testHostname(httpChannel.originalURI.host) ) {
- httpChannel.cancel(Cr.NS_BINDING_ABORTED);
+ var b = _testHostname(httpChannel.originalURI.host);
+
+ if( _reportMode ) {
+ Services.console.logStringMessage((b ? "[Blocked] " : "[Request] ")
+ + httpChannel.originalURI.host);
+ }
+
+ if( b ) {
+ httpChannel.cancel(Cr.NS_ERROR_FAILURE);
}
}
function _loadTargets() {
try {
- var branch = Services.prefs.getBranch("extensions.requestFilter.");
- var targetsString = branch.getCharPref("targets");
+ var targetsString = Services.prefs.getCharPref("extensions.requestFilter.targets");
_targets = targetsString.split(";");
} catch(e) {
_targets = [];
@@ -51,41 +57,51 @@ function _loadTargets() {
_targetsCache = {};
}
-function unloadTargets() {
+function _unloadTargets() {
_targets = undefined;
_targetsCache = undefined;
}
+function _loadReportMode() {
+ _reportMode = Services.prefs.getBoolPref("extensions.requestFilter.reportMode");
+}
+
+function _unloadReportMode() {
+ _reportMode = undefined;
+}
+
var _httpRequestObserver = {
- observe: function (subject, topic, data) {
+ observe: function(subject, topic, data) {
if( topic == "http-on-modify-request" ) {
_filterHttpRequest(subject.QueryInterface(Ci.nsIHttpChannel));
}
},
- get observerService() {
- return Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
- },
-
register: function() {
- this.observerService.addObserver(this, "http-on-modify-request", false);
+ Services.obs.addObserver(this, "http-on-modify-request", false);
},
unregister: function() {
- this.observerService.removeObserver(this, "http-on-modify-request");
+ Services.obs.removeObserver(this, "http-on-modify-request");
},
};
function attachFilter() {
_loadTargets();
+ _loadReportMode();
_httpRequestObserver.register();
}
function detachFilter() {
_httpRequestObserver.unregister();
_unloadTargets();
+ _unloadReportMode();
}
function reloadTargets() {
_loadTargets();
}
+
+function reloadReportMode() {
+ _loadReportMode();
+}
View
@@ -3,4 +3,6 @@
<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<setting type="string" pref="extensions.requestFilter.targets"
title="Targets" desc="target hostnames (separated by a semicolon)" />
+ <setting type="bool" pref="extensions.requestFilter.reportMode"
+ title="Report Mode" desc="whether report filtering results on Error console" />
</vbox>

0 comments on commit c53130e

Please sign in to comment.