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 4c6732f8ff43ddb42ae042ce84f247300377f903..25c7eb4836b3813ab4c8bf443ccb94bace3ea159 100644 |
| --- a/chrome/browser/resources/google_now/background.js |
| +++ b/chrome/browser/resources/google_now/background.js |
| @@ -81,12 +81,18 @@ 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 ON_PUSH_MESSAGE_START_TASK_NAME = 'on-push-message'; |
| var LOCATION_WATCH_NAME = 'location-watch'; |
| var WELCOME_TOAST_NOTIFICATION_ID = 'enable-now-toast'; |
| /** |
| + * Chrome push messaging subchannel for messages causing an immediate poll. |
| + */ |
| +var SUBCHANNEL_ID_POLL_NOW = 0; |
| + |
| +/** |
| * The indices of the buttons that are displayed on the welcome toast. |
| * @enum {number} |
| */ |
| @@ -168,6 +174,7 @@ wrapper.instrumentChromeApiFunction( |
| 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', |
| 0); |
| wrapper.instrumentChromeApiFunction('permissions.contains', 1); |
| +wrapper.instrumentChromeApiFunction('pushMessaging.onMessage.addListener', 0); |
| wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); |
| wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); |
| wrapper.instrumentChromeApiFunction('tabs.create', 1); |
| @@ -527,18 +534,50 @@ function parseAndShowNotificationCards(response) { |
| } |
| /** |
| - * Requests notification cards from the server. |
| - * @param {Location} position Location of this computer. |
| + * Requests notification cards from the server for specified groups. |
| + * @param {Array.<string>} groupNames Names of groups that need to be regreshed. |
| */ |
| -function requestNotificationCards(position) { |
| - console.log('requestNotificationCards ' + JSON.stringify(position) + |
| - ' from ' + NOTIFICATION_CARDS_URL); |
| +function requestNotificationGroups(groupNames) { |
| + console.log('requestNotificationGroups from ' + NOTIFICATION_CARDS_URL + |
| + ', groupNames=' + JSON.stringify(groupNames)); |
| if (!NOTIFICATION_CARDS_URL) |
| return; |
| recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_TOTAL); |
| + var requestParameters = '?timeZoneOffsetMs=' + |
| + (-new Date().getTimezoneOffset() * MS_IN_MINUTE); |
| + |
| + groupNames.forEach(function(groupName) { |
| + requestParameters += ('&requestTypes=' + groupName); |
| + }); |
| + |
| + console.log('requestNotificationGroups: request=' + requestParameters); |
| + |
| + var request = buildServerRequest('GET', 'notifications' + requestParameters); |
| + |
| + request.onloadend = function(event) { |
| + console.log('requestNotificationGroups-onloadend ' + request.status); |
| + if (request.status == HTTP_OK) { |
| + recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS); |
| + parseAndShowNotificationCards(request.response); |
| + } |
| + }; |
| + |
| + setAuthorization(request, function(success) { |
| + if (success) |
| + request.send(); |
| + }); |
| +} |
| + |
| +/** |
| + * Requests notification cards from the server. |
| + * @param {Location} position Location of this computer. |
| + */ |
| +function requestNotificationCards(position) { |
| + console.log('requestNotificationCards ' + JSON.stringify(position)); |
| + |
| instrumented.storage.local.get('notificationGroups', function(items) { |
| console.log('requestNotificationCards-storage-get ' + |
| JSON.stringify(items)); |
| @@ -547,33 +586,19 @@ function requestNotificationCards(position) { |
| var requestParameters = '?timeZoneOffsetMs=' + |
| (-new Date().getTimezoneOffset() * MS_IN_MINUTE); |
| + var groupsToRequest = []; |
| + |
| if (items.notificationGroups) { |
| var now = Date.now(); |
| for (var groupName in items.notificationGroups) { |
| var group = items.notificationGroups[groupName]; |
| if (group.nextPollTime <= now) |
| - requestParameters += ('&requestTypes=' + groupName); |
| + groupsToRequest.push(groupName); |
| } |
| } |
| - console.log('requestNotificationCards: request=' + requestParameters); |
| - |
| - var request = buildServerRequest('GET', |
| - 'notifications' + requestParameters); |
| - |
| - request.onloadend = function(event) { |
| - console.log('requestNotificationCards-onloadend ' + request.status); |
| - if (request.status == HTTP_OK) { |
| - recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS); |
| - parseAndShowNotificationCards(request.response); |
| - } |
| - }; |
| - |
| - setAuthorization(request, function(success) { |
| - if (success) |
| - request.send(); |
| - }); |
| + requestNotificationGroups(groupsToRequest); |
| }); |
| } |
| @@ -1147,3 +1172,21 @@ instrumented.omnibox.onInputEntered.addListener(function(text) { |
| localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
| initialize(); |
| }); |
| + |
| +instrumented.pushMessaging.onMessage.addListener(function(message) { |
| + if (message.subchannelId == SUBCHANNEL_ID_POLL_NOW && message.payload) { |
| + tasks.add(ON_PUSH_MESSAGE_START_TASK_NAME, function() { |
| + instrumented.storage.local.get('lastPollNowPayload', function(items) { |
| + if (items && items.lastPollNowPayload != message.payload) { |
| + chrome.storage.local.set({lastPollNowPayload: message.payload}); |
| + |
| + authenticationManager.isSignedIn(function(token) { |
| + if (token) |
| + requestNotificationGroups([]); |
| + }); |
| + } |
| + }); |
| + }); |
| + } |
| + console.log(JSON.stringify(message)); |
|
robliao
2013/10/15 00:04:55
This should either be removed or annotated with wh
vadimt
2013/10/15 01:25:47
Done.
|
| +}); |