通知 JavaScript 来自 XPcom 的外部事件

发布于 2024-12-29 02:33:38 字数 974 浏览 2 评论 0原文

我一直在尝试为看似相对简单的场景找到解决方案。我在 html 页面中运行 JavaScript,该页面调用我用 C++ 编写的 XPcom。到目前为止,一切都很好。

此 XPcom 检索对观察者服务的引用 (do_GetService("@mozilla.org/observer-service;1"));触发 JavaScript“看到”的通知; XPcom 创建一个 Windows 线程 (AFX),传递观察者服务引用的副本并返回给 JavaScript 调用者;期望 XPcom 线程在适当的时间(基于外部事件)发送附加通知。

然而,通知无法到达 JavaScript,我最初的想法是通知方法不会从“不同”线程传递通知。我使用 VStudio 调试器来确认程序序列是否符合预期;即线程正在接收外部事件并且正在调用通知方法...但是,没有通知事件到达。

我在网上读了很多帖子,但没有什么真正“钉住”我想要解决的特定场景。我不喜欢使用通知的想法:

  • 我尝试过通过 NS_DispatchToCurrentThread 进行事件通知,但这也不起作用,因为我没有来自 JavaScript 端的“事件”要传递。我可以在 XPcom 的上下文中创建我自己的一个,并且我可以“通知它”;但是,这只是一个 POC,旨在证明我可以从 XPcom 传递事件;现在我需要 JavaScript 端给我一个要通知的事件;
  • 我尝试将新的对象作为 nsiSupports arg 传递;但是,DispatchToCurrentThread 需要一个nsiRunnable,而我不知道如何传递其中之一(idl 文件不支持);
  • 我还考虑过使用某种与 nsiSupports 兼容的对象来“包装”事件;但是,我不确定这样做的细节。

目标很简单。将异步事件或通知从 XPcom 线程传递到 JavaScript 的主线程甚至子线程;但是,我在这里获得的关注度还不到 10%。 有没有人完成了这个,或者有关于如何完成它的想法?

I have been trying to find a solution to what seems to be relatively simple scenario. I have JavaScript running in an html page that makes a call to an XPcom that I have written in C++. So far, so good.

This XPcom retrieves a reference to the observer-service (do_GetService("@mozilla.org/observer-service;1")); fires a notify which is 'seen' by the JavaScript; the XPcom creates a windows thread (AFX), passes a copy of the observer-service reference and returns to the JavaScript caller; expecting the XPcom thread to send additional notifies at appropriate times (based on external events).

The notifies however fail to arrive in the JavaScript and my initial thought was the notify method will not deliver notifications from a 'different' thread. I've used the VStudio debugger to confirm the program sequence is as expected; ie the external event is being received by the thread and the notify method is being called... but, no notify event arrives.

I've read quite a few postings across the web and nothing really 'nails' the particular scenario I'm trying to address. I'm not married to the idea of using notify:

  • I've tried event notification via NS_DispatchToCurrentThread however that is not working out either because I don't have an "event" from the JavaScript side to deliver. I can create one of my own within the context of the XPcom and I can 'notify it'; but, that was just a POC to prove I could deliver events from XPcom; now I need for the JavaScript side to give me an event to notify;
  • I've tried passing a new'ed object as a nsiSupports arg; but, the DispatchToCurrentThread wants an nsiRunnable and I cannot figure out how to pass one of those (the idl file does not support);
  • I've also considered 'wrapping' the event with some sort of object that is compatible with nsiSupports; but, am unsure about the details of doing so.

The objective is quite simple. deliver asynchronous events or notifications from an XPcom thread to the main, or even sub, thread of the JavaScript; but, I'm getting less than 10% traction here.
Has anyone accomplished this, or have ideas as to how to get it done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

温柔戏命师 2025-01-05 02:33:38

你是对的,观察者服务仅在主线程上工作(检查 Notify() 调用的返回值,它应该是 NS_ERROR_UNEXPECTED)。就解决方案而言,您也是正确的 - 通过 NS_DispatchToMainThread() 向主线程分派事件。至于实际的实现,函数 NS_NewRunnableMethod() 应该有所帮助(除非您想创建自定义的 nsIRunnable 实现 - 例如传递参数)。您可能想做这样的事情:

nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(xpcomComponent, &nsMyComponent::processEvent);
nsresult rv = NS_DispatchToMainThread(event);

这里的 xpcomComponent 是对 XPCOM 组件实例的引用,而 nsMyComponent::processEvent 是需要在主线程上调用的方法。然后该方法就能够通知观察者。

You are correct, the observer service works only on the main thread (check the return value of your Notify() call, it should be NS_ERROR_UNEXPECTED). You are also correct as far as the solution goes - dispatch an event to the main thread, via NS_DispatchToMainThread(). As to the actual implementation, function NS_NewRunnableMethod() should help (unless you want to create your custom nsIRunnable implementation - e.g. to pass parameters). You probably want to do something like this:

nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(xpcomComponent, &nsMyComponent::processEvent);
nsresult rv = NS_DispatchToMainThread(event);

Here xpcomComponent would be a reference to your XPCOM component instance and nsMyComponent::processEvent its method that needs to be called on main thread. That method would then be able to notify the observer.

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