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

Side by Side 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: rebase 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 function moduleDidLoad() { 5 function moduleDidLoad() {
6 common.hideModule(); 6 common.hideModule();
7 } 7 }
8 8
9 function $(id) { 9 function $(id) {
10 return document.getElementById(id); 10 return document.getElementById(id);
(...skipping 22 matching lines...) Expand all
33 var functionEls = document.querySelectorAll('.function'); 33 var functionEls = document.querySelectorAll('.function');
34 for (var i = 0; i < functionEls.length; ++i) { 34 for (var i = 0; i < functionEls.length; ++i) {
35 var functionEl = functionEls[i]; 35 var functionEl = functionEls[i];
36 var id = functionEl.getAttribute('id'); 36 var id = functionEl.getAttribute('id');
37 var buttonEl = functionEl.querySelector('button'); 37 var buttonEl = functionEl.querySelector('button');
38 38
39 // The function name matches the element id. 39 // The function name matches the element id.
40 var func = window[id]; 40 var func = window[id];
41 buttonEl.addEventListener('click', func); 41 buttonEl.addEventListener('click', func);
42 } 42 }
43
44 $('pipe_input_box').addEventListener('keypress', onPipeInput)
45 $('pipe_output').disabled = true;
46
47 var onChange = function(e) {
binji 2014/05/01 20:22:31 this function is small enough to inline, IMO
Sam Clegg 2014/05/01 22:16:55 Done.
48 $('pipe_output').value = '';
49 }
50 $('pipe_name').addEventListener('change', onChange);
51 }
52
53 // Called with keypress events on the pipe input box
54 function onPipeInput(e) {
55 // Create an arraybuffer containing the 16-bit char code
56 // from the keypress event.
57 var buffer = new ArrayBuffer(1*2);
58 var bufferView = new Uint16Array(buffer);
59 bufferView[0] = e.charCode;
60
61 // Pass the buffer in a dictionary over the NaCl module
62 var pipeSelect = $('pipe_name');
63 var pipeName = pipeSelect[pipeSelect.selectedIndex].value;
64 var message = {};
65 message[pipeName] = ['write', buffer];
66 nacl_module.postMessage(message);
67 e.preventDefault();
68 return false;
43 } 69 }
44 70
45 function onRadioClicked(e) { 71 function onRadioClicked(e) {
46 var divId = this.id.slice(5); // skip "radio" 72 var divId = this.id.slice(5); // skip "radio"
47 var functionEls = document.querySelectorAll('.function'); 73 var functionEls = document.querySelectorAll('.function');
48 for (var i = 0; i < functionEls.length; ++i) { 74 for (var i = 0; i < functionEls.length; ++i) {
49 var visible = functionEls[i].id === divId; 75 var visible = functionEls[i].id === divId;
50 if (functionEls[i].id === divId) 76 if (functionEls[i].id === divId)
51 functionEls[i].removeAttribute('hidden'); 77 functionEls[i].removeAttribute('hidden');
52 else 78 else
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 function postCall(func) { 298 function postCall(func) {
273 var callback = arguments[arguments.length - 1]; 299 var callback = arguments[arguments.length - 1];
274 funcToCallback[func] = callback; 300 funcToCallback[func] = callback;
275 301
276 nacl_module.postMessage({ 302 nacl_module.postMessage({
277 cmd: func, 303 cmd: func,
278 args: Array.prototype.slice.call(arguments, 1, -1) 304 args: Array.prototype.slice.call(arguments, 1, -1)
279 }); 305 });
280 } 306 }
281 307
308 function ArrayBufferToString(buf) {
309 return String.fromCharCode.apply(null, new Uint16Array(buf));
binji 2014/05/01 20:22:31 probably not an issue here, but this fails for lar
310 }
311
282 // Called by the common.js module. 312 // Called by the common.js module.
283 function handleMessage(message_event) { 313 function handleMessage(message_event) {
284 var data = message_event.data; 314 var data = message_event.data;
285 if ((typeof(data) === 'string' || data instanceof String)) { 315 if ((typeof(data) === 'string' || data instanceof String)) {
286 common.logMessage(data); 316 common.logMessage(data);
317 } else if (data instanceof Object) {
318 var channelName = Object.keys(data)[0]
binji 2014/05/01 20:22:31 This seems clunky to me. It would be nicer to have
Sam Clegg 2014/05/01 22:16:55 I guess it will make the code slightly large and t
319 if (channelName == 'jspipe1' ||
binji 2014/05/01 20:22:31 nit: === here and everywhere
Sam Clegg 2014/05/01 22:16:55 Done.
320 channelName == 'jspipe2' ||
321 channelName == 'jspipe3') {
322 // Message for JavaScript I/O pipe
323 var command = data[channelName];
324 if (command[0] == 'write') {
325 $('pipe_output').value += ArrayBufferToString(command[1]);
326 } else if (command[0] == 'ack') {
327 common.logMessage(channelName + ": ack:" + command[1]);
328 } else {
329 common.logMessage('Got unexpected command: ' + command);
330 }
331 } else {
332 // Result from a function call.
333 var params = data.args;
334 var funcName = data.cmd;
335 var callback = funcToCallback[funcName];
336
337 if (!callback) {
338 common.logMessage('Error: Bad message ' + funcName +
339 ' received from NaCl module.');
340 return;
341 }
342
343 delete funcToCallback[funcName];
344 callback.apply(null, params);
345 }
287 } else { 346 } else {
288 // Result from a function call. 347 common.logMessage('Error: Unknow message `' + data +
289 var params = data.args; 348 '` received from NaCl module.');
290 var funcName = data.cmd;
291 var callback = funcToCallback[funcName];
292
293 if (!callback) {
294 common.logMessage('Error: Bad message ' + funcName +
295 ' received from NaCl module.');
296 return;
297 }
298
299 delete funcToCallback[funcName];
300 callback.apply(null, params);
301 } 349 }
302 } 350 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698