Currently there don't seem to be any utility functions to ease the
creation of Javascript components.  I'm thinking of taking a stab at
changing that.  I have high hopes of making Javascript component
creation almost trivially easy.  Here's what a sample Content Policy
component might look like in its entirety:

---------------------------- IDL File --------------------------
#include "nsISupports.idl"

interface nsIURI;
interface nsIDOMWindow;

[scriptable, uuid(188ebeab-e4cf-4388-acb8-e5ade741d7bc)]
interface nsIJavascriptContentPolicy : nsISupports
{
  boolean shouldLoad(in PRInt32 contentType, 
                     in nsIURI contentLocation,
                     in nsISupports ctxt, 
                     in nsIDOMWindow window);
};


----------------------- Javascript File -----------------------

// all constants for local convenience only
const NAME = "Javascript Content Policy"
const CONTRACT = "@mozilla.org/javascriptcontentpolicy;1";
const CID = Components.ID("{acf1ae9e-a257-4b63-9d97-edaec95e0b80}");
const INTERFACE = Components.interfaces.nsIJavascriptContentPolicy;
const IS_SINGLETON = true;
const nsIContentPolicy = Components.interfaces.nsIContentPolicy;

var component = 
  new Utils.genericComponent(NAME, CONTRACT, CID, IS_SINGLETON, 
                             INTERFACE, nsIContentPolicy);

component.register = 
  new Utils.genericRegisterCategoryFunc("content-policy", 
                                        CONTRACT, NAME);

component.shouldLoad = function(type, location, context, window) {
  var match = /yahoo/.test(location.path);
  return match;
}

var module = new Utils.genericModule(component);

function NSGetModule(compMgr, fileSpec) { return module; }

------------------------------------------------------------

Thoughts on interface and internal object organization appreciated. 
Things are still in the mock up stage, but I should have something
workable mid-week. Respond here if there is interest, otherwise
details to follow only in
http://bugzilla.mozilla.org/show_bug.cgi?id=139594

Reply via email to