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 98651bab5f283aa9d0befe0329f20336c1cd22be..aa5bd311d94ca283c049743196f1895231164bf8 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); |
@@ -522,18 +529,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 refreshed. |
*/ |
-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) { |
rgustafson
2013/10/15 20:56:54
But the signed-in condition isn't missing, it's ri
vadimt
2013/10/15 21:14:09
I'm not saying I'm changing the behavior. The chec
rgustafson
2013/10/16 17:14:32
There are two problems here:
(1) Duplicate code be
vadimt
2013/10/16 20:10:20
I've changed implementation to use updateCardsAtte
|
+ 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)); |
@@ -542,33 +581,19 @@ function requestNotificationCards(position) { |
var requestParameters = '?timeZoneOffsetMs=' + |
rgustafson
2013/10/15 20:56:54
Remove this. It was moved.
vadimt
2013/10/15 21:14:09
Done.
|
(-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); |
}); |
} |
@@ -1152,3 +1177,21 @@ instrumented.omnibox.onInputEntered.addListener(function(text) { |
localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
initialize(); |
}); |
+ |
+instrumented.pushMessaging.onMessage.addListener(function(message) { |
+ console.log('pushMessaging.onMessage ' + JSON.stringify(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) { |
rgustafson
2013/10/15 17:57:51
What are you expecting in the payload and why does
vadimt
2013/10/15 19:41:43
When the extension start for the first time, you g
rgustafson
2013/10/15 20:56:54
Some comments of that explanation, at least what y
vadimt
2013/10/15 21:14:09
Done.
|
+ chrome.storage.local.set({lastPollNowPayload: message.payload}); |
+ |
+ authenticationManager.isSignedIn(function(token) { |
rgustafson
2013/10/15 17:57:51
You can receive push messages without being signed
vadimt
2013/10/15 19:41:43
It's more for readability and solidity of the code
|
+ if (token) |
+ requestNotificationGroups([]); |
rgustafson
2013/10/16 17:14:32
Why isn't this (and the surrounding sign in check)
vadimt
2013/10/16 20:10:20
The fact that we received a push notification does
|
+ }); |
+ } |
+ }); |
+ }); |
+ } |
+}); |