OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('chrome.invalidations', function() { | 5 cr.define('chrome.invalidations', function() { |
| 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 |
| 9 * log any invalidations ocurred prior to opening the about:invalidation page) |
| 10 */ |
| 11 var tableObjects = {}; |
6 | 12 |
7 function quote(str) { | 13 function quote(str) { |
8 return '\"' + str + '\"'; | 14 return '\"' + str + '\"'; |
9 } | 15 } |
10 | 16 |
11 function nowTimeString() { | 17 function nowTimeString() { |
12 return '[' + new Date().getTime() + '] '; | 18 return '[' + new Date().getTime() + '] '; |
13 } | 19 } |
14 | 20 |
15 /** | 21 /** |
16 * Appends a string to a textarea log. | 22 * Appends a string to a textarea log. |
17 * | 23 * |
18 * @param {string} logMessage The string to be appended. | 24 * @param {string} logMessage The string to be appended. |
19 */ | 25 */ |
20 function appendToLog(logMessage) { | 26 function appendToLog(logMessage) { |
21 var invalidationsLog = $('invalidations-log'); | 27 var invalidationsLog = $('invalidations-log'); |
22 invalidationsLog.value += logMessage + '\n'; | 28 invalidationsLog.value += logMessage + '\n'; |
23 } | 29 } |
| 30 /** |
| 31 * Function that updates the jstemplate with the latest |
| 32 * ObjectIds, sorted by the registrar. |
| 33 */ |
| 34 function repaintTable() { |
| 35 var header = { |
| 36 registrar: 'Registrar', |
| 37 source: 'Source', |
| 38 name: 'Name', |
| 39 count: 'Count', |
| 40 version: 'Last Version', |
| 41 time: 'Last Time', |
| 42 payload: 'Last Payload', |
| 43 type: 'header' |
| 44 }; |
| 45 var keys = []; |
| 46 for (var key in tableObjects) { |
| 47 keys.push(key); |
| 48 } |
| 49 keys.sort(); |
| 50 var sortedInvalidations = []; |
| 51 sortedInvalidations.push(header); |
| 52 for (var i = 0; i < keys.length; i++) { |
| 53 sortedInvalidations.push(tableObjects[keys[i]]); |
| 54 } |
| 55 var wrapped = { objectsidtable: sortedInvalidations }; |
| 56 jstProcess(new JsEvalContext(wrapped), $('objectsid-table-div')); |
| 57 } |
24 | 58 |
25 /** | 59 /** |
26 * Shows the current state of the InvalidatorService | 60 * Shows the current state of the InvalidatorService |
27 * | 61 * |
28 * @param {string} newState The string to be displayed and logged. | 62 * @param {string} newState The string to be displayed and logged. |
29 */ | 63 */ |
30 function updateState(newState) { | 64 function updateState(newState) { |
31 var logMessage = nowTimeString() + | 65 var logMessage = nowTimeString() + |
32 'Invalidations service state changed to ' + quote(newState); | 66 'Invalidations service state changed to ' + quote(newState); |
33 | 67 |
34 appendToLog(logMessage); | 68 appendToLog(logMessage); |
35 $('invalidations-state').textContent = newState; | 69 $('invalidations-state').textContent = newState; |
36 currentInvalidationState = newState; | 70 currentInvalidationState = newState; |
37 } | 71 } |
38 | 72 |
39 /** | 73 /** |
40 * Adds to the log the latest invalidations received | 74 * Adds to the log the latest invalidations received |
41 * | 75 * |
42 * @param {Array of Object} allInvalidations The array of ObjectId | 76 * @param {Array of Object} allInvalidations The array of ObjectId |
43 * that contains the invalidations received by the InvalidatorService | 77 * that contains the invalidations received by the InvalidatorService. |
44 */ | 78 */ |
45 function logInvalidations(allInvalidations) { | 79 function logInvalidations(allInvalidations) { |
46 for (var i = 0; i < allInvalidations.length; i++) { | 80 for (var i = 0; i < allInvalidations.length; i++) { |
47 var inv = allInvalidations[i]; | 81 var inv = allInvalidations[i]; |
48 if (inv.hasOwnProperty('objectId')) { | 82 if (inv.hasOwnProperty('objectId')) { |
49 var logMessage = nowTimeString() + | 83 var logMessage = nowTimeString() + |
50 'Received Invalidation with type ' + | 84 'Received Invalidation with type ' + |
51 quote(inv.objectId.name) + | 85 quote(inv.objectId.name) + |
52 ' version ' + | 86 ' version ' + |
53 quote((inv.isUnknownVersion ? 'Unknown' : inv.version)) + | 87 quote((inv.isUnknownVersion ? 'Unknown' : inv.version)) + |
54 ' with payload ' + | 88 ' with payload ' + |
55 quote(inv.payload); | 89 quote(inv.payload); |
56 | 90 |
57 appendToLog(logMessage); | 91 appendToLog(logMessage); |
| 92 var isInvalidation = true; |
| 93 logToTable(inv, isInvalidation); |
58 } | 94 } |
59 } | 95 } |
| 96 repaintTable(); |
60 } | 97 } |
61 | 98 |
62 /** | 99 /** |
| 100 * Marks a change in the table whether a new invalidation has arrived |
| 101 * or a new ObjectId is currently being added or updated. |
| 102 * |
| 103 * @param {object} oId The ObjectId being added or updated. |
| 104 * @param {bool} isInvaldation A flag that says that an invalidation |
| 105 * for this ObjectId has arrived or we just need to add it to the table |
| 106 * as it was just updated its state. |
| 107 */ |
| 108 function logToTable(oId, isInvalidation) { |
| 109 var registrar = oId.registrar; |
| 110 var name = oId.objectId.name; |
| 111 var source = oId.objectId.source; |
| 112 var key = source + '-' + name; |
| 113 var time = new Date(); |
| 114 var version = oId.isUnknownVersion ? '?' : |
| 115 oId.version; |
| 116 var payload = ''; |
| 117 if (oId.hasOwnProperty('payload')) |
| 118 payload = oId.payload; |
| 119 if (!(key in tableObjects)) { |
| 120 tableObjects[key] = { |
| 121 name: name, |
| 122 source: source, |
| 123 count: 0, |
| 124 registrar: registrar, |
| 125 time: '', |
| 126 version: '', |
| 127 payload: '', |
| 128 type: 'content' |
| 129 }; |
| 130 } |
| 131 // Refresh the type to be a content because it might have been |
| 132 // greyed out. |
| 133 tableObjects[key].type = 'content'; |
| 134 if (isInvalidation) { |
| 135 tableObjects[key].count = tableObjects[key].count + 1; |
| 136 tableObjects[key].time = time.toTimeString(); |
| 137 tableObjects[key].version = version; |
| 138 tableObjects[key].payload = payload; |
| 139 } |
| 140 } |
| 141 /** |
| 142 * Updates the table with the objects ids registered for invalidations |
| 143 * |
| 144 * @param {string} registrar The name of the owner of the InvalidationHandler |
| 145 * that is registered for invalidations |
| 146 * @param {Array of Object} allIds An array of ObjectsIds that are currently |
| 147 * registered for invalidations. It is not differential (as in, whatever |
| 148 * is not registered now but was before, it mean it was taken out the |
| 149 * registered objects) |
| 150 */ |
| 151 |
| 152 function updateIds(registrar, allIds) { |
| 153 // Grey out every datatype assigned to this registrar |
| 154 // (and reenable them later in case they are still registered). |
| 155 for (var key in tableObjects) { |
| 156 if (tableObjects[key]['registrar'] === registrar) { |
| 157 tableObjects[key].type = 'greyed'; |
| 158 } |
| 159 } |
| 160 // Reenable those ObjectsIds still registered with this registrar. |
| 161 for (var i = 0; i < allIds.length; i++) { |
| 162 var oId = { objectId: allIds[i], registrar: registrar }; |
| 163 var isInvalidation = false; |
| 164 logToTable(oId, isInvalidation); |
| 165 } |
| 166 repaintTable(); |
| 167 } |
| 168 |
| 169 /** |
63 * Function that notifies the Invalidator Logger that the UI is | 170 * Function that notifies the Invalidator Logger that the UI is |
64 * ready to receive real-time notifications. | 171 * ready to receive real-time notifications. |
65 */ | 172 */ |
66 function onLoadWork() { | 173 function onLoadWork() { |
67 chrome.send('doneLoading'); | 174 chrome.send('doneLoading'); |
68 } | 175 } |
69 | 176 |
70 return { | 177 return { |
71 updateState: updateState, | 178 updateState: updateState, |
| 179 updateIds: updateIds, |
72 logInvalidations: logInvalidations, | 180 logInvalidations: logInvalidations, |
73 onLoadWork: onLoadWork | 181 onLoadWork: onLoadWork |
74 }; | 182 }; |
75 }); | 183 }); |
76 | 184 |
77 document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork); | 185 document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork); |
OLD | NEW |