How to implement XPCOM component (nsIContentPolicy) in bootstrapped Firefox extension

自古美人都是妖i 提交于 2019-12-04 07:38:56

Adblock Plus, which is restartless but not using the SDK, registers an nsIContentPolicy implementation at runtime, just like your SDK would. There are probably a few SDK add-ons registering components at run time, but I don't know any open source ones that I would recommend to look at off the top of my head.

A few points regarding that ABP implementation and what to change to make it work with the SDK:

  • The category manager is available via Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager).
  • The component registrar should be available by requiring also components from the chrome module and then components.manager.getService(Ci.nsIComponentRegistrar).
  • As Adblock Plus, you must unregister your component yourself on unload.
  • The unload part is unfortunely also a bit tricking, since you cannot synchronously unregister your component and the category entry, due to Bug 753687. Adblock Plus therefore does it async using Util.runAsync, which just dispatches a runnable (event, if you like) to the main thread. I don't think you can use any SDK stuff here, as the SDK will clean up before any async code gets a chance to run, so you'd need to use a low-level XPCOM runnable (or timer) yourself.
  • Your code will register your component at runtime. You won't touch harness-options or anything like that.

(I also implemented a generic component register function myself, but that again is not SDK code, and would need to be adapted to run in the SDK, just like the ABP one. It is also very similar to the ABP one.)

Now my nsIContentPolicy sdk-based component look like this. File interceptor.js:

'use strict';

var { Class } = require('sdk/core/heritage');
var xpcom = require('sdk/platform/xpcom');
var { Cc, Ci, Cu, Cm } = require('chrome');
var categoryManager = Cc["@mozilla.org/categorymanager;1"]
                      .getService(Ci.nsICategoryManager);


// nsIDOMNode
const TYPE_DOCUMENT_NODE        = Ci.nsIDOMNode.DOCUMENT_NODE;


/// Interceptor


var contractId = "@deferredto.com/Interceptor;1";

var Interceptor = Class({
  extends:  xpcom.Unknown,
  interfaces: [ 'nsIContentPolicy' ],
  get wrappedJSObject() this,

  shouldLoad : function dt_shouldLoad(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) {

        let result = Ci.nsIContentPolicy.ACCEPT;

        return result;
    },

    shouldProcess: function () Ci.nsIContentPolicy.ACCEPT
});

var factory = xpcom.Factory({
  contract: contractId,
  Component: Interceptor,
  unregister: false // see https://bugzilla.mozilla.org/show_bug.cgi?id=753687
});

/// unload 
var unload = require("sdk/system/unload");

unload.when(function() {
  function trueUnregister() {
    categoryManager.deleteCategoryEntry("content-policy", contractId, false);
    try {
      console.log("xpcom.isRegistered(factory)="  + xpcom.isRegistered(factory));
      console.log("trueUnregister");
      xpcom.unregister(factory);
      console.log("xpcom.isRegistered(factory)="  + xpcom.isRegistered(factory));
    } catch (ex) {
        Cu.reportError(ex);
    }      
  }
  if ("dispatch" in Cu) {
    console.log('"dispatch" in Cu');
    Cu.dispatch(trueUnregister, trueUnregister);
  } else {
    console.log('"dispatch" not! in Cu');
    Cu.import("resource://gre/modules/Services.jsm");
    Services.tm.mainThread.dispatch(trueUnregister, 0);
  }
});


//xpcom.register(factory);

var interceptor = Cc[contractId].createInstance(Ci.nsIContentPolicy);

categoryManager.deleteCategoryEntry("content-policy", contractId, false);
categoryManager.addCategoryEntry("content-policy", contractId, contractId, false, true);

And you can use it from sdk like this:

var interceptor = require("./interceptor");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!