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