[email protected] | 11025bb | 2014-02-12 01:50:30 | [diff] [blame] | 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 | |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 5 | cr.define('gcmInternals', function() { |
[email protected] | 11025bb | 2014-02-12 01:50:30 | [diff] [blame] | 6 | 'use strict'; |
| 7 | |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 8 | var isRecording = false; |
Peter Beverloo | f946918 | 2018-05-03 20:46:52 | [diff] [blame^] | 9 | var keyPressState = 0; |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 10 | |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 11 | /** |
| 12 | * If the info dictionary has property prop, then set the text content of |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 13 | * element to the value of this property. Otherwise clear the content. |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 14 | * @param {!Object} info A dictionary of device infos to be displayed. |
| 15 | * @param {string} prop Name of the property. |
peter | 1ed8298 | 2015-12-21 17:22:49 | [diff] [blame] | 16 | * @param {string} elementId The id of a HTML element. |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 17 | */ |
peter | 1ed8298 | 2015-12-21 17:22:49 | [diff] [blame] | 18 | function setIfExists(info, prop, elementId) { |
| 19 | var element = $(elementId); |
| 20 | if (!element) |
| 21 | return; |
| 22 | |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 23 | if (info[prop] !== undefined) { |
peter | 1ed8298 | 2015-12-21 17:22:49 | [diff] [blame] | 24 | element.textContent = info[prop]; |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 25 | } else { |
peter | 1ed8298 | 2015-12-21 17:22:49 | [diff] [blame] | 26 | element.textContent = ''; |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 27 | } |
| 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 Beverloo | f946918 | 2018-05-03 20:46:52 | [diff] [blame^] | 36 | setIfExists(info, 'androidSecret', 'android-secret'); |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 37 | setIfExists(info, 'profileServiceCreated', 'profile-service-created'); |
[email protected] | 085f86b3 | 2014-05-28 16:09:58 | [diff] [blame] | 38 | setIfExists(info, 'gcmEnabled', 'gcm-enabled'); |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 39 | setIfExists(info, 'gcmClientCreated', 'gcm-client-created'); |
| 40 | setIfExists(info, 'gcmClientState', 'gcm-client-state'); |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 41 | setIfExists(info, 'connectionClientCreated', 'connection-client-created'); |
| 42 | setIfExists(info, 'connectionState', 'connection-state'); |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 43 | setIfExists(info, 'lastCheckin', 'last-checkin'); |
| 44 | setIfExists(info, 'nextCheckin', 'next-checkin'); |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 45 | 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] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 11025bb | 2014-02-12 01:50:30 | [diff] [blame] | 106 | function initialize() { |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 107 | $('recording').disabled = true; |
| 108 | $('refresh').onclick = refreshAll; |
| 109 | $('recording').onclick = setRecording; |
| 110 | $('clear-logs').onclick = clearLogs; |
| 111 | chrome.send('getGcmInternalsInfo', [false]); |
zea | 76342abf | 2016-11-01 17:26:04 | [diff] [blame] | 112 | |
| 113 | // Recording defaults to on. |
| 114 | chrome.send('setGcmInternalsRecording', [true]); |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Peter Beverloo | f946918 | 2018-05-03 20:46:52 | [diff] [blame^] | 118 | * 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] | e971207 | 2014-05-07 16:03:22 | [diff] [blame] | 140 | * Refresh the log html table by clearing it first. If data is not empty, then |
| 141 | * it will be used to populate the table. |
peter | 1ed8298 | 2015-12-21 17:22:49 | [diff] [blame] | 142 | * @param {string} tableId ID of the log html table. |
[email protected] | e971207 | 2014-05-07 16:03:22 | [diff] [blame] | 143 | * @param {!Object} data A list of list of data items. |
| 144 | */ |
peter | 1ed8298 | 2015-12-21 17:22:49 | [diff] [blame] | 145 | 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] | e971207 | 2014-05-07 16:03:22 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 156 | * 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] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 160 | isRecording = infos.isRecording; |
| 161 | if (isRecording) |
| 162 | $('recording').textContent = 'Stop Recording'; |
| 163 | else |
| 164 | $('recording').textContent = 'Start Recording'; |
| 165 | $('recording').disabled = false; |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 166 | if (infos.deviceInfo !== undefined) { |
| 167 | displayDeviceInfo(infos.deviceInfo); |
| 168 | } |
[email protected] | 436bcb8 | 2014-04-18 00:40:57 | [diff] [blame] | 169 | |
[email protected] | e971207 | 2014-05-07 16:03:22 | [diff] [blame] | 170 | refreshLogTable('checkin-info', infos.checkinInfo); |
| 171 | refreshLogTable('connection-info', infos.connectionInfo); |
| 172 | refreshLogTable('registration-info', infos.registrationInfo); |
| 173 | refreshLogTable('receive-info', infos.receiveInfo); |
peter | ee284ba5 | 2016-02-01 11:53:28 | [diff] [blame] | 174 | refreshLogTable('decryption-failure-info', infos.decryptionFailureInfo); |
[email protected] | e971207 | 2014-05-07 16:03:22 | [diff] [blame] | 175 | refreshLogTable('send-info', infos.sendInfo); |
[email protected] | 11025bb | 2014-02-12 01:50:30 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // Return an object with all of the exports. |
| 179 | return { |
| 180 | initialize: initialize, |
Peter Beverloo | f946918 | 2018-05-03 20:46:52 | [diff] [blame^] | 181 | handleKeyPress: handleKeyPress, |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 182 | setGcmInternalsInfo: setGcmInternalsInfo, |
[email protected] | 11025bb | 2014-02-12 01:50:30 | [diff] [blame] | 183 | }; |
| 184 | }); |
| 185 | |
[email protected] | 3560181 | 2014-03-07 19:52:43 | [diff] [blame] | 186 | document.addEventListener('DOMContentLoaded', gcmInternals.initialize); |
Peter Beverloo | f946918 | 2018-05-03 20:46:52 | [diff] [blame^] | 187 | document.addEventListener('keypress', gcmInternals.handleKeyPress); |