OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 cr.define('gcmInternals', function() { | 5 cr.define('gcmInternals', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
| 8 var isRecording = false; |
| 9 |
8 /** | 10 /** |
9 * If the info dictionary has property prop, then set the text content of | 11 * If the info dictionary has property prop, then set the text content of |
10 * element to the value of this property. | 12 * element to the value of this property. Otherwise clear the content. |
11 * @param {!Object} info A dictionary of device infos to be displayed. | 13 * @param {!Object} info A dictionary of device infos to be displayed. |
12 * @param {string} prop Name of the property. | 14 * @param {string} prop Name of the property. |
13 * @param {string} element The id of a HTML element. | 15 * @param {string} element The id of a HTML element. |
14 */ | 16 */ |
15 function setIfExists(info, prop, element) { | 17 function setIfExists(info, prop, element) { |
16 if (info[prop] !== undefined) { | 18 if (info[prop] !== undefined) { |
17 $(element).textContent = info[prop]; | 19 $(element).textContent = info[prop]; |
| 20 } else { |
| 21 $(element).textContent = ''; |
18 } | 22 } |
19 } | 23 } |
20 | 24 |
21 /** | 25 /** |
22 * Display device informations. | 26 * Display device informations. |
23 * @param {!Object} info A dictionary of device infos to be displayed. | 27 * @param {!Object} info A dictionary of device infos to be displayed. |
24 */ | 28 */ |
25 function displayDeviceInfo(info) { | 29 function displayDeviceInfo(info) { |
26 setIfExists(info, 'androidId', 'android-id'); | 30 setIfExists(info, 'androidId', 'android-id'); |
27 setIfExists(info, 'profileServiceCreated', 'profile-service-created'); | 31 setIfExists(info, 'profileServiceCreated', 'profile-service-created'); |
28 setIfExists(info, 'gcmEnabledState', 'gcm-enabled-state'); | 32 setIfExists(info, 'gcmEnabledState', 'gcm-enabled-state'); |
29 setIfExists(info, 'signedInUserName', 'signed-in-username'); | 33 setIfExists(info, 'signedInUserName', 'signed-in-username'); |
30 setIfExists(info, 'gcmClientCreated', 'gcm-client-created'); | 34 setIfExists(info, 'gcmClientCreated', 'gcm-client-created'); |
31 setIfExists(info, 'gcmClientState', 'gcm-client-state'); | 35 setIfExists(info, 'gcmClientState', 'gcm-client-state'); |
32 setIfExists(info, 'gcmClientReady', 'gcm-client-ready'); | 36 setIfExists(info, 'gcmClientReady', 'gcm-client-ready'); |
33 setIfExists(info, 'connectionClientCreated', 'connection-client-created'); | 37 setIfExists(info, 'connectionClientCreated', 'connection-client-created'); |
34 setIfExists(info, 'connectionState', 'connection-state'); | 38 setIfExists(info, 'connectionState', 'connection-state'); |
| 39 setIfExists(info, 'registeredAppIds', 'registered-app-ids'); |
| 40 setIfExists(info, 'sendQueueSize', 'send-queue-size'); |
| 41 setIfExists(info, 'resendQueueSize', 'resend-queue-size'); |
| 42 } |
| 43 |
| 44 /** |
| 45 * Remove all the child nodes of the element. |
| 46 * @param {HTMLElement} element A HTML element. |
| 47 */ |
| 48 function removeAllChildNodes(element) { |
| 49 element.textContent = ''; |
| 50 } |
| 51 |
| 52 /** |
| 53 * For each item in line, add a row to the table. Each item is actually a list |
| 54 * of sub-items; each of which will have a corresponding cell created in that |
| 55 * row, and the sub-item will be displayed in the cell. |
| 56 * @param {HTMLElement} table A HTML tbody element. |
| 57 * @param {!Object} list A list of list of item. |
| 58 */ |
| 59 function addRows(table, list) { |
| 60 for (var i = 0; i < list.length; ++i) { |
| 61 var row = document.createElement('tr'); |
| 62 |
| 63 // The first element is always a timestamp. |
| 64 var cell = document.createElement('td'); |
| 65 var d = new Date(list[i][0]); |
| 66 cell.textContent = d; |
| 67 row.appendChild(cell); |
| 68 |
| 69 for (var j = 1; j < list[i].length; ++j) { |
| 70 var cell = document.createElement('td'); |
| 71 cell.textContent = list[i][j]; |
| 72 row.appendChild(cell); |
| 73 } |
| 74 table.appendChild(row); |
| 75 } |
| 76 } |
| 77 |
| 78 /** |
| 79 * Refresh all displayed information. |
| 80 */ |
| 81 function refreshAll() { |
| 82 chrome.send('getGcmInternalsInfo', [false]); |
| 83 } |
| 84 |
| 85 /** |
| 86 * Toggle the isRecording variable and send it to browser. |
| 87 */ |
| 88 function setRecording() { |
| 89 isRecording = !isRecording; |
| 90 chrome.send('setGcmInternalsRecording', [isRecording]); |
| 91 } |
| 92 |
| 93 /** |
| 94 * Clear all the activity logs. |
| 95 */ |
| 96 function clearLogs() { |
| 97 chrome.send('getGcmInternalsInfo', [true]); |
35 } | 98 } |
36 | 99 |
37 function initialize() { | 100 function initialize() { |
38 chrome.send('getGcmInternalsInfo'); | 101 $('recording').disabled = true; |
| 102 $('refresh').onclick = refreshAll; |
| 103 $('recording').onclick = setRecording; |
| 104 $('clear-logs').onclick = clearLogs; |
| 105 chrome.send('getGcmInternalsInfo', [false]); |
39 } | 106 } |
40 | 107 |
41 /** | 108 /** |
42 * Callback function accepting a dictionary of info items to be displayed. | 109 * Callback function accepting a dictionary of info items to be displayed. |
43 * @param {!Object} infos A dictionary of info items to be displayed. | 110 * @param {!Object} infos A dictionary of info items to be displayed. |
44 */ | 111 */ |
45 function setGcmInternalsInfo(infos) { | 112 function setGcmInternalsInfo(infos) { |
| 113 isRecording = infos.isRecording; |
| 114 if (isRecording) |
| 115 $('recording').textContent = 'Stop Recording'; |
| 116 else |
| 117 $('recording').textContent = 'Start Recording'; |
| 118 $('recording').disabled = false; |
46 if (infos.deviceInfo !== undefined) { | 119 if (infos.deviceInfo !== undefined) { |
47 displayDeviceInfo(infos.deviceInfo); | 120 displayDeviceInfo(infos.deviceInfo); |
48 } | 121 } |
| 122 |
| 123 removeAllChildNodes($('send-info')); |
| 124 if (infos.sendInfo !== undefined) { |
| 125 addRows($('send-info'), infos.sendInfo); |
| 126 } |
49 } | 127 } |
50 | 128 |
51 // Return an object with all of the exports. | 129 // Return an object with all of the exports. |
52 return { | 130 return { |
53 initialize: initialize, | 131 initialize: initialize, |
54 setGcmInternalsInfo: setGcmInternalsInfo, | 132 setGcmInternalsInfo: setGcmInternalsInfo, |
55 }; | 133 }; |
56 }); | 134 }); |
57 | 135 |
58 document.addEventListener('DOMContentLoaded', gcmInternals.initialize); | 136 document.addEventListener('DOMContentLoaded', gcmInternals.initialize); |
OLD | NEW |