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 /** |
14 * Appends a row to the output table listing the new device. | 14 * Appends a row to the output table listing the new device. |
15 * @param {string} name of the device service. | 15 * @param {string} name of the device service. |
16 * @param {string} info - additional info of the device, | 16 * @param {string} info - additional info of the device, |
17 * if empty device need to be deleted | 17 * if empty device need to be deleted. |
18 */ | 18 */ |
19 function onServiceUpdate(name, info) { | 19 function onServiceUpdate(name, info) { |
20 var table = $('devices-table'); | 20 var table = $('devices-table'); |
21 | 21 |
22 var params = []; | 22 var params = []; |
23 if (info) { | 23 if (info) { |
24 params[0] = info.domain; | 24 params[0] = info.domain; |
25 params[1] = info.port; | 25 params[1] = info.port; |
26 params[2] = info.ip; | 26 params[2] = info.ip; |
27 params[3] = info.metadata; | 27 params[3] = info.metadata; |
28 params[4] = info.lastSeen; | 28 params[4] = info.lastSeen; |
29 params[5] = info.registered; | |
30 } | 29 } |
31 | 30 |
32 for (var i = 0, row; row = table.rows[i]; i++) { | 31 for (var i = 0, row; row = table.rows[i]; i++) { |
33 if (row.cells[0].textContent == name) { | 32 if (row.cells[0].textContent == name) { |
34 if (!info) { | 33 if (!info) { |
35 // Delete service from the row. | 34 // Delete service from the row. |
36 table.removeChild(row); | 35 table.removeChild(row); |
37 } else { | 36 } else { |
38 // Replace existing service. | 37 // Replace existing service. |
39 for (var j = 0; j < params.length; j++) { | 38 for (var j = 0; j < params.length; j++) { |
(...skipping 13 matching lines...) Expand all Loading... |
53 var td = document.createElement('td'); | 52 var td = document.createElement('td'); |
54 td.textContent = name; | 53 td.textContent = name; |
55 tr.appendChild(td); | 54 tr.appendChild(td); |
56 | 55 |
57 for (var j = 0; j < params.length; j++) { | 56 for (var j = 0; j < params.length; j++) { |
58 td = document.createElement('td'); | 57 td = document.createElement('td'); |
59 td.textContent = params[j]; | 58 td.textContent = params[j]; |
60 tr.appendChild(td); | 59 tr.appendChild(td); |
61 } | 60 } |
62 | 61 |
| 62 td = document.createElement('td'); |
| 63 if (!info.registered) { |
| 64 var button = document.createElement('button'); |
| 65 button.textContent = 'Register'; |
| 66 button.addEventListener('click', sendRegisterDevice.bind(null, name)); |
| 67 td.appendChild(button); |
| 68 } else { |
| 69 td.textContent = 'Registered'; |
| 70 } |
| 71 tr.appendChild(td); |
| 72 |
63 table.appendChild(tr); | 73 table.appendChild(tr); |
64 } | 74 } |
| 75 |
| 76 /** |
| 77 * Adds a row to the logging console. |
| 78 * @param {string} msg The message to log. |
| 79 */ |
| 80 function logToInfoConsole(msg) { |
| 81 var div = document.createElement('div'); |
| 82 div.textContent = msg; |
| 83 $('infoConsole').appendChild(div); |
| 84 } |
| 85 |
| 86 /** |
| 87 * Register a device. |
| 88 * @param {string} device The device to register. |
| 89 */ |
| 90 function sendRegisterDevice(device) { |
| 91 chrome.send('register', [device]); |
| 92 logToInfoConsole('Registering ' + device + '...'); |
| 93 } |
| 94 |
| 95 /** |
| 96 * Announce that a registration failed. |
| 97 * @param {string} reason The error message. |
| 98 */ |
| 99 function registrationFailed(reason) { |
| 100 logToInfoConsole('Registration failed: ' + reason); |
| 101 } |
| 102 |
| 103 /** |
| 104 * Announce that a registration succeeeded. |
| 105 * @param {string} id The id of the newly registered device. |
| 106 */ |
| 107 function registrationSuccess(id) { |
| 108 logToInfoConsole('Registered as ' + id); |
| 109 } |
| 110 |
| 111 document.addEventListener('DOMContentLoaded', function() { |
| 112 chrome.send('start'); |
| 113 }); |
OLD | NEW |