Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:16:55+00:00 2026-05-27T23:16:55+00:00

In short Why, based on the code detailed below, is the shouldLoad function not

  • 0

In short

Why, based on the code detailed below, is the shouldLoad function not called?

Longer version

My goal

I am trying to construct a Firefox/Iceweasel extension that will cancel/redirect certain url requests.

Background

Based on what I have seen on the web, one way (if I want to intercept every request and not just the top document) to do this is to make an XPCOM component that implements the nsIContentPolicy interface, and register that component in the extension, and have the shouldLoad function examine the requested url and deny when appropriate.

Problem

I have implemented a component to the best of my effort, and integrated it with my extension. The component seems to work in the sense that it gets registered in compreg.dat, etc, BUT – the shouldLoad function does not get called, as far as I can tell.

Details

Environment

I am developing on Debian Linux using an IceWeasel version corresponding to FireFox 3.5.16.

Extension

My extension is based off of an example extension given at
http://kb.mozillazine.org/Getting_started_with_extension_development#reg-em
In essence, all it does is add a menu item that opens an alert-like dialog saying hello world. it has an onLoad registration that fires and alert saying “onLoad Reporting!”. The alert fires without problems every time.

Reference

I have installed the extension Redirector, seemingly operating on the same principles,
https://addons.mozilla.org/en-US/firefox/addon/redirector/
and it works (so it is likely to be a mistake in my code rather than the environment being bad).

Component

My component implementation is based on various sources I have found on the internet.
I have placed it in a file in directory {pathtoextension}/helloworld/components/PolicyComponent.js, and its code is as follows:

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");  
const CI = Components.interfaces, CC = Components.classes, CR = Components.results;  

var componentobj = null;

function PolicyComponent()
{
    // this.wrappedJSObject = this;
}  

PolicyComponent.prototype = {  
  classDescription: "My QWERTY nsIContentPolicy XPCOM Component",  
  classID:          Components.ID("{6ffd2f60-3784-11e1-b86c-0800200c9a66}"),  
  contractID:       "@abc.def.com/policycomp;1",  
  QueryInterface: XPCOMUtils.generateQI([CI.nsIContentPolicy]),  


  testFunction: function() { return "Your component is not entirely broken!"; },  

  _xpcom_categories: [{
        category: "content-policy"
    }],

    _xpcom_factory  :
    {
        createInstance: function(outer, iid)
        {
            if (outer)
            { throw CR.NS_ERROR_NO_AGGREGATION;}
            if (componentobj == null)
            {
                componentobj = new PolicyComponent();   
            }
            else {}
            return componentobj.QueryInterface(iid);
        }
    },



    shouldLoad: function(contentType, contentLocation, requestOrigin, aContext, mimeTypeGuess, extra)
    {
        if (contentType != Ci.nsIContentPolicy.TYPE_DOCUMENT) {
                return Ci.nsIContentPolicy.ACCEPT;
            }

        if(-1 != contentLocation.spec.search("abc"))
            {
                aContext.loadURI("http://www.stroustrup.com/", requestOrigin, null);
                return Ci.nsIContentPolicy.REJECT_REQUEST;
            }
        return CI.nsIContentPolicy.ACCEPT;
    },

    shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode, mimeType, extra) {
        return CI.nsIContentPolicy.ACCEPT;
    }    

};  
var components = [PolicyComponent];  

if (XPCOMUtils.generateNSGetFactory)  
    var NSGetFactory = XPCOMUtils.generateNSGetFactory([PolicyComponent]);  
else  
    var NSGetModule = XPCOMUtils.generateNSGetModule([PolicyComponent]);

Status

The component seems to be recognised by IceWeasel. If I remove compreg.dat and xpti.dat and restart IceWeasel, a grep on content-policy in compreg.dat gives the following result:

...
@mozilla.org/embedding/browser/content-policy;1,{f66bc334-1dd1-11b2-bab2-90e04fe15c19}
content-policy,@mozilla.org/data-document-content-policy;1,@mozilla.org/data-document-content-policy;1
content-policy,My QWERTY nsIContentPolicy XPCOM Component,@abc.def.com/policycomp;1
content-policy,@mozilla.org/no-data-protocol-content-policy;1,@mozilla.org/no-data-protocol-content-policy;1
...

So it seems as if there is at least something correct with the component.
HOWEVER, I can still access web pages with “abc” in the url (which makes believe that the shouldLoad function is not called).

Further info

I have not added anything about the extension to the chrome.manifest file. It is my belief that I need not do that in version 3.5.x of FF/IW.

Questions

  1. What’s wrong? 🙂

  2. Would I need to add something to chrome.manifest? Or is that only for FF 4+?

  3. Do I need to somehow instantiate the component/service further? Like in, say, the overlay.js in the onLoad hook?

  4. Do I need to register the component as valid for the extension in a more explicit way, and if so, how?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:16:56+00:00Added an answer on May 27, 2026 at 11:16 pm

    Looks like you didn’t verify that your shouldLoad method really isn’t being called. I would suggest using dump() function to see what’s really happening in your component. It seems more likely that it is being called but it throws an exception like “aContext.loadURI is not a function”. Reason is that aContext for TYPE_DOCUMENT calls is an HTMLDocument object and it has no loadURI method. You probably want to call aContext.defaultView.location.replace() instead. But doing that from a content policy would be a security vulnerability (in fact, doing anything from a content policy that could cause the web page scripts to run would be a security vulnerability). If you look at the interface definition you will see that it comes with big warnings attached.

    So a manipulation like this one needs to happen delayed, to make sure that it happens when the engine is in a consistent state. E.g. you could do:

    aContext.defaultView.setTimeout("window.location.replace('http://www.stroustrup.com/')", 0);
    

    What’s wrong? 🙂

    Other than what I mentioned above, you probably shouldn’t define your custom _xpcom_factory function. Content policies are always used as a service meaning that they are automatically singletons. Your own code accessing the component should use getService() as well of course.

    Would I need to add something to chrome.manifest? Or is that only for FF 4+?

    Yes, for FF4+. Something like:

    component {6ffd2f60-3784-11e1-b86c-0800200c9a66} components/PolicyComponent.js
    contract @abc.def.com/policycomp;1 {6ffd2f60-3784-11e1-b86c-0800200c9a66}
    category content-policy @abc.def.com/policycomp;1 @abc.def.com/policycomp;1
    

    Do I need to somehow instantiate the component/service further? Like in, say, the overlay.js in the onLoad hook?

    No, that happens automatically by the built-in content policy component.

    Do I need to register the component as valid for the extension in a more explicit way, and if so, how?

    Don’t know what you mean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

short version: Is there a good time based sampling profiler for Linux? long version:
UPDATED: I've updated the code based on the correct answer below. This works but
This code is supposed to be able to sort the items in self.array based
Following problem: I want to render a news stream of short messages based on
I'm creating interfaces and abstract classes that represent a messaging framework for short text-based
Short: how does modelbinding pass objects from view to controller? Long: First, based on
I am planning an application which will make clusters of short messages/tweets based on
In short I'm creating a Flash based multiplayer game and I'm now starting to
I've been given a bunch of messy code and a short time limit (no
In my application I am using code like this: DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.