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.
6function 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.
14function 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
20var msgTcpCreate = 't;'
21var msgUdpCreate = 'u;'
22var msgSend = 's;'
23var msgClose = 'c;'
24var msgListen = 'l;'
25
26function 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
39function 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
48function 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
56function doClose() {
57  // Send a request message. See also socket.cc for the request format.
58  common.naclModule.postMessage(msgClose);
59}
60
61function handleMessage(message) {
62  common.logMessage(message.data);
63}
64