[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 | */ |
| 12 | var 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 | */ |
| 19 | var cachedDetails = {}; |
| 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) { |
| 34 | var invalidationsLog = $('invalidations-log'); |
| 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() { |
| 41 | var keys = []; |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 42 | for (var 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(); |
| 46 | var sortedInvalidations = []; |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 47 | for (var 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 | } |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 50 | var wrapped = { objectsidtable: sortedInvalidations }; |
| 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) { |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 61 | var logMessage = nowTimeString() + |
| 62 | 'Invalidations service state changed to ' + quote(newState); |
| 63 | |
| 64 | appendToLog(logMessage); |
[email protected] | 46109c5 | 2014-04-08 22:58:34 | [diff] [blame^] | 65 | $('invalidations-state').textContent = newState + ' (since ' + |
| 66 | 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 |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [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) { |
| 75 | for (var i = 0; i < allInvalidations.length; i++) { |
| 76 | var inv = allInvalidations[i]; |
| 77 | if (inv.hasOwnProperty('objectId')) { |
| 78 | var logMessage = nowTimeString() + |
| 79 | 'Received Invalidation with type ' + |
| 80 | quote(inv.objectId.name) + |
| 81 | ' version ' + |
| 82 | quote((inv.isUnknownVersion ? 'Unknown' : inv.version)) + |
| 83 | ' with payload ' + |
| 84 | quote(inv.payload); |
| 85 | |
| 86 | appendToLog(logMessage); |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 87 | var isInvalidation = true; |
| 88 | logToTable(inv, isInvalidation); |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 89 | } |
| 90 | } |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 91 | repaintTable(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Marks a change in the table whether a new invalidation has arrived |
| 96 | * or a new ObjectId is currently being added or updated. |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 97 | * @param {!Object} oId The ObjectId being added or updated. |
| 98 | * @param {!boolean} isInvaldation A flag that says that an invalidation |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 99 | * for this ObjectId has arrived or we just need to add it to the table |
| 100 | * as it was just updated its state. |
| 101 | */ |
| 102 | function logToTable(oId, isInvalidation) { |
| 103 | var registrar = oId.registrar; |
| 104 | var name = oId.objectId.name; |
| 105 | var source = oId.objectId.source; |
[email protected] | 9364154f | 2014-03-31 17:39:24 | [diff] [blame] | 106 | var totalCount = oId.objectId.totalCount || 0; |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 107 | var key = source + '-' + name; |
| 108 | var time = new Date(); |
| 109 | var version = oId.isUnknownVersion ? '?' : |
| 110 | oId.version; |
| 111 | var payload = ''; |
| 112 | if (oId.hasOwnProperty('payload')) |
| 113 | payload = oId.payload; |
| 114 | if (!(key in tableObjects)) { |
| 115 | tableObjects[key] = { |
| 116 | name: name, |
| 117 | source: source, |
[email protected] | 9364154f | 2014-03-31 17:39:24 | [diff] [blame] | 118 | totalCount: totalCount, |
| 119 | sessionCount: 0, |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 120 | registrar: registrar, |
| 121 | time: '', |
| 122 | version: '', |
| 123 | payload: '', |
| 124 | type: 'content' |
| 125 | }; |
| 126 | } |
| 127 | // Refresh the type to be a content because it might have been |
| 128 | // greyed out. |
| 129 | tableObjects[key].type = 'content'; |
| 130 | if (isInvalidation) { |
[email protected] | 9364154f | 2014-03-31 17:39:24 | [diff] [blame] | 131 | tableObjects[key].totalCount = tableObjects[key].totalCount + 1; |
| 132 | tableObjects[key].sessionCount = tableObjects[key].sessionCount + 1; |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 133 | tableObjects[key].time = time.toTimeString(); |
| 134 | tableObjects[key].version = version; |
| 135 | tableObjects[key].payload = payload; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 140 | * Shows the handlers that are currently registered for invalidations |
| 141 | * (but might not have objects ids registered yet). |
| 142 | * @param {!Array.<string>} allHandlers An array of Strings that are |
| 143 | * the names of all the handlers currently registered in the |
| 144 | * InvalidatorService. |
| 145 | */ |
| 146 | function updateHandlers(allHandlers) { |
| 147 | var allHandlersFormatted = allHandlers.join(', '); |
| 148 | $('registered-handlers').textContent = allHandlersFormatted; |
| 149 | var logMessage = nowTimeString() + |
| 150 | 'InvalidatorHandlers currently registered: ' + allHandlersFormatted; |
| 151 | appendToLog(logMessage); |
| 152 | } |
| 153 | |
| 154 | /** |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 155 | * Updates the table with the objects ids registered for invalidations |
[email protected] | 9b0d66b | 2014-02-20 13:16:01 | [diff] [blame] | 156 | * @param {string} registrar The name of the owner of the InvalidationHandler |
| 157 | * that is registered for invalidations |
| 158 | * @param {Array of Object} allIds An array of ObjectsIds that are currently |
| 159 | * registered for invalidations. It is not differential (as in, whatever |
| 160 | * is not registered now but was before, it mean it was taken out the |
| 161 | * registered objects) |
| 162 | */ |
| 163 | function updateIds(registrar, allIds) { |
| 164 | // Grey out every datatype assigned to this registrar |
| 165 | // (and reenable them later in case they are still registered). |
| 166 | for (var key in tableObjects) { |
| 167 | if (tableObjects[key]['registrar'] === registrar) |
| 168 | tableObjects[key].type = 'greyed'; |
| 169 | } |
| 170 | // Reenable those ObjectsIds still registered with this registrar. |
| 171 | for (var i = 0; i < allIds.length; i++) { |
| 172 | var oId = { objectId: allIds[i], registrar: registrar }; |
| 173 | var isInvalidation = false; |
| 174 | logToTable(oId, isInvalidation); |
| 175 | } |
| 176 | repaintTable(); |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 180 | * Update the internal status display, merging new detailed information. |
| 181 | * @param {!Object} newDetails The dictionary containing assorted debugging |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 182 | * details (e.g. Network Channel information). |
| 183 | */ |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 184 | function updateDetailedStatus(newDetails) { |
| 185 | for (var key in newDetails) { |
| 186 | cachedDetails[key] = newDetails[key]; |
| 187 | } |
| 188 | $('internal-display').value = JSON.stringify(cachedDetails, null, 2); |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Function that notifies the InvalidationsMessageHandler that the UI is |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 193 | * ready to receive real-time notifications. |
| 194 | */ |
| 195 | function onLoadWork() { |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 196 | $('request-detailed-status').onclick = function() { |
[email protected] | edfe19f | 2014-03-21 01:38:12 | [diff] [blame] | 197 | cachedDetails = {}; |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 198 | chrome.send('requestDetailedStatus'); |
| 199 | }; |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 200 | chrome.send('doneLoading'); |
| 201 | } |
| 202 | |
| 203 | return { |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 204 | logInvalidations: logInvalidations, |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 205 | onLoadWork: onLoadWork, |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 206 | updateDetailedStatus: updateDetailedStatus, |
[email protected] | 063cbbb3 | 2014-02-27 01:46:20 | [diff] [blame] | 207 | updateHandlers: updateHandlers, |
| 208 | updateIds: updateIds, |
[email protected] | 5cb5b18 | 2014-03-18 00:32:39 | [diff] [blame] | 209 | updateInvalidatorState: updateInvalidatorState, |
[email protected] | b760027 | 2014-02-11 18:55:55 | [diff] [blame] | 210 | }; |
| 211 | }); |
| 212 | |
| 213 | document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork); |