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 var table = $('devices-table'); |
24 | |
25 var params = []; | |
26 if (info) { | |
27 params[0] = info.domain; | |
28 params[1] = info.port; | |
29 params[2] = info.ip; | |
30 params[3] = info.lastSeen; | |
31 } | |
32 | |
33 for (var i = 0, row; row = table.rows[i]; i++) { | |
34 if (row.cells[0].textContent == name) { | |
Dan Beam
2013/08/07 18:43:03
^ this can be buggy, btw, as
(blah.textContent
Noam Samuel
2013/08/08 18:45:36
For now added line to enforce lack of '\r' and '\n
| |
35 if (!info) { | |
36 // Delete service from the row. | |
37 table.removeChild(row); | |
38 } else { | |
39 // Replace existing service. | |
40 for (var j = 0; j < params.length; j++) { | |
41 row.cells[j + 1].textContent = params[j]; | |
42 } | |
43 } | |
44 return; | |
45 } | |
46 } | |
47 | |
48 if (!info) { | |
49 // Service could not be found in the table. | |
50 return; | |
51 } | |
52 | |
53 var tr = document.createElement('tr'); | |
54 var td = document.createElement('td'); | |
55 td.textContent = name; | |
56 tr.appendChild(td); | |
57 | |
58 for (var j = 0; j < params.length; j++) { | |
59 td = document.createElement('td'); | |
60 td.textContent = params[j]; | |
61 tr.appendChild(td); | |
62 } | |
63 | |
64 td = document.createElement('td'); | |
65 if (!info.registered) { | |
66 var button = document.createElement('button'); | |
67 button.textContent = loadTimeData.getString('serviceRegister'); | |
68 button.addEventListener('click', sendRegisterDevice.bind(null, name)); | |
69 td.appendChild(button); | |
70 } else { | |
71 td.textContent = loadTimeData.getString('registered'); | |
72 } | |
73 tr.appendChild(td); | |
74 | |
75 table.appendChild(tr); | |
30 } | 76 } |
Dan Beam
2013/08/07 18:43:03
^ do you have any tests for this code?
Noam Samuel
2013/08/08 18:45:36
Note that this function is mostly rewritten in a D
| |
31 | 77 |
32 for (var i = 0, row; row = table.rows[i]; i++) { | 78 /** |
33 if (row.cells[0].textContent == name) { | 79 * Adds a row to the logging console. |
34 if (!info) { | 80 * @param {string} msg The message to log. |
35 // Delete service from the row. | 81 */ |
36 table.removeChild(row); | 82 function logToInfoConsole(msg) { |
37 } else { | 83 var div = document.createElement('div'); |
38 // Replace existing service. | 84 div.textContent = msg; |
39 for (var j = 0; j < params.length; j++) { | 85 $('info-console').appendChild(div); |
40 row.cells[j + 1].textContent = params[j]; | |
41 } | |
42 } | |
43 return; | |
44 } | |
45 } | 86 } |
46 | 87 |
47 if (!info) { | 88 /** |
48 // Service could not be found in the table. | 89 * Register a device. |
49 return; | 90 * @param {string} device The device to register. |
91 */ | |
92 function sendRegisterDevice(device) { | |
93 chrome.send('registerDevice', [device]); | |
94 logToInfoConsole(loadTimeData.getStringF('registeringService', device)); | |
50 } | 95 } |
51 | 96 |
52 var tr = document.createElement('tr'); | 97 /** |
53 var td = document.createElement('td'); | 98 * Announce that a registration failed. |
54 td.textContent = name; | 99 * @param {string} reason The error message. |
55 tr.appendChild(td); | 100 */ |
56 | 101 function registrationFailed(reason) { |
57 for (var j = 0; j < params.length; j++) { | 102 logToInfoConsole(loadTimeData.getStringF('registrationFailed', reason)); |
58 td = document.createElement('td'); | |
59 td.textContent = params[j]; | |
60 tr.appendChild(td); | |
61 } | 103 } |
62 | 104 |
63 table.appendChild(tr); | 105 /** |
64 } | 106 * Announce that a registration succeeeded. |
107 * @param {string} id The id of the newly registered device. | |
108 */ | |
109 function registrationSuccess(id) { | |
110 logToInfoConsole(loadTimeData.getStringF('registrationSucceeded', id)); | |
111 } | |
112 | |
113 document.addEventListener('DOMContentLoaded', function() { | |
114 chrome.send('start'); | |
115 }); | |
116 | |
117 return { | |
118 registrationSuccess: registrationSuccess, | |
119 registrationFailed: registrationFailed, | |
120 onServiceUpdate: onServiceUpdate | |
121 }; | |
122 }); | |
OLD | NEW |