OLD | NEW |
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 Loading... |
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 $('pipe_name').addEventListener('change', |
| 48 function() { $('pipe_output').value = ''; }) |
| 49 } |
| 50 |
| 51 // Called with keypress events on the pipe input box |
| 52 function onPipeInput(e) { |
| 53 // Create an arraybuffer containing the 16-bit char code |
| 54 // from the keypress event. |
| 55 var buffer = new ArrayBuffer(1*2); |
| 56 var bufferView = new Uint16Array(buffer); |
| 57 bufferView[0] = e.charCode; |
| 58 |
| 59 // Pass the buffer in a dictionary over the NaCl module |
| 60 var pipeSelect = $('pipe_name'); |
| 61 var pipeName = pipeSelect[pipeSelect.selectedIndex].value; |
| 62 var message = { |
| 63 pipe: pipeName, |
| 64 operation: 'write', |
| 65 payload: buffer, |
| 66 }; |
| 67 nacl_module.postMessage(message); |
| 68 e.preventDefault(); |
| 69 return false; |
43 } | 70 } |
44 | 71 |
45 function onRadioClicked(e) { | 72 function onRadioClicked(e) { |
46 var divId = this.id.slice(5); // skip "radio" | 73 var divId = this.id.slice(5); // skip "radio" |
47 var functionEls = document.querySelectorAll('.function'); | 74 var functionEls = document.querySelectorAll('.function'); |
48 for (var i = 0; i < functionEls.length; ++i) { | 75 for (var i = 0; i < functionEls.length; ++i) { |
49 var visible = functionEls[i].id === divId; | 76 var visible = functionEls[i].id === divId; |
50 if (functionEls[i].id === divId) | 77 if (functionEls[i].id === divId) |
51 functionEls[i].removeAttribute('hidden'); | 78 functionEls[i].removeAttribute('hidden'); |
52 else | 79 else |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 function postCall(func) { | 299 function postCall(func) { |
273 var callback = arguments[arguments.length - 1]; | 300 var callback = arguments[arguments.length - 1]; |
274 funcToCallback[func] = callback; | 301 funcToCallback[func] = callback; |
275 | 302 |
276 nacl_module.postMessage({ | 303 nacl_module.postMessage({ |
277 cmd: func, | 304 cmd: func, |
278 args: Array.prototype.slice.call(arguments, 1, -1) | 305 args: Array.prototype.slice.call(arguments, 1, -1) |
279 }); | 306 }); |
280 } | 307 } |
281 | 308 |
| 309 function ArrayBufferToString(buf) { |
| 310 return String.fromCharCode.apply(null, new Uint16Array(buf)); |
| 311 } |
| 312 |
282 // Called by the common.js module. | 313 // Called by the common.js module. |
283 function handleMessage(message_event) { | 314 function handleMessage(message_event) { |
284 var data = message_event.data; | 315 var data = message_event.data; |
285 if ((typeof(data) === 'string' || data instanceof String)) { | 316 if ((typeof(data) === 'string' || data instanceof String)) { |
286 common.logMessage(data); | 317 common.logMessage(data); |
| 318 } else if (data instanceof Object) { |
| 319 var pipeName = data['pipe'] |
| 320 if (pipeName !== undefined) { |
| 321 // Message for JavaScript I/O pipe |
| 322 var operation = data['operation']; |
| 323 if (operation == 'write') { |
| 324 $('pipe_output').value += ArrayBufferToString(data['payload']); |
| 325 } else if (operation == 'ack') { |
| 326 common.logMessage(pipeName + ": ack:" + data['payload']); |
| 327 } else { |
| 328 common.logMessage('Got unexpected pipe operation: ' + operation); |
| 329 } |
| 330 } else { |
| 331 // Result from a function call. |
| 332 var params = data.args; |
| 333 var funcName = data.cmd; |
| 334 var callback = funcToCallback[funcName]; |
| 335 |
| 336 if (!callback) { |
| 337 common.logMessage('Error: Bad message ' + funcName + |
| 338 ' received from NaCl module.'); |
| 339 return; |
| 340 } |
| 341 |
| 342 delete funcToCallback[funcName]; |
| 343 callback.apply(null, params); |
| 344 } |
287 } else { | 345 } else { |
288 // Result from a function call. | 346 common.logMessage('Error: Unknow message `' + data + |
289 var params = data.args; | 347 '` 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 } | 348 } |
302 } | 349 } |
OLD | NEW |