我的nsIContentPolicy Firefox / IceWeasel扩展XPCOMponent实现中缺少的应该调用的shouldLoad是什么? [英] What is missing in my nsIContentPolicy Firefox/IceWeasel extension XPCOMponent implementation for the shouldLoad to be called?

查看:100
本文介绍了我的nsIContentPolicy Firefox / IceWeasel扩展XPCOMponent实现中缺少的应该调用的shouldLoad是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总之

为什么根据下面详细介绍的代码,shouldLoad函数是不会被调用的?

更长的版本



我的目标



我试图构建一个Firefox / Iceweasel扩展,它将取消/重定向某些url请求。

背景



基于我在网上看到的一种方法(如果我要拦截每个请求,而不是仅仅顶部的文档)做这个是做一个实现了nsIContentPolicy接口的XPCOM组件,并且在扩展中注册这个组件,并且让shouldLoad函数检查请求的url并在适当的时候拒绝。 / p>

问题



我尽我所能实现了一个组件,并将其与我的扩展集成在一起。这个组件似乎是在compreg.dat等注册的意义上工作的,但是 - 据我所知,不应该调用shouldLoad函数。



详细信息



环境

我正在使用与FireFox 3.5.16相对应的IceWeasel版本在Debian Linux上开发。 b
扩展

我的扩展是基于在
给出的示例扩展名 http://kb.mozillazine.org/Getting_started_with_extension_development#reg-em
实质上,它所做的只是添加一个菜单项,打开类似警报的对话框打招呼的世界。它有一个onLoad注册火灾和警报说onLoad报告!。这个警报在每次都没有问题的情况下触发。

引用

我已经安装了扩展重定向器,看起来是以相同的原理运行
https://addons.mozilla.org/en-US/firefox/addon/所以它可能是我的代码中的错误,而不是环境不好)。
$ b $组件

我的组件实现基于我在互联网上找到的各种源。
我把它放在目录{pathtoextension} /helloworld/components/PolicyComponent.js中,它的代码如下:

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

var componentobj = null;

函数PolicyComponent()
{
// this.wrappedJSObject = this;


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


testFunction:function(){return你的组件没有被完全破坏! },

_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);




$ b shouldLoad:function(contentType,contentLocation,requestOrigin,aContext,mimeTypeGuess,extra)
{
if(contentType!= Ci.nsIContentPolicy.TYPE_DOCUMENT){
return Ci.nsIContentPolicy.ACCEPT;如果(-1!= contentLocation.spec.search(abc))
{
aContext.loadURI(http:// www。 stroustrup.com/,requestOrigin,null);
返回Ci.nsIContentPolicy.REJECT_REQUEST;
}
返回CI.nsIContentPolicy.ACCEPT;
},

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

};
var components = [PolicyComponent];

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



状态

组件似乎被IceWeasel所识别。如果我删除compreg.dat和xpti.dat并重新启动IceWeasel,则compreg.dat中的content-policy的grep会给出以下结果:

 
...
@ 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组件,@ abc.def.com / policycomp; 1
content 1,@ mozilla.org / no-data-protocol-content-policy; 1
...

所以看起来好像至少有一些正确的组件。
然而,我仍然可以在网址中使用abc来访问网页(这使得相信不应该调用shouldLoad函数)。

更多信息

我没有向chrome.manifest文件添加任何关于扩展的信息。我相信我不需要在FF / IW的3.5.x版本中做到这一点。

问题




  1. 有什么不对? :)


  2. 我需要添加一些东西给chrome.manifest?或者是只有FF 4 +?

  3. 我需要以某种方式进一步实例化组件/服务吗?像onLoad钩子中的overlay.js一样吗?

  4. 我是否需要以更明确的方式将组件注册为有效的扩展名,如果是这样的话,怎么样?


看起来你没有验证你的 shouldLoad 方法真的没有被调用。我建议使用 dump()函数来查看您的真实情况零件。它似乎更有可能被调用,但它会抛出一个异常像aContext.loadURI不是一个函数。原因是 aContext 对于 TYPE_DOCUMENT 调用是一个 HTMLDocument 对象它没有 loadURI 方法。你可能想调用 aContext.defaultView.location.replace()来代替。但是,从内容策略这样做是一个安全漏洞(事实上,从内容策略中执行任何可能导致网页脚本运行的内容都是安全漏洞)。如果您查看界面定义
因此,像这样的操作需要延迟发生,以确保它在发动机处于一致的状态。例如。你可以这样做:

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




怎么了? :)

除了上面提到的,你可能不应该定义你自定义的 _xpcom_factory 函数。内容策略总是作为服务使用,意味着它们是自动单例。您自己访问组件的代码当然也应该使用 getService()


我需要添加一些东西chrome.manifest?或者是只有FF 4 +?

是的,用于FF4 +。例如:

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




我需要以某种方式进一步实例化组件/服务吗?像onLoad钩子中的overlay.js一样?


不,内置的内容策略组件自动发生。


我是否需要以更明确的方式注册该组件对扩展有效,如果是这样,如何?


不知道你的意思。


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!

解决方案

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.

这篇关于我的nsIContentPolicy Firefox / IceWeasel扩展XPCOMponent实现中缺少的应该调用的shouldLoad是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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