[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | f17be76 | 2014-01-30 21:05:38 | [diff] [blame] | 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] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 5 | cr.define('chrome.invalidations', function() { |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 6 | /** |
| 7 | * Local variable where we maintain a count of the invalidations received |
| 8 | * and of every ObjectId that has ever been updated (note that this doesn't |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 9 | * log any invalidations ocurred prior to opening the about:invalidation |
| 10 | * page). |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 11 | */ |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 12 | const tableObjects = {}; |
[email protected] | f17be76 | 2014-01-30 21:05:38 | [diff] [blame] | 13 | |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 14 | /** |
| 15 | * Local variable that contains the detailed information in an object form. |
| 16 | * This was done this way as to allow multiple calls to updateDetailedStatus |
| 17 | * to keep adding new items. |
| 18 | */ |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 19 | let cachedDetails = {}; |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 20 | |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 21 | function quote(str) { |
| 22 | return '\"' + str + '\"'; |
| 23 | } |
| 24 | |
| 25 | function nowTimeString() { |
| 26 | return '[' + new Date().getTime() + '] '; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Appends a string to a textarea log. |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 31 | * @param {string} logMessage The string to be appended. |
| 32 | */ |
| 33 | function appendToLog(logMessage) { |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 34 | const invalidationsLog = $('invalidations-log'); |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 35 | invalidationsLog.value += logMessage + '\n'; |
| 36 | } |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 37 | /** |
| 38 | * Updates the jstemplate with the latest ObjectIds, ordered by registrar. |
| 39 | */ |
| 40 | function repaintTable() { |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 41 | const keys = []; |
| 42 | for (const key in tableObjects) { |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 43 | keys.push(key); |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 44 | } |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 45 | keys.sort(); |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 46 | const sortedInvalidations = []; |
| 47 | for (let i = 0; i < keys.length; i++) { |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 48 | sortedInvalidations.push(tableObjects[keys[i]]); |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 49 | } |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 50 | const wrapped = {objectsidtable: sortedInvalidations}; |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 51 | jstProcess(new JsEvalContext(wrapped), $('objectsid-table-div')); |
| 52 | } |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 53 | |
| 54 | /** |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 55 | * Shows the current state of the InvalidatorService. |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 56 | * @param {string} newState The string to be displayed and logged. |
[email protected] | 46109c5 | 2014-04-08 22:58:34 | [diff] [blame] | 57 | * @param {number} lastChangedTime The time in epoch when the state was last |
| 58 | * changed. |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 59 | */ |
[email protected] | 46109c5 | 2014-04-08 22:58:34 | [diff] [blame] | 60 | function updateInvalidatorState(newState, lastChangedTime) { |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 61 | const logMessage = nowTimeString() + |
dbeam | d0e4268 | 2017-06-19 07:14:10 | [diff] [blame] | 62 | 'Invalidations service state changed to ' + quote(newState); |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 63 | |
| 64 | appendToLog(logMessage); |
dbeam | d0e4268 | 2017-06-19 07:14:10 | [diff] [blame] | 65 | $('invalidations-state').textContent = |
| 66 | newState + ' (since ' + new Date(lastChangedTime) + ')'; |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Adds to the log the latest invalidations received |
Dan Beam | b9749f41 | 2015-02-14 02:42:50 | [diff] [blame] | 71 | * @param {!Array<!Object>} allInvalidations The array of ObjectId |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 72 | * that contains the invalidations received by the InvalidatorService. |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 73 | */ |
| 74 | function logInvalidations(allInvalidations) { |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 75 | for (let i = 0; i < allInvalidations.length; i++) { |
| 76 | const inv = allInvalidations[i]; |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 77 | if (inv.hasOwnProperty('objectId')) { |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 78 | const logMessage = nowTimeString() + |
| 79 | 'Received Invalidation with type ' + quote(inv.objectId.name) + |
| 80 | ' version ' + |
dbeam | d0e4268 | 2017-06-19 07:14:10 | [diff] [blame] | 81 | quote((inv.isUnknownVersion ? 'Unknown' : inv.version)) + |
| 82 | ' with payload ' + quote(inv.payload); |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 83 | |
| 84 | appendToLog(logMessage); |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 85 | const isInvalidation = true; |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 86 | logToTable(inv, isInvalidation); |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 87 | } |
| 88 | } |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 89 | repaintTable(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Marks a change in the table whether a new invalidation has arrived |
| 94 | * or a new ObjectId is currently being added or updated. |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 95 | * @param {!Object} oId The ObjectId being added or updated. |
| 96 | * @param {!boolean} isInvaldation A flag that says that an invalidation |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 97 | * for this ObjectId has arrived or we just need to add it to the table |
| 98 | * as it was just updated its state. |
| 99 | */ |
| 100 | function logToTable(oId, isInvalidation) { |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 101 | const registrar = oId.registrar; |
| 102 | const name = oId.objectId.name; |
| 103 | const source = oId.objectId.source; |
| 104 | const totalCount = oId.objectId.totalCount || 0; |
| 105 | const key = source + '-' + name; |
| 106 | const time = new Date(); |
| 107 | const version = oId.isUnknownVersion ? '?' : oId.version; |
| 108 | let payload = ''; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame^] | 109 | if (oId.hasOwnProperty('payload')) { |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 110 | payload = oId.payload; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame^] | 111 | } |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 112 | if (!(key in tableObjects)) { |
| 113 | tableObjects[key] = { |
| 114 | name: name, |
| 115 | source: source, |
[email protected] | 9364154f | 2014-03-31 17:39:24 | [diff] [blame] | 116 | totalCount: totalCount, |
| 117 | sessionCount: 0, |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 118 | registrar: registrar, |
| 119 | time: '', |
| 120 | version: '', |
| 121 | payload: '', |
| 122 | type: 'content' |
| 123 | }; |
| 124 | } |
| 125 | // Refresh the type to be a content because it might have been |
| 126 | // greyed out. |
| 127 | tableObjects[key].type = 'content'; |
| 128 | if (isInvalidation) { |
[email protected] | 9364154f | 2014-03-31 17:39:24 | [diff] [blame] | 129 | tableObjects[key].totalCount = tableObjects[key].totalCount + 1; |
| 130 | tableObjects[key].sessionCount = tableObjects[key].sessionCount + 1; |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 131 | tableObjects[key].time = time.toTimeString(); |
| 132 | tableObjects[key].version = version; |
| 133 | tableObjects[key].payload = payload; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 138 | * Shows the handlers that are currently registered for invalidations |
| 139 | * (but might not have objects ids registered yet). |
Dan Beam | b9749f41 | 2015-02-14 02:42:50 | [diff] [blame] | 140 | * @param {!Array<string>} allHandlers An array of Strings that are |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 141 | * the names of all the handlers currently registered in the |
| 142 | * InvalidatorService. |
| 143 | */ |
| 144 | function updateHandlers(allHandlers) { |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 145 | const allHandlersFormatted = allHandlers.join(', '); |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 146 | $('registered-handlers').textContent = allHandlersFormatted; |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 147 | const logMessage = nowTimeString() + |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 148 | 'InvalidatorHandlers currently registered: ' + allHandlersFormatted; |
| 149 | appendToLog(logMessage); |
| 150 | } |
| 151 | |
| 152 | /** |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 153 | * Updates the table with the objects ids registered for invalidations |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 154 | * @param {string} registrar The name of the owner of the InvalidationHandler |
| 155 | * that is registered for invalidations |
| 156 | * @param {Array of Object} allIds An array of ObjectsIds that are currently |
| 157 | * registered for invalidations. It is not differential (as in, whatever |
| 158 | * is not registered now but was before, it mean it was taken out the |
| 159 | * registered objects) |
| 160 | */ |
| 161 | function updateIds(registrar, allIds) { |
| 162 | // Grey out every datatype assigned to this registrar |
| 163 | // (and reenable them later in case they are still registered). |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 164 | for (const key in tableObjects) { |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame^] | 165 | if (tableObjects[key]['registrar'] === registrar) { |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 166 | tableObjects[key].type = 'greyed'; |
Dan Beam | d1cca6e | 2019-01-03 02:46:27 | [diff] [blame^] | 167 | } |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 168 | } |
| 169 | // Reenable those ObjectsIds still registered with this registrar. |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 170 | for (let i = 0; i < allIds.length; i++) { |
| 171 | const oId = {objectId: allIds[i], registrar: registrar}; |
| 172 | const isInvalidation = false; |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 173 | logToTable(oId, isInvalidation); |
| 174 | } |
| 175 | repaintTable(); |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 179 | * Update the internal status display, merging new detailed information. |
| 180 | * @param {!Object} newDetails The dictionary containing assorted debugging |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 181 | * details (e.g. Network Channel information). |
| 182 | */ |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 183 | function updateDetailedStatus(newDetails) { |
dpapad | aa2d59d | 2018-12-05 19:10:40 | [diff] [blame] | 184 | for (const key in newDetails) { |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 185 | cachedDetails[key] = newDetails[key]; |
| 186 | } |
| 187 | $('internal-display').value = JSON.stringify(cachedDetails, null, 2); |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Function that notifies the InvalidationsMessageHandler that the UI is |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 192 | * ready to receive real-time notifications. |
| 193 | */ |
| 194 | function onLoadWork() { |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 195 | $('request-detailed-status').onclick = function() { |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 196 | cachedDetails = {}; |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 197 | chrome.send('requestDetailedStatus'); |
| 198 | }; |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 199 | chrome.send('doneLoading'); |
| 200 | } |
| 201 | |
| 202 | return { |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 203 | logInvalidations: logInvalidations, |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 204 | onLoadWork: onLoadWork, |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 205 | updateDetailedStatus: updateDetailedStatus, |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 206 | updateHandlers: updateHandlers, |
| 207 | updateIds: updateIds, |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 208 | updateInvalidatorState: updateInvalidatorState, |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 209 | }; |
| 210 | }); |
| 211 | |
| 212 | document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork); |