唯一的选择是将这个代码块包含到我的每个函数中? [英] The only option is to include that block of code into each of my functions?
问题描述
UniversalXPConnect
特权。 netscape.security.PrivilegeManager.enablePrivilege( 'UniversalXPConnect');
所以,我的函数看起来像这样:
function oneOfMyFunctions(){
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
// ...
}
其实,我还有 try
到 catch
特权被拒绝时的异常。看起来如下:
pre $ try $ $ $
catch(e){
// ...
}
pre>
我宁愿将它作为一个单独的函数,并从我的函数中调用它,如下所示:
<$ (){
// ...
}其他$ {
// ...函数oneOfMyFunctions(){
if(enablePrivilege()) 。
$ b $ p
$ b $ p $ enablePrivilege 函数如下:
function enablePrivilege(){
try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
} catch(e){
return false;
}
return true;
$ b但是,出于安全原因,这是不可能的,因为只有在请求函数的范围。
因此,唯一的选择是将这段代码包含到我的每个函数中?
更新:
正如我要尝试
catch
其他一些异常我已经用下面的设计结束了:
function readFile(path,start,length){
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var file = Components.classes ['@ mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
var istream = Components.classes ['@ mozilla.org/network/file-input-stream;1'].createInstance(Components.interfaces.nsIFileInputStream);
istream.init(file,-1,-1,false);
istream.QueryInterface(Components.interfaces.nsISeekableStream);
istream.seek(0,start);
var bstream = Components.classes ['@ mozilla.org/binaryinputstream;1'].createInstance(Components.interfaces.nsIBinaryInputStream);
bstream.setInputStream(istream);
return bstream.readBytes(length);
}
var filepath ='C:\\test.txt',start = 440,length = 5;
尝试{
console.log(readFile(filepath,start,length));
} catch(e){
if(e.name =='Error')console.log('没有授予读取文件的权限。
else console.log('试图读取文件时发生错误');
解决方案您可以使enablePrivilege成为一种排序包装函数,接受一个函数作为参数,然后调用它自己,如此
函数enablePrivilege(funcParam){
//启用特权,在try-catch中
funcParam();
$ / code>
这样,当你像这样调用它时
enablePrivilege(oneOfMyFunctions);
需要权限的函数应该有它们,因为它在enablePrivilege的范围内调用。 p>
Several of my functions require the UniversalXPConnect
privilege to be enabled.
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
So, my functions look like this:
function oneOfMyFunctions() {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
// ...
}
Actually, I also try
to catch
the exception when the privilege is denied. Looks as follows:
try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
// ...
} catch (e) {
// ...
}
I'd rather to make that a separate function and call it from within my functions as follows:
function oneOfMyFunctions() {
if (enablePrivilege()) {
// ...
} else {
// ...
}
}
Given that the enablePrivilege
function would be as follows:
function enablePrivilege() {
try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
} catch (e) {
return false;
}
return true;
}
But, for security reasons, that is impossible as the privilege is granted only in the scope of the requesting function.
So, the only option is to include that block of code into each of my functions?
UPDATE:
As I am going to also try
to catch
some other exceptions I've ended up with the following design:
function readFile(path, start, length) {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var file = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
var istream = Components.classes['@mozilla.org/network/file-input-stream;1'].createInstance(Components.interfaces.nsIFileInputStream);
istream.init(file, -1, -1, false);
istream.QueryInterface(Components.interfaces.nsISeekableStream);
istream.seek(0, start);
var bstream = Components.classes['@mozilla.org/binaryinputstream;1'].createInstance(Components.interfaces.nsIBinaryInputStream);
bstream.setInputStream(istream);
return bstream.readBytes(length);
}
var filepath = 'C:\\test.txt', start = 440, length = 5;
try {
console.log(readFile(filepath, start, length));
} catch (e) {
if (e.name == 'Error') console.log('The privilege to read the file is not granted.');
else console.log('An error happened trying to read the file.');
}
解决方案 You could make enablePrivilege a sort of wrapper function that accepts a function as a parameter that it then calls inside itself, like so
function enablePrivilege(funcParam) {
//enable privileges, in try-catch
funcParam();
}
so that when you call it like so
enablePrivilege(oneOfMyFunctions);
the function that needs the privileges should have them since it is called inside the scope of enablePrivilege.
这篇关于唯一的选择是将这个代码块包含到我的每个函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!