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
3 // found in the LICENSE file.
5 cr
.define('chrome.invalidations', function() {
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
12 var tableObjects
= {};
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.
19 var cachedDetails
= {};
22 return '\"' + str
+ '\"';
25 function nowTimeString() {
26 return '[' + new Date().getTime() + '] ';
30 * Appends a string to a textarea log.
31 * @param {string} logMessage The string to be appended.
33 function appendToLog(logMessage
) {
34 var invalidationsLog
= $('invalidations-log');
35 invalidationsLog
.value
+= logMessage
+ '\n';
38 * Updates the jstemplate with the latest ObjectIds, ordered by registrar.
40 function repaintTable() {
42 for (var key
in tableObjects
) {
46 var sortedInvalidations
= [];
47 for (var i
= 0; i
< keys
.length
; i
++) {
48 sortedInvalidations
.push(tableObjects
[keys
[i
]]);
50 var wrapped
= { objectsidtable
: sortedInvalidations
};
51 jstProcess(new JsEvalContext(wrapped
), $('objectsid-table-div'));
55 * Shows the current state of the InvalidatorService.
56 * @param {string} newState The string to be displayed and logged.
58 function updateInvalidatorState(newState
) {
59 var logMessage
= nowTimeString() +
60 'Invalidations service state changed to ' + quote(newState
);
62 appendToLog(logMessage
);
63 $('invalidations-state').textContent
= newState
;
64 currentInvalidationState
= newState
;
68 * Adds to the log the latest invalidations received
69 * @param {!Array.<!Object>} allInvalidations The array of ObjectId
70 * that contains the invalidations received by the InvalidatorService.
72 function logInvalidations(allInvalidations
) {
73 for (var i
= 0; i
< allInvalidations
.length
; i
++) {
74 var inv
= allInvalidations
[i
];
75 if (inv
.hasOwnProperty('objectId')) {
76 var logMessage
= nowTimeString() +
77 'Received Invalidation with type ' +
78 quote(inv
.objectId
.name
) +
80 quote((inv
.isUnknownVersion
? 'Unknown' : inv
.version
)) +
84 appendToLog(logMessage
);
85 var isInvalidation
= true;
86 logToTable(inv
, isInvalidation
);
93 * Marks a change in the table whether a new invalidation has arrived
94 * or a new ObjectId is currently being added or updated.
95 * @param {!Object} oId The ObjectId being added or updated.
96 * @param {!boolean} isInvaldation A flag that says that an invalidation
97 * for this ObjectId has arrived or we just need to add it to the table
98 * as it was just updated its state.
100 function logToTable(oId
, isInvalidation
) {
101 var registrar
= oId
.registrar
;
102 var name
= oId
.objectId
.name
;
103 var source
= oId
.objectId
.source
;
104 var key
= source
+ '-' + name
;
105 var time
= new Date();
106 var version
= oId
.isUnknownVersion
? '?' :
109 if (oId
.hasOwnProperty('payload'))
110 payload
= oId
.payload
;
111 if (!(key
in tableObjects
)) {
112 tableObjects
[key
] = {
116 registrar
: registrar
,
123 // Refresh the type to be a content because it might have been
125 tableObjects
[key
].type
= 'content';
126 if (isInvalidation
) {
127 tableObjects
[key
].count
= tableObjects
[key
].count
+ 1;
128 tableObjects
[key
].time
= time
.toTimeString();
129 tableObjects
[key
].version
= version
;
130 tableObjects
[key
].payload
= payload
;
135 * Shows the handlers that are currently registered for invalidations
136 * (but might not have objects ids registered yet).
137 * @param {!Array.<string>} allHandlers An array of Strings that are
138 * the names of all the handlers currently registered in the
139 * InvalidatorService.
141 function updateHandlers(allHandlers
) {
142 var allHandlersFormatted
= allHandlers
.join(', ');
143 $('registered-handlers').textContent
= allHandlersFormatted
;
144 var logMessage
= nowTimeString() +
145 'InvalidatorHandlers currently registered: ' + allHandlersFormatted
;
146 appendToLog(logMessage
);
150 * Updates the table with the objects ids registered for invalidations
151 * @param {string} registrar The name of the owner of the InvalidationHandler
152 * that is registered for invalidations
153 * @param {Array of Object} allIds An array of ObjectsIds that are currently
154 * registered for invalidations. It is not differential (as in, whatever
155 * is not registered now but was before, it mean it was taken out the
156 * registered objects)
158 function updateIds(registrar
, allIds
) {
159 // Grey out every datatype assigned to this registrar
160 // (and reenable them later in case they are still registered).
161 for (var key
in tableObjects
) {
162 if (tableObjects
[key
]['registrar'] === registrar
)
163 tableObjects
[key
].type
= 'greyed';
165 // Reenable those ObjectsIds still registered with this registrar.
166 for (var i
= 0; i
< allIds
.length
; i
++) {
167 var oId
= { objectId
: allIds
[i
], registrar
: registrar
};
168 var isInvalidation
= false;
169 logToTable(oId
, isInvalidation
);
175 * Update the internal status display, merging new detailed information.
176 * @param {!Object} newDetails The dictionary containing assorted debugging
177 * details (e.g. Network Channel information).
179 function updateDetailedStatus(newDetails
) {
180 for (var key
in newDetails
) {
181 cachedDetails
[key
] = newDetails
[key
];
183 $('internal-display').value
= JSON
.stringify(cachedDetails
, null, 2);
187 * Function that notifies the InvalidationsMessageHandler that the UI is
188 * ready to receive real-time notifications.
190 function onLoadWork() {
191 $('request-detailed-status').onclick = function() {
193 chrome
.send('requestDetailedStatus');
195 chrome
.send('doneLoading');
199 logInvalidations
: logInvalidations
,
200 onLoadWork
: onLoadWork
,
201 updateDetailedStatus
: updateDetailedStatus
,
202 updateHandlers
: updateHandlers
,
203 updateIds
: updateIds
,
204 updateInvalidatorState
: updateInvalidatorState
,
208 document
.addEventListener('DOMContentLoaded', chrome
.invalidations
.onLoadWork
);