Firefox 29,XPCOM和wrappedJSObject

时间:2014-05-23 17:43:42

标签: javascript firefox firefox-addon firefox-addon-sdk xpcom

我们正在使用Firefox的自定义Javascript附加组件,这在我们的一些内部网站点中使用。该附加组件应该从用户的PC加载特定的文本文件,然后将特定的变量暴露给我们的一些内部网页。

目前的实施工作从FF3到FF28。在FF29中,wrappedJSObject的行为已经改变。

这是我在附加组件代码中得到的内容:

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");  

function PrivateClass() {
  this.wrappedJSObject = this;
}

PrivateClass.prototype = {
// Component details
classDescription: "...",
classID:          Components.ID("{...}"),
contractID:       "@foo.bar/PrivateClass;1",
QueryInterface:   XPCOMUtils.generateQI([Ci.nsIClassInfo]),

getInterfaces: function(countRef) {
    var interfaces = [Ci.nsIClassInfo, Ci.nsISupports];
    countRef.value = interfaces.length;
    return interfaces;
},

implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
flags: Ci.nsIClassInfo.DOM_OBJECT,
getHelperForLanguage: function(count) { return null; },

// We use the default _xpcom_factory

// Categories to register
_xpcom_categories: [{
  category: "JavaScript global property",
  entry: "PrivateClass",          // optional, defaults to the object's classDescription. Needed for FF3.
  value: "@foo.bar/PrivateClass;1",  // optional, defaults to the object's contractID. Needed for FF3.
  service: false
}],

// nsISecurityCheckedComponent permissions
// return "AllAccess"; / return "NoAccess";
canCreateWrapper : function canCreateWrapper(aIID) { return "AllAccess"; },
canCallMethod: function canCallMethod(aIID, methodName) { return "AllAccess"; },
canGetProperty: function canGetProperty(aIID, propertyName) { return "AllAccess"; },  // needed to access wrappedJSObject
canSetProperty: function canSetProperty(aIID, propertyName) { return "NoAccess"; },

getFunctionA : function() { return "This is A"; },
getFunctionB : function() { return "This is B"; },

// New functionality, needed for FF 17+
// https://developer.mozilla.org/en-US/docs/XPConnect_wrappers#__exposedProps__
__exposedProps__ : { getFunctionA : "r", getFunctionB : "r" }
}

在客户页面中:

if (typeof PrivateClass != "undefined") {
  var obj = PrivateClass.wrappedJSObject;
  var a = obj.getFunctionA()
  var b = obj.getFunctionB();
}

但是,现在FF在此行返回Error: Attempt to use .wrappedJSObject in untrusted code

var obj = PrivateClass.wrappedJSObject;

我已经在这篇博客文章中了解了FF30中对wrappedJSObject的即将发生的变化: https://blog.mozilla.org/addons/2014/04/10/changes-to-unsafewindow-for-the-add-on-sdk/

...但推荐的解决方案对我不起作用

请注意,wrappedJSObject解决方案位于官方Mozilla开发人员文档herehere中。它也可以在互联网上找到(例如here),但显然它在FF29中以某种方式被破坏/更改,因此这些都不适用。

我已经检查了优秀的FF附加组件jsPrintSetup,并且它没有使用wrappedJSObject(尽管它也有一个C ++二进制组件,这可能解释了这一点)。我已经通过wrappedJSObject读了很多关于这个问题的论坛帖子,但是我找不到任何关于它的行为的特定变化。任何人都可以了解需要做些什么才能使这个功能在FF29 +下工作?

1 个答案:

答案 0 :(得分:1)

是的,我添加了一个检查不受信任内容的检查,检查XPCOM组件的内容,它真的没有业务。它应该仍然可以在特权代码中正常工作。

Also note that nsISecurityCheckedComponent was removed,所以你不再需要那个部分了。

通常,向内容公开API的最具前瞻性的方法是使用exportFunction将其直接注入内容范围。

Extra info

相关问题