本文介绍了QueryInterface的一些魔术字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很多时候,我有一个问题和帖子求助,很多次QueryInterface是解决方案。我已经很长一段时间的插件编程,但从来没有理解QueryInterface。这看起来像是魔法,就像它可以解决所有问题一样。
像:
window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor )
.getInterface(Ci.nsIDOMWindow);
所以我的问题是我看到一个QueryInterface链,我不明白为什么链,绝对没有得到如何创建自己的链。我不知道怎么知道从nsiInterfaceRequester链接到nsiWebNavigation,而不是从nsiInterfaceRequester直接链接到nsiDOMWindow
这里没什么特别的DOM窗口实现了允许获取相关对象的 nsIInterfaceRequestor
接口。一个这样的对象是与窗口相关联的docshell - 通过询问 nsIWebNavigation
接口得到它,但是它也实现了 nsIDocShell
和 nsIDocShellTreeItem
接口 - 和 nsIInterfaceRequestor
。尽管chrome和内容之间存在安全边界,但当前窗口的docshell可以让您访问顶层窗口的docshell。在那里你可以再次使用 nsIInterfaceRequestor
界面来询问docshell的相关窗口。
你可以只需在XPCOM组件中实现 nsIInterfaceRequestor
接口就像其他接口一样。我看不出有什么理由可以做到这一点,但是从DOM中隐藏与窗口有关的内部接口是一件很丑恶的事。
many times I have had an issue and post for help and so many times QueryInterface is the solution. I have been addon programming for a long time now but never understood QueryInterface. It seems like magic, like it holds the solution to everything.
like:
window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
So my question is I see a QueryInterface chain, I don't get why the chain, and I definitely don't get how to create my own chain. I don't get how do you know to chain from nsiInterfaceRequester to nsiWebNavigation and not from nsiInterfaceRequester straight to nsiDOMWindow
解决方案
There is really nothing special here. DOM windows implement the nsIInterfaceRequestor
interface that allows getting related objects. One such object is the docshell that is associated with the window - you get it by asking for the nsIWebNavigation
interface but it also implements the nsIDocShell
and nsIDocShellTreeItem
interfaces - and nsIInterfaceRequestor
. The docshell for the current window lets you get to the docshell for the top window, despite the security boundary between chrome and content. And there you can use the nsIInterfaceRequestor
interface again to ask the docshell for the window associated with it.
You can simply implement the nsIInterfaceRequestor
interface in your XPCOM component just like any other interface. I don't see any reason to ever do that however, it's an ugly hack to hide internal window-related interfaces from the DOM.
这篇关于QueryInterface的一些魔术字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!