blob: 572c20bdaeba84cd3c17974878ae31415849de47 [file] [log] [blame]
[email protected]9b0d66b2014-02-20 13:16:011// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]f17be762014-01-30 21:05:382// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b7600272014-02-11 18:55:555cr.define('chrome.invalidations', function() {
[email protected]9b0d66b2014-02-20 13:16:016 /**
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]5cb5b182014-03-18 00:32:399 * log any invalidations ocurred prior to opening the about:invalidation
10 * page).
[email protected]9b0d66b2014-02-20 13:16:0111 */
12 var tableObjects = {};
[email protected]f17be762014-01-30 21:05:3813
[email protected]b7600272014-02-11 18:55:5514 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]b7600272014-02-11 18:55:5524 * @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]9b0d66b2014-02-20 13:16:0130 /**
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]b7600272014-02-11 18:55:5544
45 /**
[email protected]5cb5b182014-03-18 00:32:3946 * Shows the current state of the InvalidatorService.
[email protected]b7600272014-02-11 18:55:5547 * @param {string} newState The string to be displayed and logged.
48 */
[email protected]5cb5b182014-03-18 00:32:3949 function updateInvalidatorState(newState) {
[email protected]b7600272014-02-11 18:55:5550 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]063cbbb32014-02-27 01:46:2060 * @param {!Array.<!Object>} allInvalidations The array of ObjectId
[email protected]9b0d66b2014-02-20 13:16:0161 * that contains the invalidations received by the InvalidatorService.
[email protected]b7600272014-02-11 18:55:5562 */
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]9b0d66b2014-02-20 13:16:0176 var isInvalidation = true;
77 logToTable(inv, isInvalidation);
[email protected]b7600272014-02-11 18:55:5578 }
79 }
[email protected]9b0d66b2014-02-20 13:16:0180 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]063cbbb32014-02-27 01:46:2086 * @param {!Object} oId The ObjectId being added or updated.
87 * @param {!boolean} isInvaldation A flag that says that an invalidation
[email protected]9b0d66b2014-02-20 13:16:0188 * 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]063cbbb32014-02-27 01:46:20126 * 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]9b0d66b2014-02-20 13:16:01141 * Updates the table with the objects ids registered for invalidations
[email protected]9b0d66b2014-02-20 13:16:01142 * @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]b7600272014-02-11 18:55:55163 }
164
165 /**
[email protected]5cb5b182014-03-18 00:32:39166 * 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]b7600272014-02-11 18:55:55176 * ready to receive real-time notifications.
177 */
178 function onLoadWork() {
[email protected]5cb5b182014-03-18 00:32:39179 $('request-detailed-status').onclick = function() {
180 chrome.send('requestDetailedStatus');
181 };
[email protected]b7600272014-02-11 18:55:55182 chrome.send('doneLoading');
183 }
184
185 return {
[email protected]b7600272014-02-11 18:55:55186 logInvalidations: logInvalidations,
[email protected]063cbbb32014-02-27 01:46:20187 onLoadWork: onLoadWork,
[email protected]5cb5b182014-03-18 00:32:39188 updateDetailedStatus: updateDetailedStatus,
[email protected]063cbbb32014-02-27 01:46:20189 updateHandlers: updateHandlers,
190 updateIds: updateIds,
[email protected]5cb5b182014-03-18 00:32:39191 updateInvalidatorState: updateInvalidatorState,
[email protected]b7600272014-02-11 18:55:55192 };
193});
194
195document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork);