Skip to content

Commit

Permalink
refactoring, needs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthropohedron committed Apr 7, 2015
1 parent 6ff8d71 commit 5dab4b7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 38 deletions.
56 changes: 56 additions & 0 deletions lib/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*jslint white: true, todo: true */
"use strict";

var util = require("./util");
var Ci = require("chrome").Ci;

var topics = [
"http-on-modify-request",
"http-on-examine-response",
"http-on-examine-cached-response",
"http-on-examine-merged-response"
];

function Controller() {
var me = this;
this.observerService = util.getService("@mozilla.org/observer-service;1",
"nsIObserverService");
topics.forEach(function(topic) {
me.observerService.addObserver(me, topic, false);
});
this[" is valid "] = true;
}

Controller.prototype = {
observe: function(subject, topic) {
var httpChannel;
switch (topic) {
case "http-on-modify-request":
httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
//TODO
//httpChannel.setRequestHeader("X-Hello", "World", false);
break;
case "http-on-examine-response":
case "http-on-examine-cached-response":
case "http-on-examine-merged-response":
httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
//TODO
break;
}
return httpChannel;
},

isValid: function isValid() {
return !!this[" is valid "];
},

invalidate: function() {
if (!this[" is valid "]) { return; }
this.observerService.removeObserver(this, "http-on-modify-request");
delete this[" is valid "];
}

};

exports.Controller = Controller;

44 changes: 6 additions & 38 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,8 @@
/*jslint white: true, todo: true */
"use strict";

// Import the page-mod API
var pageMod = require("sdk/page-mod");
// Import the self API
var self = require("sdk/self");
// Import Mozilla chrome APIs
var chrome = require("chrome");
var Cc = chrome.Cc;
var Ci = chrome.Ci;

var prerequestObserver = {
observe: function(subject, topic) {
if (topic === "http-on-modify-request") {
var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
//TODO
//httpChannel.setRequestHeader("X-Hello", "World", false);
return httpChannel;
}
},

observerService: (function() {
var services = Cc["@mozilla.org/observer-service;1"];
return services.getService(Ci.nsIObserverService);
}()),

register: function() {
if (this[" is registered "]) { return; }
this.observerService.addObserver(this, "http-on-modify-request", false);
this[" is registered "] = true;
},

unregister: function() {
if (!this[" is registered "]) { return; }
this.observerService.removeObserver(this, "http-on-modify-request");
delete this[" is registered "];
}
};
var Controller = require("./controller").Controller;
var session;

function addonStartup(options /*, callbacks */) {
switch (options.loadReason) {
Expand All @@ -50,7 +16,8 @@ function addonStartup(options /*, callbacks */) {
default:
return;
}
prerequestObserver.register();
if (session && session.isValid()) { return; }
session = new Controller();
}

function addonShutdown(reason) {
Expand All @@ -65,7 +32,8 @@ function addonShutdown(reason) {
default:
return;
}
prerequestObserver.unregister();
session.invalidate();
session = null;
}

exports.main = addonStartup;
Expand Down
16 changes: 16 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*jslint white: true, todo: true */
"use strict";

// Import Mozilla chrome APIs
var chrome = require("chrome");
var Cc = chrome.Cc;
var Ci = chrome.Ci;

exports.createInstance = function createInstance(contractId, iface) {
return Cc[contractId].createInstance(Ci[iface]);
};

exports.getService = function getService(contractId, service) {
return Cc[contractId].getService(Ci[service]);
};

0 comments on commit 5dab4b7

Please sign in to comment.