[英]Notifying javascript of external event from XPcom
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++. 我的html页面中运行着javascript,该页面调用了我用C ++编写的XPcom。 So far, so good. 到现在为止还挺好。
This XPcom retrieves a reference to the observer-service ( do_GetService("@mozilla.org/observer-service;1")
); 该XPcom检索对观察者服务的引用( do_GetService("@mozilla.org/observer-service;1")
); fires a notify which is 'seen' by the javascript; 触发javascript可以“看到”的通知; the XPcom creates a windows thread (AFX), passes a copy of the observer-service reference and returns to the javascript caller; XPcom创建一个Windows线程(AFX),传递观察者服务引用的副本并返回给javascript调用者; expecting the XPcom thread to send additional notifies at appropriate times (based on external events). 期望XPcom线程在适当的时候发送其他通知(基于外部事件)。
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. 但是,notify未能到达javascript中,我最初的想法是notify方法将不会从“不同”线程传递通知。 I've used the VStudio debugger to confirm the program sequence is as expected; 我已经使用VStudio调试器来确认程序顺序是否符合预期; ie the external event is being received by the thread and the notify method is being called... but, no notify event arrives. 即,该线程正在接收外部事件,并且正在调用notify方法……但是,没有notify事件到达。
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: 我不喜欢使用notify的想法:
The objective is quite simple. 目标很简单。 deliver ansynchronous events or notifications from an XPcom thread to the main, or even sub, thread of the javascript; 从XPcom线程向javascript的主线程甚至子线程传递异步事件或通知; but, I'm getting less than 10% traction here. 但是,我在这里的牵引力不到10%。 Has anyone accomplished this? 有人做到了吗? or have ideas as to how to get it done? 或对如何完成工作有想法?
Thanks much. 非常感谢。
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
). 您是正确的,观察者服务仅在主线程上起作用(检查Notify()
调用的返回值,它应该是NS_ERROR_UNEXPECTED
)。 You are also correct as far as the solution goes - dispatch an event to the main thread, via NS_DispatchToMainThread()
. 就解决方案而言,您也是正确的-通过NS_DispatchToMainThread()
将事件调度到主线程。 As to the actual implementation, function NS_NewRunnableMethod()
should help (unless you want to create your custom nsIRunnable
implementation - eg to pass parameters). 对于实际的实现,函数NS_NewRunnableMethod()
应该会有所帮助(除非您要创建自定义的nsIRunnable
实现-例如,传递参数)。 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. 在这里, xpcomComponent
是对XPCOM组件实例的引用,而nsMyComponent::processEvent
是需要在主线程上调用的方法的引用。 That method would then be able to notify the observer. 然后,该方法将能够通知观察者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.