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