Can a Native Client module render HTML generated by C++ code?

1k Views Asked by At

I have some data analysis code that processes an input file and outputs HTML and/or SVG. It is C++ and CPU-intensive.

I would like to create a browser plugin so that my code can be used without leaving the browser. Google's Native Client framework and the Pepper API sound interesting but perhaps is a little too restrictive. From a Native Client module is it possible to do the following via Pepper?

  1. Access a local file on the users filesystem. I know NaCl sandbox generally prevents this but there is surely a way to pass a user-selected file from the Javascript layer down into NaCL?

  2. Output HTML or SVG back to the browser. Is there any way that the C++ can modify the containing document's DOM?

2

There are 2 best solutions below

0
On BEST ANSWER

i am less certain for (1), but (2) is definitely not directly possible.

for (1), the Pepper API is supposed to allow file pickers to make files available to NaCl modules, the same way that WebFS allows it for JavaScript. i'm not sure of the implementation status of this.

for (2), C++ code cannot directly manipulate the DOM. however, the NaCl module can use the PostMessage interface to send strings to JavaScript code, which can then do the actual DOM manipulation. note that in Chrome, NaCl modules are event handlers. this means that if the computation is started by some event and run synchronously (and it is CPU intensive), it would cause the browser to lose interactive responsiveness, or to become janky, since the event handler would take too long to return. a better way is to spawn a background thread to do the work in the event handler (or wake a worker thread up), and return from the event handler immediately. when the computation is done and results are ready, PostMessage the result back to let the JavaScript thread wake up (again as an event handler) and do the DOM manipulation. note that currently PostMessage must be invoked on the main thread, so the worker thread must use CallOnMainThread to wake up the main event handler thread so that it can in turn do the PostMessage.

isn't continuation passing style fun?

0
On
  1. A file can be uploaded to the HTML file system using a file input element and JavaScript, where it can then be manipulated by both JavaScript and NaCl. The nacl_io library implements standard posix methods such as fread, so once you've saved your file it's pretty straight forward. See here for JS (don't be put off by the deprecation message, it's implemented in Chrome), and here for NaCl.

  2. As @Bennet Yee mentioned, the NaCl module can not modify the page, but instead passes messages back to the browser window which are handled by your JavaScript. Sounds like you'll probably want to pass back some JSON data. See here for Google's docs on the messaging system.