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

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

Issue 114533002: Chrome Now notificationGroups Storage Race Condition Fix (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CR Feedback 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
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/cards.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 * @fileoverview The event page for Google Now for Chrome implementation. 8 * @fileoverview The event page for Google Now for Chrome implementation.
9 * The Google Now event page gets Google Now cards from the server and shows 9 * The Google Now event page gets Google Now cards from the server and shows
10 * them as Chrome notifications. 10 * them as Chrome notifications.
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 originalOnLoadEnd(event); 286 originalOnLoadEnd(event);
287 } 287 }
288 }); 288 });
289 289
290 callbackBoolean(true); 290 callbackBoolean(true);
291 }); 291 });
292 } 292 }
293 293
294 /** 294 /**
295 * Shows parsed and combined cards as notifications. 295 * Shows parsed and combined cards as notifications.
296 * @param {Object.<string, StoredNotificationGroup>} notificationGroups Map from
297 * group name to group information.
296 * @param {Object.<ChromeNotificationId, CombinedCard>} cards Map from 298 * @param {Object.<ChromeNotificationId, CombinedCard>} cards Map from
297 * chromeNotificationId to the combined card, containing cards to show. 299 * chromeNotificationId to the combined card, containing cards to show.
300 * @param {function()} onSuccess Called on success.
298 * @param {function(ReceivedNotification)=} onCardShown Optional parameter 301 * @param {function(ReceivedNotification)=} onCardShown Optional parameter
299 * called when each card is shown. 302 * called when each card is shown.
300 */ 303 */
301 function showNotificationCards(cards, onCardShown) { 304 function showNotificationCards(
305 notificationGroups, cards, onSuccess, onCardShown) {
302 console.log('showNotificationCards ' + JSON.stringify(cards)); 306 console.log('showNotificationCards ' + JSON.stringify(cards));
303 307
304 instrumented.notifications.getAll(function(notifications) { 308 instrumented.notifications.getAll(function(notifications) {
305 console.log('showNotificationCards-getAll ' + 309 console.log('showNotificationCards-getAll ' +
306 JSON.stringify(notifications)); 310 JSON.stringify(notifications));
307 notifications = notifications || {}; 311 notifications = notifications || {};
308 312
309 // Mark notifications that didn't receive an update as having received 313 // Mark notifications that didn't receive an update as having received
310 // an empty update. 314 // an empty update.
311 for (var chromeNotificationId in notifications) { 315 for (var chromeNotificationId in notifications) {
312 cards[chromeNotificationId] = cards[chromeNotificationId] || []; 316 cards[chromeNotificationId] = cards[chromeNotificationId] || [];
313 } 317 }
314 318
315 /** @type {Object.<string, NotificationDataEntry>} */ 319 /** @type {Object.<string, NotificationDataEntry>} */
316 var notificationsData = {}; 320 var notificationsData = {};
317 321
318 // Create/update/delete notifications. 322 // Create/update/delete notifications.
319 for (var chromeNotificationId in cards) { 323 for (var chromeNotificationId in cards) {
320 notificationsData[chromeNotificationId] = cardSet.update( 324 notificationsData[chromeNotificationId] = cardSet.update(
321 chromeNotificationId, 325 chromeNotificationId,
322 cards[chromeNotificationId], 326 cards[chromeNotificationId],
327 notificationGroups,
323 onCardShown); 328 onCardShown);
324 } 329 }
325 chrome.storage.local.set({notificationsData: notificationsData}); 330 chrome.storage.local.set({notificationsData: notificationsData});
331 onSuccess();
326 }); 332 });
327 } 333 }
328 334
329 /** 335 /**
330 * Removes all cards and card state on Google Now close down. 336 * Removes all cards and card state on Google Now close down.
331 * For example, this occurs when the geolocation preference is unchecked in the 337 * For example, this occurs when the geolocation preference is unchecked in the
332 * content settings. 338 * content settings.
333 */ 339 */
334 function removeAllCards() { 340 function removeAllCards() {
335 console.log('removeAllCards'); 341 console.log('removeAllCards');
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 updateCardsAttempts.start(optinPollPeriodSeconds); 416 updateCardsAttempts.start(optinPollPeriodSeconds);
411 }); 417 });
412 } 418 }
413 } 419 }
414 420
415 /** 421 /**
416 * Combines notification groups into a set of Chrome notifications and shows 422 * Combines notification groups into a set of Chrome notifications and shows
417 * them. 423 * them.
418 * @param {Object.<string, StoredNotificationGroup>} notificationGroups Map from 424 * @param {Object.<string, StoredNotificationGroup>} notificationGroups Map from
419 * group name to group information. 425 * group name to group information.
426 * @param {function()} onSuccess Called on success.
420 * @param {function(ReceivedNotification)=} onCardShown Optional parameter 427 * @param {function(ReceivedNotification)=} onCardShown Optional parameter
421 * called when each card is shown. 428 * called when each card is shown.
422 */ 429 */
423 function combineAndShowNotificationCards(notificationGroups, onCardShown) { 430 function combineAndShowNotificationCards(
431 notificationGroups, onSuccess, onCardShown) {
424 console.log('combineAndShowNotificationCards ' + 432 console.log('combineAndShowNotificationCards ' +
425 JSON.stringify(notificationGroups)); 433 JSON.stringify(notificationGroups));
426 /** @type {Object.<ChromeNotificationId, CombinedCard>} */ 434 /** @type {Object.<ChromeNotificationId, CombinedCard>} */
427 var combinedCards = {}; 435 var combinedCards = {};
428 436
429 for (var groupName in notificationGroups) 437 for (var groupName in notificationGroups)
430 combineGroup(combinedCards, notificationGroups[groupName]); 438 combineGroup(combinedCards, notificationGroups[groupName]);
431 439
432 showNotificationCards(combinedCards, onCardShown); 440 showNotificationCards(
441 notificationGroups, combinedCards, onSuccess, onCardShown);
433 } 442 }
434 443
435 /** 444 /**
436 * Parses JSON response from the notification server, shows notifications and 445 * Parses JSON response from the notification server, shows notifications and
437 * schedules next update. 446 * schedules next update.
438 * @param {string} response Server response. 447 * @param {string} response Server response.
439 * @param {function(ReceivedNotification)=} onCardShown Optional parameter 448 * @param {function(ReceivedNotification)=} onCardShown Optional parameter
440 * called when each card is shown. 449 * called when each card is shown.
441 */ 450 */
442 function parseAndShowNotificationCards(response, onCardShown) { 451 function parseAndShowNotificationCards(response, onCardShown) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 // cards updates. 533 // cards updates.
525 if (receivedGroup.nextPollSeconds !== undefined) { 534 if (receivedGroup.nextPollSeconds !== undefined) {
526 storedGroup.nextPollTime = 535 storedGroup.nextPollTime =
527 now + receivedGroup.nextPollSeconds * MS_IN_SECOND; 536 now + receivedGroup.nextPollSeconds * MS_IN_SECOND;
528 } 537 }
529 538
530 updatedGroups[groupName] = storedGroup; 539 updatedGroups[groupName] = storedGroup;
531 } 540 }
532 541
533 scheduleNextPoll(updatedGroups, !parsedResponse.googleNowDisabled); 542 scheduleNextPoll(updatedGroups, !parsedResponse.googleNowDisabled);
534 chrome.storage.local.set({ 543 combineAndShowNotificationCards(updatedGroups,
vadimt 2013/12/13 20:07:40 nit: please start "updatedGroups," from new line
robliao 2013/12/13 20:38:17 Done.
535 notificationGroups: updatedGroups, 544 function() {
536 recentDismissals: updatedRecentDismissals 545 chrome.storage.local.set({
537 }); 546 notificationGroups: updatedGroups,
538 combineAndShowNotificationCards(updatedGroups, onCardShown); 547 recentDismissals: updatedRecentDismissals
539 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); 548 });
549 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS);
550 },
551 onCardShown);
540 }); 552 });
541 } 553 }
542 554
543 /** 555 /**
544 * Update Location Cards Shown Count. 556 * Update Location Cards Shown Count.
545 * @param {ReceivedNotification} receivedNotification Notification as it was 557 * @param {ReceivedNotification} receivedNotification Notification as it was
546 * received from the server. 558 * received from the server.
547 */ 559 */
548 function countLocationCard(receivedNotification) { 560 function countLocationCard(receivedNotification) {
549 if (receivedNotification.locationBased) { 561 if (receivedNotification.locationBased) {
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 if (!byUser) 910 if (!byUser)
899 return; 911 return;
900 912
901 // At this point we are guaranteed that the notification is a now card. 913 // At this point we are guaranteed that the notification is a now card.
902 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed'); 914 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed');
903 915
904 tasks.add(DISMISS_CARD_TASK_NAME, function() { 916 tasks.add(DISMISS_CARD_TASK_NAME, function() {
905 dismissalAttempts.start(); 917 dismissalAttempts.start();
906 918
907 instrumented.storage.local.get( 919 instrumented.storage.local.get(
908 ['pendingDismissals', 'notificationsData'], function(items) { 920 ['pendingDismissals', 'notificationsData', 'notificationGroups'],
909 items = items || {}; 921 function(items) {
910 /** @type {Array.<PendingDismissal>} */ 922 items = items || {};
911 items.pendingDismissals = items.pendingDismissals || []; 923 /** @type {Array.<PendingDismissal>} */
912 /** @type {Object.<string, NotificationDataEntry>} */ 924 items.pendingDismissals = items.pendingDismissals || [];
913 items.notificationsData = items.notificationsData || {}; 925 /** @type {Object.<string, NotificationDataEntry>} */
926 items.notificationsData = items.notificationsData || {};
927 /** @type {Object.<string, StoredNotificationGroup>} */
928 items.notificationGroups = items.notificationGroups || {};
914 929
915 /** @type {NotificationDataEntry} */ 930 /** @type {NotificationDataEntry} */
916 var notificationData = items.notificationsData[chromeNotificationId] || { 931 var notificationData =
917 timestamp: Date.now(), 932 items.notificationsData[chromeNotificationId] ||
918 combinedCard: [] 933 {
919 }; 934 timestamp: Date.now(),
935 combinedCard: []
936 };
920 937
921 var dismissalResult = 938 var dismissalResult =
922 cardSet.onDismissal(chromeNotificationId, notificationData); 939 cardSet.onDismissal(
940 chromeNotificationId,
941 notificationData,
942 items.notificationGroups);
923 943
924 for (var i = 0; i < dismissalResult.dismissals.length; i++) { 944 for (var i = 0; i < dismissalResult.dismissals.length; i++) {
925 /** @type {PendingDismissal} */ 945 /** @type {PendingDismissal} */
926 var dismissal = { 946 var dismissal = {
927 chromeNotificationId: chromeNotificationId, 947 chromeNotificationId: chromeNotificationId,
928 time: Date.now(), 948 time: Date.now(),
929 dismissalData: dismissalResult.dismissals[i] 949 dismissalData: dismissalResult.dismissals[i]
930 }; 950 };
931 items.pendingDismissals.push(dismissal); 951 items.pendingDismissals.push(dismissal);
932 } 952 }
933 953
934 items.notificationsData[chromeNotificationId] = 954 items.notificationsData[chromeNotificationId] =
935 dismissalResult.notificationData; 955 dismissalResult.notificationData;
936 956
937 chrome.storage.local.set({ 957 chrome.storage.local.set(items);
938 pendingDismissals: items.pendingDismissals,
939 notificationsData: items.notificationsData
940 });
941 958
942 processPendingDismissals(function(success) {}); 959 processPendingDismissals(function(success) {});
943 }); 960 });
944 }); 961 });
945 } 962 }
946 963
947 /** 964 /**
948 * Initializes the polling system to start monitoring location and fetching 965 * Initializes the polling system to start monitoring location and fetching
949 * cards. 966 * cards.
950 */ 967 */
951 function startPollingCards() { 968 function startPollingCards() {
952 // Create an update timer for a case when for some reason location request 969 // Create an update timer for a case when for some reason location request
953 // gets stuck. 970 // gets stuck.
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1118 // Show notifications received by earlier polls. Doing this as early as 1135 // Show notifications received by earlier polls. Doing this as early as
1119 // possible to reduce latency of showing first notifications. This mimics how 1136 // possible to reduce latency of showing first notifications. This mimics how
1120 // persistent notifications will work. 1137 // persistent notifications will work.
1121 tasks.add(SHOW_ON_START_TASK_NAME, function() { 1138 tasks.add(SHOW_ON_START_TASK_NAME, function() {
1122 instrumented.storage.local.get('notificationGroups', function(items) { 1139 instrumented.storage.local.get('notificationGroups', function(items) {
1123 console.log('onStartup-get ' + JSON.stringify(items)); 1140 console.log('onStartup-get ' + JSON.stringify(items));
1124 items = items || {}; 1141 items = items || {};
1125 /** @type {Object.<string, StoredNotificationGroup>} */ 1142 /** @type {Object.<string, StoredNotificationGroup>} */
1126 items.notificationGroups = items.notificationGroups || {}; 1143 items.notificationGroups = items.notificationGroups || {};
1127 1144
1128 combineAndShowNotificationCards(items.notificationGroups); 1145 combineAndShowNotificationCards(items.notificationGroups, function() {});
vadimt 2013/12/13 20:07:40 We need to store notificationGroups here too, in c
robliao 2013/12/13 20:38:17 Ah. I for some reason thought we didn't need this.
1129 }); 1146 });
1130 }); 1147 });
1131 1148
1132 initialize(); 1149 initialize();
1133 }); 1150 });
1134 1151
1135 instrumented. 1152 instrumented.
1136 preferencesPrivate. 1153 preferencesPrivate.
1137 googleGeolocationAccessEnabled. 1154 googleGeolocationAccessEnabled.
1138 onChange. 1155 onChange.
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1216 lastPollNowPayloads: items.lastPollNowPayloads, 1233 lastPollNowPayloads: items.lastPollNowPayloads,
1217 notificationGroups: items.notificationGroups 1234 notificationGroups: items.notificationGroups
1218 }); 1235 });
1219 1236
1220 updateNotificationsCards(); 1237 updateNotificationsCards();
1221 } 1238 }
1222 }); 1239 });
1223 }); 1240 });
1224 } 1241 }
1225 }); 1242 });
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/cards.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698