blob: 400070a078fe9839407326310e71614f7c433455 [file] [log] [blame]
[email protected]11025bb2014-02-12 01:50:301// 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
[email protected]35601812014-03-07 19:52:435cr.define('gcmInternals', function() {
[email protected]11025bb2014-02-12 01:50:306 'use strict';
7
[email protected]436bcb82014-04-18 00:40:578 var isRecording = false;
Peter Beverloof9469182018-05-03 20:46:529 var keyPressState = 0;
[email protected]436bcb82014-04-18 00:40:5710
[email protected]35601812014-03-07 19:52:4311 /**
12 * If the info dictionary has property prop, then set the text content of
[email protected]436bcb82014-04-18 00:40:5713 * element to the value of this property. Otherwise clear the content.
[email protected]35601812014-03-07 19:52:4314 * @param {!Object} info A dictionary of device infos to be displayed.
15 * @param {string} prop Name of the property.
peter1ed82982015-12-21 17:22:4916 * @param {string} elementId The id of a HTML element.
[email protected]35601812014-03-07 19:52:4317 */
peter1ed82982015-12-21 17:22:4918 function setIfExists(info, prop, elementId) {
19 var element = $(elementId);
20 if (!element)
21 return;
22
[email protected]35601812014-03-07 19:52:4323 if (info[prop] !== undefined) {
peter1ed82982015-12-21 17:22:4924 element.textContent = info[prop];
[email protected]436bcb82014-04-18 00:40:5725 } else {
peter1ed82982015-12-21 17:22:4926 element.textContent = '';
[email protected]35601812014-03-07 19:52:4327 }
28 }
29
30 /**
31 * Display device informations.
32 * @param {!Object} info A dictionary of device infos to be displayed.
33 */
34 function displayDeviceInfo(info) {
35 setIfExists(info, 'androidId', 'android-id');
Peter Beverloof9469182018-05-03 20:46:5236 setIfExists(info, 'androidSecret', 'android-secret');
[email protected]35601812014-03-07 19:52:4337 setIfExists(info, 'profileServiceCreated', 'profile-service-created');
[email protected]085f86b32014-05-28 16:09:5838 setIfExists(info, 'gcmEnabled', 'gcm-enabled');
[email protected]35601812014-03-07 19:52:4339 setIfExists(info, 'gcmClientCreated', 'gcm-client-created');
40 setIfExists(info, 'gcmClientState', 'gcm-client-state');
[email protected]35601812014-03-07 19:52:4341 setIfExists(info, 'connectionClientCreated', 'connection-client-created');
42 setIfExists(info, 'connectionState', 'connection-state');
zea76342abf2016-11-01 17:26:0443 setIfExists(info, 'lastCheckin', 'last-checkin');
44 setIfExists(info, 'nextCheckin', 'next-checkin');
[email protected]436bcb82014-04-18 00:40:5745 setIfExists(info, 'registeredAppIds', 'registered-app-ids');
46 setIfExists(info, 'sendQueueSize', 'send-queue-size');
47 setIfExists(info, 'resendQueueSize', 'resend-queue-size');
48 }
49
50 /**
51 * Remove all the child nodes of the element.
52 * @param {HTMLElement} element A HTML element.
53 */
54 function removeAllChildNodes(element) {
55 element.textContent = '';
56 }
57
58 /**
59 * For each item in line, add a row to the table. Each item is actually a list
60 * of sub-items; each of which will have a corresponding cell created in that
61 * row, and the sub-item will be displayed in the cell.
62 * @param {HTMLElement} table A HTML tbody element.
63 * @param {!Object} list A list of list of item.
64 */
65 function addRows(table, list) {
66 for (var i = 0; i < list.length; ++i) {
67 var row = document.createElement('tr');
68
69 // The first element is always a timestamp.
70 var cell = document.createElement('td');
71 var d = new Date(list[i][0]);
72 cell.textContent = d;
73 row.appendChild(cell);
74
75 for (var j = 1; j < list[i].length; ++j) {
76 var cell = document.createElement('td');
77 cell.textContent = list[i][j];
78 row.appendChild(cell);
79 }
80 table.appendChild(row);
81 }
82 }
83
84 /**
85 * Refresh all displayed information.
86 */
87 function refreshAll() {
88 chrome.send('getGcmInternalsInfo', [false]);
89 }
90
91 /**
92 * Toggle the isRecording variable and send it to browser.
93 */
94 function setRecording() {
95 isRecording = !isRecording;
96 chrome.send('setGcmInternalsRecording', [isRecording]);
97 }
98
99 /**
100 * Clear all the activity logs.
101 */
102 function clearLogs() {
103 chrome.send('getGcmInternalsInfo', [true]);
[email protected]35601812014-03-07 19:52:43104 }
105
[email protected]11025bb2014-02-12 01:50:30106 function initialize() {
[email protected]436bcb82014-04-18 00:40:57107 $('recording').disabled = true;
108 $('refresh').onclick = refreshAll;
109 $('recording').onclick = setRecording;
110 $('clear-logs').onclick = clearLogs;
111 chrome.send('getGcmInternalsInfo', [false]);
zea76342abf2016-11-01 17:26:04112
113 // Recording defaults to on.
114 chrome.send('setGcmInternalsRecording', [true]);
[email protected]35601812014-03-07 19:52:43115 }
116
117 /**
Peter Beverloof9469182018-05-03 20:46:52118 * Allows displaying the Android Secret by typing a secret phrase.
119 *
120 * There are good reasons for displaying the Android Secret associated with
121 * the local connection info, but we also need to be careful to make sure that
122 * users don't share this value by accident. Therefore we require a secret
123 * phrase to be typed into the page for making it visible.
124 *
125 * @param {!Event} event The keypress event handler.
126 */
127 function handleKeyPress(event) {
128 var PHRASE = 'secret';
129 if (PHRASE.charCodeAt(keyPressState) === event.keyCode) {
130 if (++keyPressState < PHRASE.length)
131 return;
132
133 $('android-secret-container').classList.remove('invisible');
134 }
135
136 keyPressState = 0;
137 }
138
139 /**
[email protected]e9712072014-05-07 16:03:22140 * Refresh the log html table by clearing it first. If data is not empty, then
141 * it will be used to populate the table.
peter1ed82982015-12-21 17:22:49142 * @param {string} tableId ID of the log html table.
[email protected]e9712072014-05-07 16:03:22143 * @param {!Object} data A list of list of data items.
144 */
peter1ed82982015-12-21 17:22:49145 function refreshLogTable(tableId, data) {
146 var element = $(tableId);
147 if (!element)
148 return;
149
150 removeAllChildNodes(element);
151 if (data !== undefined)
152 addRows(element, data);
[email protected]e9712072014-05-07 16:03:22153 }
154
155 /**
[email protected]35601812014-03-07 19:52:43156 * Callback function accepting a dictionary of info items to be displayed.
157 * @param {!Object} infos A dictionary of info items to be displayed.
158 */
159 function setGcmInternalsInfo(infos) {
[email protected]436bcb82014-04-18 00:40:57160 isRecording = infos.isRecording;
161 if (isRecording)
162 $('recording').textContent = 'Stop Recording';
163 else
164 $('recording').textContent = 'Start Recording';
165 $('recording').disabled = false;
[email protected]35601812014-03-07 19:52:43166 if (infos.deviceInfo !== undefined) {
167 displayDeviceInfo(infos.deviceInfo);
168 }
[email protected]436bcb82014-04-18 00:40:57169
[email protected]e9712072014-05-07 16:03:22170 refreshLogTable('checkin-info', infos.checkinInfo);
171 refreshLogTable('connection-info', infos.connectionInfo);
172 refreshLogTable('registration-info', infos.registrationInfo);
173 refreshLogTable('receive-info', infos.receiveInfo);
peteree284ba52016-02-01 11:53:28174 refreshLogTable('decryption-failure-info', infos.decryptionFailureInfo);
[email protected]e9712072014-05-07 16:03:22175 refreshLogTable('send-info', infos.sendInfo);
[email protected]11025bb2014-02-12 01:50:30176 }
177
178 // Return an object with all of the exports.
179 return {
180 initialize: initialize,
Peter Beverloof9469182018-05-03 20:46:52181 handleKeyPress: handleKeyPress,
[email protected]35601812014-03-07 19:52:43182 setGcmInternalsInfo: setGcmInternalsInfo,
[email protected]11025bb2014-02-12 01:50:30183 };
184});
185
[email protected]35601812014-03-07 19:52:43186document.addEventListener('DOMContentLoaded', gcmInternals.initialize);
Peter Beverloof9469182018-05-03 20:46:52187document.addEventListener('keypress', gcmInternals.handleKeyPress);