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.
5 cr.define('gcmInternals', function() {
8 var isRecording = false;
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.
17 function setIfExists(info, prop, element) {
18 if (info[prop] !== undefined) {
19 $(element).textContent = info[prop];
21 $(element).textContent = '';
26 * Display device informations.
27 * @param {!Object} info A dictionary of device infos to be displayed.
29 function displayDeviceInfo(info) {
30 setIfExists(info, 'androidId', 'android-id');
31 setIfExists(info, 'profileServiceCreated', 'profile-service-created');
32 setIfExists(info, 'gcmEnabledState', 'gcm-enabled-state');
33 setIfExists(info, 'signedInUserName', 'signed-in-username');
34 setIfExists(info, 'gcmClientCreated', 'gcm-client-created');
35 setIfExists(info, 'gcmClientState', 'gcm-client-state');
36 setIfExists(info, 'gcmClientReady', 'gcm-client-ready');
37 setIfExists(info, 'connectionClientCreated', 'connection-client-created');
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');
45 * Remove all the child nodes of the element.
46 * @param {HTMLElement} element A HTML element.
48 function removeAllChildNodes(element) {
49 element.textContent = '';
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.
59 function addRows(table, list) {
60 for (var i = 0; i < list.length; ++i) {
61 var row = document.createElement('tr');
63 // The first element is always a timestamp.
64 var cell = document.createElement('td');
65 var d = new Date(list[i][0]);
67 row.appendChild(cell);
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);
74 table.appendChild(row);
79 * Refresh all displayed information.
81 function refreshAll() {
82 chrome.send('getGcmInternalsInfo', [false]);
86 * Toggle the isRecording variable and send it to browser.
88 function setRecording() {
89 isRecording = !isRecording;
90 chrome.send('setGcmInternalsRecording', [isRecording]);
94 * Clear all the activity logs.
96 function clearLogs() {
97 chrome.send('getGcmInternalsInfo', [true]);
100 function initialize() {
101 $('recording').disabled = true;
102 $('refresh').onclick = refreshAll;
103 $('recording').onclick = setRecording;
104 $('clear-logs').onclick = clearLogs;
105 chrome.send('getGcmInternalsInfo', [false]);
109 * Refresh the log html table by clearing it first. If data is not empty, then
110 * it will be used to populate the table.
111 * @param {string} id ID of the log html table.
112 * @param {!Object} data A list of list of data items.
114 function refreshLogTable(id, data) {
115 removeAllChildNodes($(id));
116 if (data !== undefined) {
117 addRows($(id), data);
122 * Callback function accepting a dictionary of info items to be displayed.
123 * @param {!Object} infos A dictionary of info items to be displayed.
125 function setGcmInternalsInfo(infos) {
126 isRecording = infos.isRecording;
128 $('recording').textContent = 'Stop Recording';
130 $('recording').textContent = 'Start Recording';
131 $('recording').disabled = false;
132 if (infos.deviceInfo !== undefined) {
133 displayDeviceInfo(infos.deviceInfo);
136 refreshLogTable('checkin-info', infos.checkinInfo);
137 refreshLogTable('connection-info', infos.connectionInfo);
138 refreshLogTable('registration-info', infos.registrationInfo);
139 refreshLogTable('receive-info', infos.receiveInfo);
140 refreshLogTable('send-info', infos.sendInfo);
143 // Return an object with all of the exports.
145 initialize: initialize,
146 setGcmInternalsInfo: setGcmInternalsInfo,
150 document.addEventListener('DOMContentLoaded', gcmInternals.initialize);