【问题标题】:Firefox 29, XPCOM and wrappedJSObjectFirefox 29、XPCOM 和 WrappedJSObject
【发布时间】:2014-05-23 17:43:42
【问题描述】:

我们正在为 Firefox 使用仅 Javascript 的自定义插件,该插件用于我们的一些内部网站点。这个加载项应该从用户的 PC 加载特定的文本文件,然后将特定的变量暴露给我们的一些 Intranet 页面。

当前的实现适用于 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+ 下运行吗?

【问题讨论】:

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


    【解决方案1】:

    是的,我添加了一项针对不受信任的内容的检查,以检查 XPCOM 组件的内容,它确实没有任何业务要​​做。它应该仍然可以在特权代码中正常工作。

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

    一般来说,向内容公开 API 的最面向未来的方法是使用 exportFunction 将其直接注入内容范围。

    Extra info

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-22
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多