Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1413)

Side by Side Diff: chrome/browser/resources/google_now/cards.js

Issue 114533002: Chrome Now notificationGroups Storage Race Condition Fix (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * Show/hide trigger in a card. 8 * Show/hide trigger in a card.
9 * 9 *
10 * @typedef {{ 10 * @typedef {{
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 176
177 callback(uncombinedNotification, shouldShow && !shouldHide); 177 callback(uncombinedNotification, shouldShow && !shouldHide);
178 } 178 }
179 } 179 }
180 180
181 /** 181 /**
182 * Refreshes (shows/hides) the notification corresponding to the combined card 182 * Refreshes (shows/hides) the notification corresponding to the combined card
183 * based on the current time and show-hide intervals in the combined card. 183 * based on the current time and show-hide intervals in the combined card.
184 * @param {ChromeNotificationId} cardId Card ID. 184 * @param {ChromeNotificationId} cardId Card ID.
185 * @param {CombinedCard} combinedCard Combined cards with |cardId|. 185 * @param {CombinedCard} combinedCard Combined cards with |cardId|.
186 * @param {Object.<string, StoredNotificationGroup>} notificationGroups
187 * Map from group name to group information.
186 * @param {function(ReceivedNotification)=} onCardShown Optional parameter 188 * @param {function(ReceivedNotification)=} onCardShown Optional parameter
187 * called when each card is shown. 189 * called when each card is shown.
188 * @return {(NotificationDataEntry|undefined)} Notification data entry for 190 * @return {(NotificationDataEntry|undefined)} Notification data entry for
189 * this card. It's 'undefined' if the card's life is over. 191 * this card. It's 'undefined' if the card's life is over.
190 */ 192 */
191 function update(cardId, combinedCard, onCardShown) { 193 function update(cardId, combinedCard, notificationGroups, onCardShown) {
192 console.log('cardManager.update ' + JSON.stringify(combinedCard)); 194 console.log('cardManager.update ' + JSON.stringify(combinedCard));
193 195
194 chrome.alarms.clear(alarmPrefix + cardId); 196 chrome.alarms.clear(alarmPrefix + cardId);
195 var now = Date.now(); 197 var now = Date.now();
196 /** @type {?UncombinedNotification} */ 198 /** @type {?UncombinedNotification} */
197 var winningCard = null; 199 var winningCard = null;
198 // Next moment of time when winning notification selection algotithm can 200 // Next moment of time when winning notification selection algotithm can
199 // potentially return a different notification. 201 // potentially return a different notification.
200 /** @type {?number} */ 202 /** @type {?number} */
201 var nextEventTime = null; 203 var nextEventTime = null;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 249
248 return { 250 return {
249 actionUrls: winningActionUrls, 251 actionUrls: winningActionUrls,
250 timestamp: now, 252 timestamp: now,
251 combinedCard: combinedCard 253 combinedCard: combinedCard
252 }; 254 };
253 } else { 255 } else {
254 // If there are no more events, we are done with this card. Note that all 256 // If there are no more events, we are done with this card. Note that all
255 // received notifications have hideTime. 257 // received notifications have hideTime.
256 verify(!winningCard, 'No events left, but card is shown.'); 258 verify(!winningCard, 'No events left, but card is shown.');
257 clearCardFromGroups(cardId); 259 clearCardFromGroups(cardId, notificationGroups);
258 return undefined; 260 return undefined;
259 } 261 }
260 } 262 }
261 263
262 /** 264 /**
263 * Removes dismissed part of a card and refreshes the card. Returns remaining 265 * Removes dismissed part of a card and refreshes the card. Returns remaining
264 * dismissals for the combined card and updated notification data. 266 * dismissals for the combined card and updated notification data.
265 * @param {ChromeNotificationId} cardId Card ID. 267 * @param {ChromeNotificationId} cardId Card ID.
266 * @param {NotificationDataEntry} notificationData Stored notification entry 268 * @param {NotificationDataEntry} notificationData Stored notification entry
267 * for this card. 269 * for this card.
(...skipping 18 matching lines...) Expand all
286 notificationId: uncombinedCard.receivedNotification.notificationId, 288 notificationId: uncombinedCard.receivedNotification.notificationId,
287 parameters: uncombinedCard.receivedNotification.dismissal 289 parameters: uncombinedCard.receivedNotification.dismissal
288 }); 290 });
289 } else { 291 } else {
290 newCombinedCard.push(uncombinedCard); 292 newCombinedCard.push(uncombinedCard);
291 } 293 }
292 }); 294 });
293 295
294 return { 296 return {
295 dismissals: dismissals, 297 dismissals: dismissals,
296 notificationData: update(cardId, newCombinedCard) 298 notificationData: update(cardId, newCombinedCard)
vadimt 2013/12/13 18:20:54 Please pass notificationGroups here too.
robliao 2013/12/13 19:31:26 Done.
297 }; 299 };
298 } 300 }
299 301
300 /** 302 /**
301 * Removes card information from 'notificationGroups'. 303 * Removes card information from |notificationGroups|.
302 * @param {ChromeNotificationId} cardId Card ID. 304 * @param {ChromeNotificationId} cardId Card ID.
305 * @param {Object.<string, StoredNotificationGroup>} notificationGroups
306 * Map from group name to group information.
303 */ 307 */
304 function clearCardFromGroups(cardId) { 308 function clearCardFromGroups(cardId, notificationGroups) {
305 console.log('cardManager.clearCardFromGroups ' + cardId); 309 console.log('cardManager.clearCardFromGroups ' + cardId);
306 310 for (var groupName in notificationGroups) {
307 instrumented.storage.local.get('notificationGroups', function(items) { 311 var group = notificationGroups[groupName];
308 items = items || {}; 312 for (var i = 0; i != group.cards.length; ++i) {
309 /** @type {Object.<string, StoredNotificationGroup>} */ 313 if (group.cards[i].chromeNotificationId == cardId) {
310 items.notificationGroups = items.notificationGroups || {}; 314 group.cards.splice(i, 1);
311 315 break;
312 for (var groupName in items.notificationGroups) {
313 var group = items.notificationGroups[groupName];
314 for (var i = 0; i != group.cards.length; ++i) {
315 if (group.cards[i].chromeNotificationId == cardId) {
316 group.cards.splice(i, 1);
317 break;
318 }
319 } 316 }
320 } 317 }
321 318 }
322 chrome.storage.local.set(items);
323 });
324 } 319 }
325 320
326 instrumented.alarms.onAlarm.addListener(function(alarm) { 321 instrumented.alarms.onAlarm.addListener(function(alarm) {
327 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); 322 console.log('cardManager.onAlarm ' + JSON.stringify(alarm));
328 323
329 if (alarm.name.indexOf(alarmPrefix) == 0) { 324 if (alarm.name.indexOf(alarmPrefix) == 0) {
330 // Alarm to show the card. 325 // Alarm to show the card.
331 tasks.add(UPDATE_CARD_TASK_NAME, function() { 326 tasks.add(UPDATE_CARD_TASK_NAME, function() {
332 var cardId = alarm.name.substring(alarmPrefix.length); 327 var cardId = alarm.name.substring(alarmPrefix.length);
333 instrumented.storage.local.get('notificationsData', function(items) { 328 instrumented.storage.local.get('notificationsData', function(items) {
334 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); 329 console.log('cardManager.onAlarm.get ' + JSON.stringify(items));
335 items = items || {}; 330 items = items || {};
336 /** @type {Object.<string, NotificationDataEntry>} */ 331 /** @type {Object.<string, NotificationDataEntry>} */
337 items.notificationsData = items.notificationsData || {}; 332 items.notificationsData = items.notificationsData || {};
338 var combinedCard = 333 var combinedCard =
339 (items.notificationsData[cardId] && 334 (items.notificationsData[cardId] &&
340 items.notificationsData[cardId].combinedCard) || []; 335 items.notificationsData[cardId].combinedCard) || [];
341 336
342 var cardShownCallback = undefined; 337 var cardShownCallback = undefined;
343 if (localStorage['locationCardsShown'] < 338 if (localStorage['locationCardsShown'] <
344 LOCATION_CARDS_LINK_THRESHOLD) { 339 LOCATION_CARDS_LINK_THRESHOLD) {
345 cardShownCallback = countLocationCard; 340 cardShownCallback = countLocationCard;
346 } 341 }
347 342
348 items.notificationsData[cardId] = 343 items.notificationsData[cardId] =
349 update(cardId, combinedCard, cardShownCallback); 344 update(cardId, combinedCard, cardShownCallback);
vadimt 2013/12/13 18:20:54 Please pass notificationGroups here too.
robliao 2013/12/13 19:31:26 Done.
350 345
351 chrome.storage.local.set(items); 346 chrome.storage.local.set(items);
352 }); 347 });
353 }); 348 });
354 } 349 }
355 }); 350 });
356 351
357 return { 352 return {
358 update: update, 353 update: update,
359 onDismissal: onDismissal 354 onDismissal: onDismissal
360 }; 355 };
361 } 356 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698