Search code examples
google-nativeclientppapi

Calling asynchronous PPAPI functions from a ppapi_simple program


I have a NaCl C program using ppapi_simple. I need to call asynchronous PPAPI functions such as PPB_NetworkMonitor::UpdateNetworkList().

When I tried implementing it naively, the callback I passed to UpdateNetworkList() was never called. Looking at the ppapi_simple source code, I noticed that ppapi_simple never calls PPB_MessageLoop::Run() on its internal message loop, which probably explains why my callback is never called.

What is the right way to use asynchronous PPAPI functions in a ppapi_simple program? Should I create my own thread and message loop?

EDIT: According to MessageLoop's documentation, it's only needed for making PPAPI calls on a thread. So I tried calling UpdateNetworkList() on the main thread using CallOnMainThread() and it does work - my callback is called. Not sure it's the best solution though.


Solution

  • ppapi_simple runs everything off of the main thread, and assumes that the user is using blocking PPAPI calls. This makes porting code simpler. In the case where you need callbacks, you've already found the two solutions:

    1. Call the function on the main thread (which runs an implicit message loop)
    2. Start a new thread, run a message loop, and post work to that message loop to call your function.

    As long as you are not doing a lot of work in the callback, I'd say that calling on the main thread is simpler. The concern with doing too much work on the main thread is that it can make your page unresponsive.