Firefox的插件nsIContentPolicy的一个例子? [英] An example of nsIContentPolicy for firefox addon?

查看:112
本文介绍了Firefox的插件nsIContentPolicy的一个例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 NsIContentPolicy ,并且已经在整个Stackoverflow中搜索了实现NsIContentPolicy的正确教程,但是一切都是徒劳。我知道Adblock使用NsIContentPolicy作为他们的主要武器。 Adblock逆向工程并没有帮助我理解如何实现NsIContentPolicy。有没有简单的插件使用NsIContentPolicy进行学习,或者任何有关NsIContentPolicy的好教程?

解决方案

我不知道有什么好的教程但我可以给你一些最小的示例代码:

Cu.import(resource:// GRE /模块/ XPCOMUtils.jsm);
Cu.import(resource://gre/modules/Services.jsm);

let policy =
{
classDescription:Test content policy,
classID:Components.ID({12345678-1234-1234-1234-123456789abc} ),
contractID:@ adblockplus.org / test-policy; 1,
xpcom_categories:[content-policy],
$ b $ init:function()
{
让registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(this.classID,this.classDescription,this.contractID,this);

let catMan = Cc [@ mozilla.org/categorymanager;1\"].getService(Ci.nsICategoryManager);
(let this category in this.xpcom_categories)
catMan.addCategoryEntry(category,this.contractID,this.contractID,false,true);

onShutdown.add((function()
{
for each(let this category in this.xpcom_categories)
catMan.deleteCategoryEntry(category,this.contractID,false );

//这需要异步运行,参见bug 753687
Services.tm.currentThread.dispatch(function()
{
registrar.unregisterFactory .classID,this);
} .bind(this),Ci.nsIEventTarget.DISPATCH_NORMAL);
})。bind(this));


// nsIContentPolicy接口实现
shouldLoad:function(contentType,contentLocation,requestOrigin,node,mimeTypeGuess,extra)
{
dump shouldLoad:+ contentType ++
(contentLocation?contentLocation.spec:null)++
(requestOrigin?requestOrigin.spec:null)++
node ++
mimeTypeGuess +\\\
);
返回Ci.nsIContentPolicy.ACCEPT;


应该:函数(contentType,contentLocation,requestOrigin,node,mimeTypeGuess,extra)
{
dump(shouldProcess:+ contentType + +
(contentLocation?contentLocation.spec:null)++
(requestOrigin?requestOrigin.spec:null)++
node ++
mimeTypeGuess +\\\
);
返回Ci.nsIContentPolicy.ACCEPT;


// nsIFactory接口实现
createInstance:function(outer,iid)
{
if(outer)
throw Cr .NS_ERROR_NO_AGGREGATION;
返回this.QueryInterface(iid);
},

// nsISupports接口实现
QueryInterface:XPCOMUtils.generateQI([Ci.nsIContentPolicy,Ci.nsIFactory])
};

policy.init();

这来自我用来查看内容策略实现问题的最小内容策略实现 - 它除了将所有内容策略调用转储到控制台之外,不会执行任何操作( window.dump 文档)。显然,在真正的实现中, classDescription classID contractID onShutdown 属于我正在使用的私有框架:这个扩展是无重启的,这就是为什么它需要注册组件手动,也将运行此代码,以删除它,如果它在浏览器会话期间关闭。



您也可以下载完整的扩展名: testpolicy.xpi


I have read NsIContentPolicy and have searched whole Stackoverflow for a proper tutorial for implementing NsIContentPolicy, but all in vain. I know that Adblock uses NsIContentPolicy as their main weapon. Reverse engineering Adblock didn't help me to understand how to implement NsIContentPolicy. Is there any simple addon using NsIContentPolicy for learning, or any good tutorial on NsIContentPolicy?

解决方案

I am not aware of any good tutorial but I can give you some minimal example code:

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

let policy =
{
  classDescription: "Test content policy",
  classID: Components.ID("{12345678-1234-1234-1234-123456789abc}"),
  contractID: "@adblockplus.org/test-policy;1",
  xpcom_categories: ["content-policy"],

  init: function()
  {
    let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
    registrar.registerFactory(this.classID, this.classDescription, this.contractID, this);

    let catMan = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
    for each (let category in this.xpcom_categories)
      catMan.addCategoryEntry(category, this.contractID, this.contractID, false, true);

    onShutdown.add((function()
    {
      for each (let category in this.xpcom_categories)
        catMan.deleteCategoryEntry(category, this.contractID, false);

      // This needs to run asynchronously, see bug 753687
      Services.tm.currentThread.dispatch(function()
      {
        registrar.unregisterFactory(this.classID, this);
      }.bind(this), Ci.nsIEventTarget.DISPATCH_NORMAL);
    }).bind(this));
  },

  // nsIContentPolicy interface implementation
  shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra)
  {
    dump("shouldLoad: " + contentType + " " +
                          (contentLocation ? contentLocation.spec : "null") + " " +
                          (requestOrigin ? requestOrigin.spec : "null") + " " +
                          node + " " +
                          mimeTypeGuess + "\n");
    return Ci.nsIContentPolicy.ACCEPT;
  },

  shouldProcess: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra)
  {
    dump("shouldProcess: " + contentType + " " +
                            (contentLocation ? contentLocation.spec : "null") + " " +
                            (requestOrigin ? requestOrigin.spec : "null") + " " +
                            node + " " +
                            mimeTypeGuess + "\n");
    return Ci.nsIContentPolicy.ACCEPT;
  },

  // nsIFactory interface implementation
  createInstance: function(outer, iid)
  {
    if (outer)
      throw Cr.NS_ERROR_NO_AGGREGATION;
    return this.QueryInterface(iid);
  },

  // nsISupports interface implementation
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIFactory])
};

policy.init();

This comes from the minimal content policy implementation I use to look at issues with the content policies implementation - it doesn't do anything other than dumping all content policies calls to the console (window.dump documentation). Obviously, in a real implementation the fields classDescription , classID and contractID should be changed to something proper. onShutdown belongs to the private framework I am using: this extension is restartless which is why it needs to register the component "manually" and will also run this code to remove it if it is shut down during a browser session.

You can also download the complete extension: testpolicy.xpi.

这篇关于Firefox的插件nsIContentPolicy的一个例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆