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. |
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]; |
18 } | 20 } |
19 } | 21 } |
20 | 22 |
21 /** | 23 /** |
22 * Display device informations. | 24 * Display device informations. |
23 * @param {!Object} info A dictionary of device infos to be displayed. | 25 * @param {!Object} info A dictionary of device infos to be displayed. |
24 */ | 26 */ |
25 function displayDeviceInfo(info) { | 27 function displayDeviceInfo(info) { |
26 setIfExists(info, 'androidId', 'android-id'); | 28 setIfExists(info, 'androidId', 'android-id'); |
27 setIfExists(info, 'profileServiceCreated', 'profile-service-created'); | 29 setIfExists(info, 'profileServiceCreated', 'profile-service-created'); |
28 setIfExists(info, 'gcmEnabledState', 'gcm-enabled-state'); | 30 setIfExists(info, 'gcmEnabledState', 'gcm-enabled-state'); |
29 setIfExists(info, 'signedInUserName', 'signed-in-username'); | 31 setIfExists(info, 'signedInUserName', 'signed-in-username'); |
30 setIfExists(info, 'gcmClientCreated', 'gcm-client-created'); | 32 setIfExists(info, 'gcmClientCreated', 'gcm-client-created'); |
31 setIfExists(info, 'gcmClientState', 'gcm-client-state'); | 33 setIfExists(info, 'gcmClientState', 'gcm-client-state'); |
32 setIfExists(info, 'gcmClientReady', 'gcm-client-ready'); | 34 setIfExists(info, 'gcmClientReady', 'gcm-client-ready'); |
33 setIfExists(info, 'connectionClientCreated', 'connection-client-created'); | 35 setIfExists(info, 'connectionClientCreated', 'connection-client-created'); |
34 setIfExists(info, 'connectionState', 'connection-state'); | 36 setIfExists(info, 'connectionState', 'connection-state'); |
37 setIfExists(info, 'appIdsCached', 'app-ids-cached'); | |
38 setIfExists(info, 'sendQueueSize', 'send-queue-size'); | |
39 setIfExists(info, 'unackedQueueSize', 'unacked-queue-size'); | |
40 } | |
41 | |
42 /** | |
43 * Remove all the child nodes of the element. | |
44 * @param {HTMLElement} ele A HTML element. | |
fgorski
2014/03/18 21:28:37
rename ele to element.
juyik
2014/03/20 01:09:53
Done.
| |
45 */ | |
46 function removeAllChildNodes(ele) { | |
47 while (ele.firstChild) { | |
48 ele.removeChild(ele.firstChild); | |
49 } | |
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 for (var j = 0; j < list[i].length; ++j) { | |
fgorski
2014/03/18 21:28:37
Why did we choose to have a list here instead of a
juyik
2014/03/20 01:09:53
Do you mean a dictionary kind of object? I thought
| |
63 var cell = document.createElement('td'); | |
64 | |
65 if (j == 0) { | |
fgorski
2014/03/18 21:28:37
in that case you should probably have a simple loo
juyik
2014/03/20 01:09:53
Done.
| |
66 // The first element is always a timestamp. | |
67 var d = new Date(list[i][j]); | |
68 cell.textContent = d.toString(); | |
69 } else | |
jianli
2014/03/18 23:53:23
nit: add brackets for else to be symmetric
juyik
2014/03/20 01:09:53
N/A anymore after I refactored the code.
| |
70 cell.textContent = list[i][j]; | |
71 row.appendChild(cell); | |
72 } | |
73 if ((i % 2) == 0) | |
74 row.className = 'odd-number-row'; | |
75 table.appendChild(row); | |
76 } | |
77 } | |
78 | |
79 /** | |
80 * Refresh all displayed information. | |
81 */ | |
82 function refreshAll() { | |
83 chrome.send('getGcmInternalsInfo', [false]); | |
84 } | |
85 | |
86 /** | |
87 * Toggle the isRecording variable and send it to browser. | |
88 */ | |
89 function setRecording() { | |
90 isRecording = !isRecording; | |
91 chrome.send('setGcmInternalsRecording', [isRecording]); | |
92 } | |
93 | |
94 /** | |
95 * Clear all the activity logs. | |
96 */ | |
97 function clearLogs() { | |
98 chrome.send('getGcmInternalsInfo', [true]); | |
35 } | 99 } |
36 | 100 |
37 function initialize() { | 101 function initialize() { |
38 chrome.send('getGcmInternalsInfo'); | 102 $('recording').disabled = true; |
103 $('refresh').onclick = refreshAll; | |
104 $('recording').onclick = setRecording; | |
105 $('clear-logs').onclick = clearLogs; | |
106 chrome.send('getGcmInternalsInfo', [false]); | |
39 } | 107 } |
40 | 108 |
41 /** | 109 /** |
42 * Callback function accepting a dictionary of info items to be displayed. | 110 * Callback function accepting a dictionary of info items to be displayed. |
43 * @param {!Object} infos A dictionary of info items to be displayed. | 111 * @param {!Object} infos A dictionary of info items to be displayed. |
44 */ | 112 */ |
45 function setGcmInternalsInfo(infos) { | 113 function setGcmInternalsInfo(infos) { |
114 isRecording = infos.isRecording; | |
115 if (isRecording) | |
116 $('recording').textContent = 'Stop Recording'; | |
117 else | |
118 $('recording').textContent = 'Start Recording'; | |
119 $('recording').disabled = false; | |
46 if (infos.deviceInfo !== undefined) { | 120 if (infos.deviceInfo !== undefined) { |
47 displayDeviceInfo(infos.deviceInfo); | 121 displayDeviceInfo(infos.deviceInfo); |
48 } | 122 } |
123 | |
124 removeAllChildNodes($('checkin-info')); | |
125 removeAllChildNodes($('register-info')); | |
126 removeAllChildNodes($('send-info')); | |
127 removeAllChildNodes($('receive-info')); | |
128 if (infos.checkinInfo !== undefined) { | |
129 addRows($('checkin-info'), infos.checkinInfo); | |
130 } | |
131 if (infos.registerInfo !== undefined) { | |
132 addRows($('register-info'), infos.registerInfo); | |
133 } | |
134 if (infos.sendInfo !== undefined) { | |
135 addRows($('send-info'), infos.sendInfo); | |
136 } | |
137 if (infos.receiveInfo !== undefined) { | |
138 addRows($('receive-info'), infos.receiveInfo); | |
139 } | |
49 } | 140 } |
50 | 141 |
51 // Return an object with all of the exports. | 142 // Return an object with all of the exports. |
52 return { | 143 return { |
53 initialize: initialize, | 144 initialize: initialize, |
54 setGcmInternalsInfo: setGcmInternalsInfo, | 145 setGcmInternalsInfo: setGcmInternalsInfo, |
55 }; | 146 }; |
56 }); | 147 }); |
57 | 148 |
58 document.addEventListener('DOMContentLoaded', gcmInternals.initialize); | 149 document.addEventListener('DOMContentLoaded', gcmInternals.initialize); |
OLD | NEW |