Firefox 6:XPCOM 组件中的 QueryInterface(nsIDOMWindow::GetIID() 问题,用于从 js 传递的浏览器内容窗口
我正在努力为 Mac OS 上的附加组件添加对 Firefox 6 的支持,其中以下逻辑在 Firefox 4,5 版本中有效,但在 Firefox 6 中失败。
XPCOM 组件具有 IObserverClient 的子类,并且它将自己添加为自定义事件的观察者。 此自定义事件是从浏览器overlay.js 发布的,传递所选浏览器的内容窗口。
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
if (observerService) {
var data = gBrowser.selectedBrowser.contentWindow.location.href;
observerService.notifyObservers(gBrowser.selectedBrowser.contentWindow, JSEventTopic, data);
}
在 XPCOM 组件处理程序中,尝试从 nsISupports 获取 nsIDOMWindow 接口
void XXX::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
{
nsCOMPtr<nsIDOMWindow> pWin;
aSubject->QueryInterface(nsIDOMWindow::GetIID(), getter_AddRefs(pWin));
}
问题是,对于 Firefox 6,pWin 为零。在 Firefox 4 和 5 中,pWin
与预期一致,而不是 nil。
I am working on adding support for Firefox 6 for my add-on on Mac OS, where the following logic is working in Firefox 4,5 versions but fails in Firefox 6.
XPCOM component has subclass of IObserverClient
and which adds itself as observer for a custom event.
This custom event is posted from browser overlay.js passing the selected browser's content window.
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
if (observerService) {
var data = gBrowser.selectedBrowser.contentWindow.location.href;
observerService.notifyObservers(gBrowser.selectedBrowser.contentWindow, JSEventTopic, data);
}
In XPCOM components handler, trying to get the nsIDOMWindow
interface from nsISupports
void XXX::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData)
{
nsCOMPtr<nsIDOMWindow> pWin;
aSubject->QueryInterface(nsIDOMWindow::GetIID(), getter_AddRefs(pWin));
}
The problem is, with Firefox 6 pWin
is nil. In Firefox 4 and 5 pWin
is as expected and not nil.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
眼前的问题似乎是,无论作为
aSubject
传递的对象都没有实现nsIDOMWindow
接口,这自然会导致QueryInterface
失败并产生空值。您不能将一个对象 QI 到它未实现的接口,也不能将 QI 为 null 到任何对象。我暂时不知道是什么导致了这种变化,但这里有一些需要检查的事情可能会帮助您找到真正的问题:
nsIDOMWindow
。aTopic
是否包含您期望的值,而不是其他事件字符串。The immediate problem seems to be that whatever object is being delivered as
aSubject
doesn't implement thensIDOMWindow
interface, which naturally causes theQueryInterface
to fail and yield null. You can't QI an object to an interface that it doesn't implement, and you can't QI null to anything.I don't know off-hand what causred this change, but here are a few things to check that might help you find the real problem:
nsIDOMWindow
.aTopic
contains the value you expect, and not some other event string.我对附加开发不太熟悉。但这在我的应用程序(C++)中对我有用。
I am not much familiar with add-on development. But this works for me in my application(C++).