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, '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');
43 * Remove all the child nodes of the element.
44 * @param {HTMLElement} element A HTML element.
46 function removeAllChildNodes(element) {
47 element.textContent = '';
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.
57 function addRows(table, list) {
58 for (var i = 0; i < list.length; ++i) {
59 var row = document.createElement('tr');
61 // The first element is always a timestamp.
62 var cell = document.createElement('td');
63 var d = new Date(list[i][0]);
65 row.appendChild(cell);
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);
72 table.appendChild(row);
77 * Refresh all displayed information.
79 function refreshAll() {
80 chrome.send('getGcmInternalsInfo', [false]);
84 * Toggle the isRecording variable and send it to browser.
86 function setRecording() {
87 isRecording = !isRecording;
88 chrome.send('setGcmInternalsRecording', [isRecording]);
92 * Clear all the activity logs.
94 function clearLogs() {
95 chrome.send('getGcmInternalsInfo', [true]);
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]);
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.
112 function refreshLogTable(id, data) {
113 removeAllChildNodes($(id));
114 if (data !== undefined) {
115 addRows($(id), data);
120 * Callback function accepting a dictionary of info items to be displayed.
121 * @param {!Object} infos A dictionary of info items to be displayed.
123 function setGcmInternalsInfo(infos) {
124 isRecording = infos.isRecording;
126 $('recording').textContent = 'Stop Recording';
128 $('recording').textContent = 'Start Recording';
129 $('recording').disabled = false;
130 if (infos.deviceInfo !== undefined) {
131 displayDeviceInfo(infos.deviceInfo);
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);
141 // Return an object with all of the exports.
143 initialize: initialize,
144 setGcmInternalsInfo: setGcmInternalsInfo,
148 document.addEventListener('DOMContentLoaded', gcmInternals.initialize);