OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 /** | 5 /** |
6 * Javascript for local_discovery.html, served from chrome://devices/ | 6 * Javascript for local_discovery.html, served from chrome://devices/ |
7 * This is used to show discoverable devices near the user. | 7 * This is used to show discoverable devices near the user. |
8 * | 8 * |
9 * The simple object defined in this javascript file listens for | 9 * The simple object defined in this javascript file listens for |
10 * callbacks from the C++ code saying that a new device is available. | 10 * callbacks from the C++ code saying that a new device is available. |
11 */ | 11 */ |
12 | 12 |
13 /** | 13 cr.define('local_discovery', function() { |
14 * Appends a row to the output table listing the new device. | 14 'use strict'; |
15 * @param {string} name of the device service. | |
16 * @param {string} info - additional info of the device, | |
17 * if empty device need to be deleted | |
18 */ | |
19 function onServiceUpdate(name, info) { | |
20 var table = $('devices-table'); | |
21 | 15 |
22 var params = []; | 16 /** |
23 if (info) { | 17 * Appends a row to the output table listing the new device. |
24 params[0] = info.domain; | 18 * @param {string} name Name of the device. |
25 params[1] = info.port; | 19 * @param {string} info Additional info of the device, if empty device need to |
26 params[2] = info.ip; | 20 * be deleted. |
27 params[3] = info.metadata; | 21 */ |
28 params[4] = info.lastSeen; | 22 function onServiceUpdate(name, info) { |
29 params[5] = info.registered; | 23 name = name.replace('\r', '').replace('\n', ''); |
Dan Beam
2013/08/14 20:43:02
this doesn't replace all encountered \r or \n :(
Noam Samuel
2013/08/14 21:21:56
Done.
| |
24 var table = $('devices-table'); | |
25 | |
26 var params = []; | |
27 if (info) { | |
28 params[0] = info.domain; | |
29 params[1] = info.port; | |
30 params[2] = info.ip; | |
31 params[3] = info.lastSeen; | |
32 } | |
33 | |
34 for (var i = 0, row; row = table.rows[i]; i++) { | |
35 if (row.cells[0].textContent == name) { | |
36 if (!info) { | |
37 // Delete service from the row. | |
38 table.removeChild(row); | |
39 } else { | |
40 // Replace existing service. | |
41 for (var j = 0; j < params.length; j++) { | |
42 row.cells[j + 1].textContent = params[j]; | |
43 } | |
44 } | |
45 return; | |
46 } | |
47 } | |
48 | |
49 if (!info) { | |
50 // Service could not be found in the table. | |
51 return; | |
52 } | |
53 | |
54 var tr = document.createElement('tr'); | |
55 var td = document.createElement('td'); | |
56 td.textContent = name; | |
57 tr.appendChild(td); | |
58 | |
59 for (var j = 0; j < params.length; j++) { | |
60 td = document.createElement('td'); | |
61 td.textContent = params[j]; | |
62 tr.appendChild(td); | |
63 } | |
64 | |
65 td = document.createElement('td'); | |
66 if (!info.registered) { | |
67 var button = document.createElement('button'); | |
68 button.textContent = loadTimeData.getString('serviceRegister'); | |
69 button.addEventListener('click', sendRegisterDevice.bind(null, name)); | |
70 td.appendChild(button); | |
71 } else { | |
72 td.textContent = loadTimeData.getString('registered'); | |
73 } | |
74 tr.appendChild(td); | |
75 | |
76 table.appendChild(tr); | |
30 } | 77 } |
31 | 78 |
32 for (var i = 0, row; row = table.rows[i]; i++) { | 79 /** |
33 if (row.cells[0].textContent == name) { | 80 * Adds a row to the logging console. |
34 if (!info) { | 81 * @param {string} msg The message to log. |
35 // Delete service from the row. | 82 */ |
36 table.removeChild(row); | 83 function logToInfoConsole(msg) { |
37 } else { | 84 var div = document.createElement('div'); |
38 // Replace existing service. | 85 div.textContent = msg; |
39 for (var j = 0; j < params.length; j++) { | 86 $('info-console').appendChild(div); |
40 row.cells[j + 1].textContent = params[j]; | |
41 } | |
42 } | |
43 return; | |
44 } | |
45 } | 87 } |
46 | 88 |
47 if (!info) { | 89 /** |
48 // Service could not be found in the table. | 90 * Register a device. |
49 return; | 91 * @param {string} device The device to register. |
92 */ | |
93 function sendRegisterDevice(device) { | |
94 chrome.send('registerDevice', [device]); | |
95 logToInfoConsole(loadTimeData.getStringF('registeringService', device)); | |
50 } | 96 } |
51 | 97 |
52 var tr = document.createElement('tr'); | 98 /** |
53 var td = document.createElement('td'); | 99 * Announce that a registration failed. |
54 td.textContent = name; | 100 * @param {string} reason The error message. |
55 tr.appendChild(td); | 101 */ |
56 | 102 function registrationFailed(reason) { |
57 for (var j = 0; j < params.length; j++) { | 103 logToInfoConsole(loadTimeData.getStringF('registrationFailed', reason)); |
58 td = document.createElement('td'); | |
59 td.textContent = params[j]; | |
60 tr.appendChild(td); | |
61 } | 104 } |
62 | 105 |
63 table.appendChild(tr); | 106 /** |
64 } | 107 * Announce that a registration succeeeded. |
108 * @param {string} id The id of the newly registered device. | |
109 */ | |
110 function registrationSuccess(id) { | |
111 logToInfoConsole(loadTimeData.getStringF('registrationSucceeded', id)); | |
112 } | |
113 | |
114 document.addEventListener('DOMContentLoaded', function() { | |
115 chrome.send('start'); | |
116 }); | |
117 | |
118 return { | |
119 registrationSuccess: registrationSuccess, | |
120 registrationFailed: registrationFailed, | |
121 onServiceUpdate: onServiceUpdate | |
122 }; | |
123 }); | |
OLD | NEW |