1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Called by the common.js module. 6 function attachListeners() { 7 document.getElementById('connectForm').addEventListener('submit', doConnect); 8 document.getElementById('sendForm').addEventListener('submit', doSend); 9 document.getElementById('listenForm').addEventListener('submit', doListen); 10 document.getElementById('closeButton').addEventListener('click', doClose); 11 } 12 13 // Called by the common.js module. 14 function moduleDidLoad() { 15 // The module is not hidden by default so we can easily see if the plugin 16 // failed to load. 17 common.hideModule(); 18 } 19 20 var msgTcpCreate = 't;' 21 var msgUdpCreate = 'u;' 22 var msgSend = 's;' 23 var msgClose = 'c;' 24 var msgListen = 'l;' 25 26 function doConnect(event) { 27 // Send a request message. See also socket.cc for the request format. 28 event.preventDefault(); 29 var hostname = document.getElementById('hostname').value; 30 var type = document.getElementById('connect_type').value; 31 common.logMessage(type); 32 if (type == 'tcp') { 33 common.naclModule.postMessage(msgTcpCreate + hostname); 34 } else { 35 common.naclModule.postMessage(msgUdpCreate + hostname); 36 } 37 } 38 39 function doSend(event) { 40 // Send a request message. See also socket.cc for the request format. 41 event.preventDefault(); 42 var message = document.getElementById('message').value; 43 while (message.indexOf('\\n') > -1) 44 message = message.replace('\\n', '\n'); 45 common.naclModule.postMessage(msgSend + message); 46 } 47 48 function doListen(event) { 49 // Listen a the given port. 50 event.preventDefault(); 51 var port = document.getElementById('port').value; 52 var type = document.getElementById('listen_type').value; 53 common.naclModule.postMessage(msgListen + port); 54 } 55 56 function doClose() { 57 // Send a request message. See also socket.cc for the request format. 58 common.naclModule.postMessage(msgClose); 59 } 60 61 function handleMessage(message) { 62 common.logMessage(message.data); 63 } 64