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 header = { | |
rlarocque
2014/02/15 00:09:39
Why not just put this in the HTML? Putting it the
mferreria_g
2014/02/15 01:27:39
Wasn't aware of the <thead> element. Done.
| |
35 registrar: 'Registrar', | |
36 source: 'Source', | |
37 name: 'Name', | |
38 count: 'Count', | |
39 version: 'Last Version', | |
40 time: 'Last Time', | |
41 payload: 'Last Payload', | |
42 type: 'header' | |
43 }; | |
44 var keys = []; | |
45 for (var key in tableObjects) | |
46 keys.push(key); | |
47 keys.sort(); | |
48 var sortedInvalidations = []; | |
49 sortedInvalidations.push(header); | |
50 for (var i = 0; i < keys.length; i++) | |
51 sortedInvalidations.push(tableObjects[keys[i]]); | |
52 var wrapped = { objectsidtable: sortedInvalidations }; | |
53 jstProcess(new JsEvalContext(wrapped), $('objectsid-table-div')); | |
54 } | |
24 | 55 |
25 /** | 56 /** |
26 * Shows the current state of the InvalidatorService | 57 * Shows the current state of the InvalidatorService |
27 * | 58 * |
28 * @param {string} newState The string to be displayed and logged. | 59 * @param {string} newState The string to be displayed and logged. |
29 */ | 60 */ |
30 function updateState(newState) { | 61 function updateState(newState) { |
31 var logMessage = nowTimeString() + | 62 var logMessage = nowTimeString() + |
32 'Invalidations service state changed to ' + quote(newState); | 63 'Invalidations service state changed to ' + quote(newState); |
33 | 64 |
34 appendToLog(logMessage); | 65 appendToLog(logMessage); |
35 $('invalidations-state').textContent = newState; | 66 $('invalidations-state').textContent = newState; |
36 currentInvalidationState = newState; | 67 currentInvalidationState = newState; |
37 } | 68 } |
38 | 69 |
39 /** | 70 /** |
40 * Adds to the log the latest invalidations received | 71 * Adds to the log the latest invalidations received |
41 * | 72 * |
42 * @param {Array of Object} allInvalidations The array of ObjectId | 73 * @param {Array of Object} allInvalidations The array of ObjectId |
43 * that contains the invalidations received by the InvalidatorService | 74 * that contains the invalidations received by the InvalidatorService. |
44 */ | 75 */ |
45 function logInvalidations(allInvalidations) { | 76 function logInvalidations(allInvalidations) { |
46 for (var i = 0; i < allInvalidations.length; i++) { | 77 for (var i = 0; i < allInvalidations.length; i++) { |
47 var inv = allInvalidations[i]; | 78 var inv = allInvalidations[i]; |
48 if (inv.hasOwnProperty('objectId')) { | 79 if (inv.hasOwnProperty('objectId')) { |
49 var logMessage = nowTimeString() + | 80 var logMessage = nowTimeString() + |
50 'Received Invalidation with type ' + | 81 'Received Invalidation with type ' + |
51 quote(inv.objectId.name) + | 82 quote(inv.objectId.name) + |
52 ' version ' + | 83 ' version ' + |
53 quote((inv.isUnknownVersion ? 'Unknown' : inv.version)) + | 84 quote((inv.isUnknownVersion ? 'Unknown' : inv.version)) + |
54 ' with payload ' + | 85 ' with payload ' + |
55 quote(inv.payload); | 86 quote(inv.payload); |
56 | 87 |
57 appendToLog(logMessage); | 88 appendToLog(logMessage); |
89 var isInvalidation = true; | |
90 logToTable(inv, isInvalidation); | |
58 } | 91 } |
59 } | 92 } |
93 repaintTable(); | |
94 } | |
95 | |
96 /** | |
97 * Marks a change in the table whether a new invalidation has arrived | |
98 * or a new ObjectId is currently being added or updated. | |
99 * | |
100 * @param {object} oId The ObjectId being added or updated. | |
101 * @param {bool} isInvaldation A flag that says that an invalidation | |
102 * for this ObjectId has arrived or we just need to add it to the table | |
103 * as it was just updated its state. | |
104 */ | |
105 function logToTable(oId, isInvalidation) { | |
106 var registrar = oId.registrar; | |
107 var name = oId.objectId.name; | |
108 var source = oId.objectId.source; | |
109 var key = source + '-' + name; | |
110 var time = new Date(); | |
111 var version = oId.isUnknownVersion ? '?' : | |
112 oId.version; | |
113 var payload = ''; | |
114 if (oId.hasOwnProperty('payload')) | |
115 payload = oId.payload; | |
116 if (!(key in tableObjects)) { | |
117 tableObjects[key] = { | |
118 name: name, | |
119 source: source, | |
120 count: 0, | |
121 registrar: registrar, | |
122 time: '', | |
123 version: '', | |
124 payload: '', | |
125 type: 'content' | |
126 }; | |
127 } | |
128 // Refresh the type to be a content because it might have been | |
129 // greyed out. | |
130 tableObjects[key].type = 'content'; | |
131 if (isInvalidation) { | |
132 tableObjects[key].count = tableObjects[key].count + 1; | |
133 tableObjects[key].time = time.toTimeString(); | |
134 tableObjects[key].version = version; | |
135 tableObjects[key].payload = payload; | |
136 } | |
60 } | 137 } |
61 | 138 |
62 /** | 139 /** |
140 * Updates the table with the objects ids registered for invalidations | |
141 * | |
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(); | |
163 } | |
164 | |
165 /** | |
63 * Function that notifies the Invalidator Logger that the UI is | 166 * Function that notifies the Invalidator Logger that the UI is |
64 * ready to receive real-time notifications. | 167 * ready to receive real-time notifications. |
65 */ | 168 */ |
66 function onLoadWork() { | 169 function onLoadWork() { |
67 chrome.send('doneLoading'); | 170 chrome.send('doneLoading'); |
68 } | 171 } |
69 | 172 |
70 return { | 173 return { |
71 updateState: updateState, | 174 updateState: updateState, |
175 updateIds: updateIds, | |
72 logInvalidations: logInvalidations, | 176 logInvalidations: logInvalidations, |
73 onLoadWork: onLoadWork | 177 onLoadWork: onLoadWork |
74 }; | 178 }; |
75 }); | 179 }); |
76 | 180 |
77 document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork); | 181 document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork); |
OLD | NEW |