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 * 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 } |
24 | 44 |
25 /** | 45 /** |
26 * Shows the current state of the InvalidatorService | 46 * Shows the current state of the InvalidatorService |
27 * | 47 * |
28 * @param {string} newState The string to be displayed and logged. | 48 * @param {string} newState The string to be displayed and logged. |
29 */ | 49 */ |
30 function updateState(newState) { | 50 function updateState(newState) { |
31 var logMessage = nowTimeString() + | 51 var logMessage = nowTimeString() + |
32 'Invalidations service state changed to ' + quote(newState); | 52 'Invalidations service state changed to ' + quote(newState); |
33 | 53 |
34 appendToLog(logMessage); | 54 appendToLog(logMessage); |
35 $('invalidations-state').textContent = newState; | 55 $('invalidations-state').textContent = newState; |
36 currentInvalidationState = newState; | 56 currentInvalidationState = newState; |
37 } | 57 } |
38 | 58 |
39 /** | 59 /** |
40 * Adds to the log the latest invalidations received | 60 * Adds to the log the latest invalidations received |
41 * | 61 * |
42 * @param {Array of Object} allInvalidations The array of ObjectId | 62 * @param {Array of Object} allInvalidations The array of ObjectId |
43 * that contains the invalidations received by the InvalidatorService | 63 * that contains the invalidations received by the InvalidatorService. |
44 */ | 64 */ |
45 function logInvalidations(allInvalidations) { | 65 function logInvalidations(allInvalidations) { |
46 for (var i = 0; i < allInvalidations.length; i++) { | 66 for (var i = 0; i < allInvalidations.length; i++) { |
47 var inv = allInvalidations[i]; | 67 var inv = allInvalidations[i]; |
48 if (inv.hasOwnProperty('objectId')) { | 68 if (inv.hasOwnProperty('objectId')) { |
49 var logMessage = nowTimeString() + | 69 var logMessage = nowTimeString() + |
50 'Received Invalidation with type ' + | 70 'Received Invalidation with type ' + |
51 quote(inv.objectId.name) + | 71 quote(inv.objectId.name) + |
52 ' version ' + | 72 ' version ' + |
53 quote((inv.isUnknownVersion ? 'Unknown' : inv.version)) + | 73 quote((inv.isUnknownVersion ? 'Unknown' : inv.version)) + |
54 ' with payload ' + | 74 ' with payload ' + |
55 quote(inv.payload); | 75 quote(inv.payload); |
56 | 76 |
57 appendToLog(logMessage); | 77 appendToLog(logMessage); |
| 78 var isInvalidation = true; |
| 79 logToTable(inv, isInvalidation); |
58 } | 80 } |
59 } | 81 } |
| 82 repaintTable(); |
| 83 } |
| 84 |
| 85 /** |
| 86 * Marks a change in the table whether a new invalidation has arrived |
| 87 * or a new ObjectId is currently being added or updated. |
| 88 * |
| 89 * @param {object} oId The ObjectId being added or updated. |
| 90 * @param {bool} isInvaldation A flag that says that an invalidation |
| 91 * for this ObjectId has arrived or we just need to add it to the table |
| 92 * as it was just updated its state. |
| 93 */ |
| 94 function logToTable(oId, isInvalidation) { |
| 95 var registrar = oId.registrar; |
| 96 var name = oId.objectId.name; |
| 97 var source = oId.objectId.source; |
| 98 var key = source + '-' + name; |
| 99 var time = new Date(); |
| 100 var version = oId.isUnknownVersion ? '?' : |
| 101 oId.version; |
| 102 var payload = ''; |
| 103 if (oId.hasOwnProperty('payload')) |
| 104 payload = oId.payload; |
| 105 if (!(key in tableObjects)) { |
| 106 tableObjects[key] = { |
| 107 name: name, |
| 108 source: source, |
| 109 count: 0, |
| 110 registrar: registrar, |
| 111 time: '', |
| 112 version: '', |
| 113 payload: '', |
| 114 type: 'content' |
| 115 }; |
| 116 } |
| 117 // Refresh the type to be a content because it might have been |
| 118 // greyed out. |
| 119 tableObjects[key].type = 'content'; |
| 120 if (isInvalidation) { |
| 121 tableObjects[key].count = tableObjects[key].count + 1; |
| 122 tableObjects[key].time = time.toTimeString(); |
| 123 tableObjects[key].version = version; |
| 124 tableObjects[key].payload = payload; |
| 125 } |
60 } | 126 } |
61 | 127 |
62 /** | 128 /** |
| 129 * Updates the table with the objects ids registered for invalidations |
| 130 * |
| 131 * @param {string} registrar The name of the owner of the InvalidationHandler |
| 132 * that is registered for invalidations |
| 133 * @param {Array of Object} allIds An array of ObjectsIds that are currently |
| 134 * registered for invalidations. It is not differential (as in, whatever |
| 135 * is not registered now but was before, it mean it was taken out the |
| 136 * registered objects) |
| 137 */ |
| 138 function updateIds(registrar, allIds) { |
| 139 // Grey out every datatype assigned to this registrar |
| 140 // (and reenable them later in case they are still registered). |
| 141 for (var key in tableObjects) { |
| 142 if (tableObjects[key]['registrar'] === registrar) |
| 143 tableObjects[key].type = 'greyed'; |
| 144 } |
| 145 // Reenable those ObjectsIds still registered with this registrar. |
| 146 for (var i = 0; i < allIds.length; i++) { |
| 147 var oId = { objectId: allIds[i], registrar: registrar }; |
| 148 var isInvalidation = false; |
| 149 logToTable(oId, isInvalidation); |
| 150 } |
| 151 repaintTable(); |
| 152 } |
| 153 |
| 154 /** |
63 * Function that notifies the Invalidator Logger that the UI is | 155 * Function that notifies the Invalidator Logger that the UI is |
64 * ready to receive real-time notifications. | 156 * ready to receive real-time notifications. |
65 */ | 157 */ |
66 function onLoadWork() { | 158 function onLoadWork() { |
67 chrome.send('doneLoading'); | 159 chrome.send('doneLoading'); |
68 } | 160 } |
69 | 161 |
70 return { | 162 return { |
71 updateState: updateState, | 163 updateState: updateState, |
| 164 updateIds: updateIds, |
72 logInvalidations: logInvalidations, | 165 logInvalidations: logInvalidations, |
73 onLoadWork: onLoadWork | 166 onLoadWork: onLoadWork |
74 }; | 167 }; |
75 }); | 168 }); |
76 | 169 |
77 document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork); | 170 document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork); |
OLD | NEW |