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