0

Introduction

I'm trying to intercept certain HTTP requests and then notify the user and cancel these requests:

UPDATE: reproducible code

const {Cc, Ci, Cr} = require("chrome");
const self = require("sdk/self");
const data = self.data;
const notif = require("notifications");

const REGEX = {
    'lookup.bluecava.com': "1",
    'ds.bluecava.com/v50/AC/BCAC': "2"
};
// ------------------------------------------------------------------

// Initialize observer service
var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);

function TracingListener() {
    this.originalListener = null;
}


// Request observer
var httpRequestObserver = {
    observe: function (aSubject, aTopic, aData) {
        var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
        if ("http-on-modify-request" == aTopic) {
            var url = httpChannel.originalURI.spec;            
            for (var str_re in REGEX) {
                var re = new RegExp(str_re, "g");
                if (url.match(re)) {
                    console.log("Requested URL: " + url.match(re));            
                    notif.notify({
                        title: "foo",
                        text: "foo"
                    });
                    aSubject.cancel(Cr.NS_BINDING_ABORTED); // Cancel request TODO: fix if uncommented notification won't pop up!
                }
            }            
        }
    },
    QueryInterface: function (aIID) {
        if (aIID.equals(Ci.nsIObserver) || aIID.equals(Ci.nsISupports)) {
            return this;
        }

        throw Cr.NS_NOINTERFACE;
    }
};

// Register service
observerService.addObserver(httpRequestObserver,
    "http-on-modify-request", false);

exports.main = function () {
    console.log("Addon is running...");
};

// Unload addon event
exports.onUnload = function (reason) {
    // Unregister service
    observerService.removeObserver(httpRequestObserver,
        "http-on-modify-request");
};

Problem

The problem is that, no matter the order of the call to the cancel method and the notify method, the notification won't pop up.

Possible cause

I guess that it has something to do with the asynchronous nature of javascript and that the method cancel blocks somehow the notification call. However, I don't find any callbacks neither in the notification nor in the cancel method that I can use to chain the calls. Any idea?

synack
  • 1,699
  • 3
  • 24
  • 50
  • Questions concerning problems with code you've written must describe the specific problem — **and include valid code to reproduce it** — in the question itself. See http://SSCCE.org for guidance. Right now, there could be a lots of reasons I can think of, most of which in the part of the code you didn't post. – nmaier Nov 06 '13 at 19:11
  • @nmaier: after some research, I more or less now the reason. I tried to explain it in the "possible cause" section. I'll try to explain it a bit better in this comment: the problem is that the call to `cancel` method closes the channel and thus prevents the notification to show up. The solution should be to use a callback to show the notification after this event (sth like what they propose in here http://stackoverflow.com/questions/8464151/redirect-http-request-uri-in-mozilla-addon-builder?rq=1 related to `nsIChannel.notificationCallbacks`). However, I can't find the way to do it. – synack Nov 06 '13 at 23:38
  • You're confusing `notificationCallbacks` with the SDK `notifications` user-interface module. These two have nothing to do with each other. `sdl/notifications` works completely independent from anything an `nsIChannel` may do. Still, if you want help, provide a full, reproducible example. – nmaier Nov 07 '13 at 02:10
  • @nmaier: no, I'm not confusing them. ok, I'll try to give a reproducible example. – synack Nov 07 '13 at 08:19
  • @nmaier: now it should be working. – synack Nov 07 '13 at 08:25
  • Works for me (via https://builder.addons.mozilla.org/package/210511/latest/ , which I cleaned up a bit, but didn't really change). The load is canceled and the notification shows up. – nmaier Nov 07 '13 at 16:57
  • @nmaier: your example also works for me but I still can't make my code work. There is something that I miss. I posted a more accurate version of the code. – synack Nov 07 '13 at 23:08
  • That's weird... mmh... Could it be my FF? – synack Nov 08 '13 at 08:46
  • 1
    It is entirely possible that something in your Firefox profile (another add-on most likely), either interacts badly with your code or prevents it from running at all... – nmaier Nov 08 '13 at 21:47

0 Answers0