Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(782)

Unified Diff: native_client_sdk/src/examples/demo/nacl_io/example.js

Issue 242533005: [NaCl SDK] nacl_io: Add flow control the JavaScript pipes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | native_client_sdk/src/examples/demo/nacl_io/index.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: native_client_sdk/src/examples/demo/nacl_io/example.js
diff --git a/native_client_sdk/src/examples/demo/nacl_io/example.js b/native_client_sdk/src/examples/demo/nacl_io/example.js
index 1a13580c6d7145cece2e2edb729f86d1fd839e8d..128bcd037013552b9f62742984e069d5f908498e 100644
--- a/native_client_sdk/src/examples/demo/nacl_io/example.js
+++ b/native_client_sdk/src/examples/demo/nacl_io/example.js
@@ -40,6 +40,33 @@ function attachListeners() {
var func = window[id];
buttonEl.addEventListener('click', func);
}
+
+ $('pipe_input_box').addEventListener('keypress', onPipeInput)
+ $('pipe_output').disabled = true;
+
+ $('pipe_name').addEventListener('change',
+ function() { $('pipe_output').value = ''; })
+}
+
+// Called with keypress events on the pipe input box
+function onPipeInput(e) {
+ // Create an arraybuffer containing the 16-bit char code
+ // from the keypress event.
+ var buffer = new ArrayBuffer(1*2);
+ var bufferView = new Uint16Array(buffer);
+ bufferView[0] = e.charCode;
+
+ // Pass the buffer in a dictionary over the NaCl module
+ var pipeSelect = $('pipe_name');
+ var pipeName = pipeSelect[pipeSelect.selectedIndex].value;
+ var message = {
+ pipe: pipeName,
+ operation: 'write',
+ payload: buffer,
+ };
+ nacl_module.postMessage(message);
+ e.preventDefault();
+ return false;
}
function onRadioClicked(e) {
@@ -279,24 +306,44 @@ function postCall(func) {
});
}
+function ArrayBufferToString(buf) {
+ return String.fromCharCode.apply(null, new Uint16Array(buf));
+}
+
// Called by the common.js module.
function handleMessage(message_event) {
var data = message_event.data;
if ((typeof(data) === 'string' || data instanceof String)) {
common.logMessage(data);
- } else {
- // Result from a function call.
- var params = data.args;
- var funcName = data.cmd;
- var callback = funcToCallback[funcName];
-
- if (!callback) {
- common.logMessage('Error: Bad message ' + funcName +
- ' received from NaCl module.');
- return;
+ } else if (data instanceof Object) {
+ var pipeName = data['pipe']
+ if (pipeName !== undefined) {
+ // Message for JavaScript I/O pipe
+ var operation = data['operation'];
+ if (operation == 'write') {
+ $('pipe_output').value += ArrayBufferToString(data['payload']);
+ } else if (operation == 'ack') {
+ common.logMessage(pipeName + ": ack:" + data['payload']);
+ } else {
+ common.logMessage('Got unexpected pipe operation: ' + operation);
+ }
+ } else {
+ // Result from a function call.
+ var params = data.args;
+ var funcName = data.cmd;
+ var callback = funcToCallback[funcName];
+
+ if (!callback) {
+ common.logMessage('Error: Bad message ' + funcName +
+ ' received from NaCl module.');
+ return;
+ }
+
+ delete funcToCallback[funcName];
+ callback.apply(null, params);
}
-
- delete funcToCallback[funcName];
- callback.apply(null, params);
+ } else {
+ common.logMessage('Error: Unknow message `' + data +
+ '` received from NaCl module.');
}
}
« no previous file with comments | « no previous file | native_client_sdk/src/examples/demo/nacl_io/index.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698