gpt4 book ai didi

javascript - Prefs 为什么 QI 分支?

转载 作者:行者123 更新时间:2023-11-30 12:43:00 26 4
gpt4 key购买 nike

大家好,我正在尝试连接偏好听众和其他东西,但在代码中我发现它们正在 QI'ing,我不知道为什么。你们知道为什么吗?这些是我看到的代码示例:

this._branch = Services.prefs.getBranch(branch_name);
this._defaultBranch = Services.prefs.getDefaultBranch(branch_name);
this._branch.QueryInterface(Ci.nsIPrefBranch2);

在上面他们没有 QI _defaultBranch,这是为什么呢?为什么 qi nsIPrefBranch2 为什么不只是 nsIPrefBranch

这段代码片段来自xul学校:https://developer.mozilla.org/en-US/docs/Adding_preferences_to_an_extension

 this.prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.stockwatcher2.");
this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch);
this.prefs.addObserver("", this, false);

他们在这里 QI 只是 nsIPrefBranch

我对所有方法感到困惑,哪种方法才是正确的方法?

最佳答案

nsIPrefBranch2 是一个遗留接口(interface),如今它继承了 nsIPrefBranch 但在其他方面为空。它仍然存在,以便在 Firefox 3.x 时代编写并因此使用 nsIPrefBranch2 的“旧”代码不会中断;否则它早就被删除了。因此,任何不应在 Firefox 3.x 上运行的新代码都无需 QI 到 nsIPrefBranch2

(顺便说一句:nsIFilensILocalFile 本质上是同一个故事)。

然而,曾几何时,nsIPrefBranch2实际上added addObserver and removeObserver (现在是 nsIPrefBranch 的一部分)。因此,要使用 addObserver,您需要那些 QI 调用。 IIRC,使用新接口(interface)而不是扩展旧接口(interface)的原因是 ABI(二进制)兼容性。由于 Firefox 4 无法保证主要版本之间的二进制兼容性,因此这两个接口(interface)被合并了。

getBranch().QueryInterfac(Ci.nsIPrefBranch) 完全没有意义。我的猜测是代码曾经在某个时候说 nsIPrefBranch2 而作者做了一个自动化的 nsIPrefBranch2 -> nsIPrefBranch 稍后重写......

所以,再重复一遍:新代码不需要 QI 到 nsIPrefBranch2,当然也不需要 QI nsIPrefBranchnsIPrefBranch

我会将您的示例重写为:

this._branch = Services.prefs.getBranch(branch_name); // Already returns nsIPrefBranch
this._defaultBranch = Services.prefs.getDefaultBranch(branch_name); // Already returns nsIPrefBranch

和:

this.prefs = Services.prefs.getBranch("extensions.stockwatcher2.");
this.prefs.addObserver("", this, false);

PS:看起来 QI(Ci.nsIPrefBranch) 代码确实是 automatic rewrite .我现在删除了 QI 调用。

关于javascript - Prefs 为什么 QI 分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23717720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com