Chromium Code Reviews| Index: chrome/browser/resources/google_now/background.js |
| diff --git a/chrome/browser/resources/google_now/background.js b/chrome/browser/resources/google_now/background.js |
| index da293eaaf19e29c258896a91f6a69ccff6a4a0c0..98651bab5f283aa9d0befe0329f20336c1cd22be 100644 |
| --- a/chrome/browser/resources/google_now/background.js |
| +++ b/chrome/browser/resources/google_now/background.js |
| @@ -20,7 +20,6 @@ |
| // TODO(vadimt): Decide what to do in incognito mode. |
| // TODO(vadimt): Figure out the final values of the constants. |
| // TODO(vadimt): Remove 'console' calls. |
| -// TODO(vadimt): Consider sending JS stacks for malformed server responses. |
| /** |
| * Standard response code for successful HTTP requests. This is the only success |
| @@ -81,6 +80,7 @@ var UPDATE_CARDS_TASK_NAME = 'update-cards'; |
| var DISMISS_CARD_TASK_NAME = 'dismiss-card'; |
| var RETRY_DISMISS_TASK_NAME = 'retry-dismiss'; |
| var STATE_CHANGED_TASK_NAME = 'state-changed'; |
| +var SHOW_ON_START_TASK_NAME = 'show-cards-on-start'; |
| var LOCATION_WATCH_NAME = 'location-watch'; |
| @@ -317,8 +317,6 @@ function showNotificationCards(cards) { |
| previousVersion); |
| } |
| - recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); |
| - |
| chrome.storage.local.set({ |
| notificationsData: newNotificationsData, |
| recentDismissals: updatedRecentDismissals |
| @@ -447,6 +445,21 @@ function scheduleNextPoll(groups) { |
| } |
| /** |
| + * Merges notification groups into a set of Chrome notifications and shows them. |
| + * @param {Object.<string, StorageGroup>} notificationGroups Map from group name |
| + * to group information. |
| + */ |
| +function mergeAndShowNotificationCards(notificationGroups) { |
| + // Merge cards from all groups into one set. |
|
rgustafson
2013/10/15 21:14:33
nit: comment not really necessary. If you want mor
vadimt
2013/10/15 21:28:03
Done.
|
| + var mergedCards = {}; |
| + |
| + for (var groupName in notificationGroups) |
| + mergeGroup(mergedCards, notificationGroups[groupName]); |
| + |
| + showNotificationCards(mergedCards); |
| +} |
| + |
| +/** |
| * Parses JSON response from the notification server, shows notifications and |
| * schedules next update. |
| * @param {string} response Server response. |
| @@ -455,13 +468,13 @@ function parseAndShowNotificationCards(response) { |
| console.log('parseAndShowNotificationCards ' + response); |
| var parsedResponse = JSON.parse(response); |
| - var groups = parsedResponse.groups; |
| + var receivedGroups = parsedResponse.groups; |
| // Populate groups with corresponding cards. |
| if (parsedResponse.notifications) { |
| for (var i = 0; i != parsedResponse.notifications.length; ++i) { |
| var card = parsedResponse.notifications[i]; |
| - var group = groups[card.groupName]; |
| + var group = receivedGroups[card.groupName]; |
| group.cards = group.cards || []; |
| group.cards.push(card); |
| } |
| @@ -474,12 +487,9 @@ function parseAndShowNotificationCards(response) { |
| var now = Date.now(); |
| - // Build updated set of groups and merge cards from all groups into one set. |
| - var updatedGroups = {}; |
|
rgustafson
2013/10/15 21:14:33
For updatedGroups -> items.notificationGroups
Wha
vadimt
2013/10/15 21:28:03
Ah, looks like I forgot the protocol...
So, the se
rgustafson
2013/10/15 21:43:41
This is the joy of having extensibility which ther
vadimt
2013/10/15 22:05:36
Thanks; you've found a regression; I'm fixing it.
|
| - var mergedCards = {}; |
| - |
| - for (var groupName in groups) { |
| - var receivedGroup = groups[groupName]; |
| + // Build updated set of groups. |
| + for (var groupName in receivedGroups) { |
| + var receivedGroup = receivedGroups[groupName]; |
| var storageGroup = items.notificationGroups[groupName] || { |
| cards: [], |
| cardsTimestamp: undefined, |
| @@ -501,16 +511,13 @@ function parseAndShowNotificationCards(response) { |
| now + receivedGroup.nextPollSeconds * MS_IN_SECOND; |
| } |
| - updatedGroups[groupName] = storageGroup; |
| - |
| - mergeGroup(mergedCards, storageGroup); |
| + items.notificationGroups[groupName] = storageGroup; |
| } |
| - scheduleNextPoll(updatedGroups); |
| - |
| - chrome.storage.local.set({notificationGroups: updatedGroups}); |
| - |
| - showNotificationCards(mergedCards); |
| + scheduleNextPoll(items.notificationGroups); |
| + chrome.storage.local.set({notificationGroups: items.notificationGroups}); |
| + mergeAndShowNotificationCards(items.notificationGroups); |
| + recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); |
| }); |
| } |
| @@ -876,12 +883,6 @@ function stopPollingCards() { |
| */ |
| function initialize() { |
| recordEvent(GoogleNowEvent.EXTENSION_START); |
| - |
| - // Alarms persist across chrome restarts. This is undesirable since it |
| - // prevents us from starting up everything (alarms are a heuristic to |
| - // determine if we are already running). To mitigate this, we will |
| - // shut everything down on initialize before starting everything up. |
| - stopPollingCards(); |
| onStateChange(); |
| } |
| @@ -1085,6 +1086,20 @@ instrumented.runtime.onInstalled.addListener(function(details) { |
| instrumented.runtime.onStartup.addListener(function() { |
| console.log('onStartup'); |
| + |
| + // Show notifications received by earlier polls. Doing this as early as |
| + // possible to reduce latency of showing first notifications. This mimics how |
| + // persistent notifications will work. |
| + tasks.add(SHOW_ON_START_TASK_NAME, function() { |
| + instrumented.storage.local.get('notificationGroups', function(items) { |
| + console.log('onStartup-get ' + JSON.stringify(items)); |
| + items = items || {}; |
| + items.notificationGroups = items.notificationGroups || {}; |
| + |
| + mergeAndShowNotificationCards(items.notificationGroups); |
| + }); |
| + }); |
| + |
| initialize(); |
| }); |