我正在测试一个应用程序,其中包含 Firefox 扩展作为一个组件。它最初在 FF3.5.5 为最新版本时部署,并在 3.5.6 和 3.5.7 中幸存下来。但是在 FF3.6 上,我在错误控制台中收到以下信息:
Warning: reference to undefined property Components.interfaces.nsIProcess2
Source file: chrome://overthewall/content/otwhelper.js
Line: 55
Error: Component returned failure code: 0x80570018 (NS_ERROR_XPC_BAD_IID)
[nsIJSCID.createInstance]
Source file: chrome://overthewall/content/otwhelper.js
Line: 55
抛出错误的函数是:
48 function otwRunHelper(cmd, aCallback) {
49 var file =
50 Components.classes["@mozilla.org/file/local;1"].
51 createInstance(Components.interfaces.nsILocalFile);
52 file.initWithPath(otwRegInstallDir+'otwhelper.exe');
53
54 otwProcess = Components.classes["@mozilla.org/process/util;1"]
55 .createInstance(Components.interfaces.nsIProcess2);
56
57 otwProcess.init(file);
58 var params = new Array();
59 params = cmd.split(' ');
60
61 otwNextCallback = aCallback;
62 otwObserver = new otwHelperProcess();
63 otwProcess.runAsync(params, params.length, otwObserver, false);
64 }
如您所见,该函数所做的就是使用一些命令行参数运行外部 EXE 帮助程序文件(由注册表项定位),并设置观察者以异步等待响应并处理退出代码。
有问题的行意味着 FF3.6 中不再定义 Components.interfaces.nsIProcess2。它去哪儿了?我在 Mozilla 文档中找不到任何表明它在最新版本中已被更改的内容。
最佳答案
nsIProcess2 上的方法已移至 nsIProcess。为了使您的代码在两个版本中都能工作,请更改此行:
otwProcess = Components.classes["@mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess2);
对此:
otwProcess = Components.classes["@mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess2 || Components.interfaces.nsIProcess);
您仍然会收到警告,但错误将会消失,并且您的代码在两个版本中都可以正常工作。您还可以将接口(interface) iid 存储在变量中并使用该变量:
let iid = ("nsIProcess2" in Components.interfaces) ?
Components.interfaces.nsIProcess2 :
Components.interfaces.nsIProcess;
otwProcess = Components.classes["@mozilla.org/process/util;1"]
.createInstance(iid);
关于xpcom - Firefox 3.6 中的 Components.interfaces.nsIProcess2 —— 它去了哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2221469/