Firefox 6:XPCOM 组件中的 QueryInterface(nsIDOMWindow::GetIID() 问题,用于从 js 传递的浏览器内容窗口

发布于 2024-12-01 04:30:27 字数 982 浏览 0 评论 0原文

我正在努力为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

━╋う一瞬間旳綻放 2024-12-08 04:30:27

眼前的问题似乎是,无论作为 aSubject 传递的对象都没有实现 nsIDOMWindow 接口,这自然会导致 QueryInterface 失败并产生空值。您不能将一个对象 QI 到它未实现的接口,也不能将 QI 为 null 到任何对象。

我暂时不知道是什么导致了这种变化,但这里有一些需要检查的事情可能会帮助您找到真正的问题:

  • 确保您接收到的主题不为空,并且您所在的指针尝试 QI 是侦听器方法收到的参数。
  • 在您的 JavaScript 代码中,检查您传递的窗口对象是否为 nil,并且实际上应该实现 nsIDOMWindow
  • 检查aTopic 是否包含您期望的值,而不是其他事件字符串。

The immediate problem seems to be that whatever object is being delivered as aSubject doesn't implement the nsIDOMWindow interface, which naturally causes the QueryInterface 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:

  • Make sure the subject you're receiving isn't null, and that the pointer you're trying to QI is the argument received by the listener method.
  • In your JavaScript code, check that the window object you're passing isn't nil, and is actually something that should implement nsIDOMWindow.
  • Check that aTopic contains the value you expect, and not some other event string.
冬天的雪花 2024-12-08 04:30:27

我对附加开发不太熟悉。但这在我的应用程序(C++)中对我有用。

nsCOMPtr<nsIDOMWindow> domWindow; 
nsresult rv = mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));

nsCOMPtr<nsIDOMWindowUtils> windowUtils(do_GetInterface(domWindow)); 

I am not much familiar with add-on development. But this works for me in my application(C++).

nsCOMPtr<nsIDOMWindow> domWindow; 
nsresult rv = mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));

nsCOMPtr<nsIDOMWindowUtils> windowUtils(do_GetInterface(domWindow)); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文