blob: f6982cac488186654ca57fc8d1c926391600a6d4 [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
9 * log any invalidations ocurred prior to opening the about:invalidation page)
10 */
11 var tableObjects = {};
[email protected]f17be762014-01-30 21:05:3812
[email protected]b7600272014-02-11 18:55:5513 function quote(str) {
14 return '\"' + str + '\"';
15 }
16
17 function nowTimeString() {
18 return '[' + new Date().getTime() + '] ';
19 }
20
21 /**
22 * Appends a string to a textarea log.
23 *
24 * @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 /**
46 * Shows the current state of the InvalidatorService
47 *
48 * @param {string} newState The string to be displayed and logged.
49 */
50 function updateState(newState) {
51 var logMessage = nowTimeString() +
52 'Invalidations service state changed to ' + quote(newState);
53
54 appendToLog(logMessage);
55 $('invalidations-state').textContent = newState;
56 currentInvalidationState = newState;
57 }
58
59 /**
60 * Adds to the log the latest invalidations received
61 *
62 * @param {Array of Object} allInvalidations The array of ObjectId
[email protected]9b0d66b2014-02-20 13:16:0163 * that contains the invalidations received by the InvalidatorService.
[email protected]b7600272014-02-11 18:55:5564 */
65 function logInvalidations(allInvalidations) {
66 for (var i = 0; i < allInvalidations.length; i++) {
67 var inv = allInvalidations[i];
68 if (inv.hasOwnProperty('objectId')) {
69 var logMessage = nowTimeString() +
70 'Received Invalidation with type ' +
71 quote(inv.objectId.name) +
72 ' version ' +
73 quote((inv.isUnknownVersion ? 'Unknown' : inv.version)) +
74 ' with payload ' +
75 quote(inv.payload);
76
77 appendToLog(logMessage);
[email protected]9b0d66b2014-02-20 13:16:0178 var isInvalidation = true;
79 logToTable(inv, isInvalidation);
[email protected]b7600272014-02-11 18:55:5580 }
81 }
[email protected]9b0d66b2014-02-20 13:16:0182 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 }
126 }
127
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();
[email protected]b7600272014-02-11 18:55:55152 }
153
154 /**
155 * Function that notifies the Invalidator Logger that the UI is
156 * ready to receive real-time notifications.
157 */
158 function onLoadWork() {
159 chrome.send('doneLoading');
160 }
161
162 return {
163 updateState: updateState,
[email protected]9b0d66b2014-02-20 13:16:01164 updateIds: updateIds,
[email protected]b7600272014-02-11 18:55:55165 logInvalidations: logInvalidations,
166 onLoadWork: onLoadWork
167 };
168});
169
170document.addEventListener('DOMContentLoaded', chrome.invalidations.onLoadWork);