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 188a3a92cd821664086afb7c67a9a1d48d2eb8ae..84135c8d582e3f425e4584899d7d631e94ff79a5 100644 |
| --- a/chrome/browser/resources/google_now/background.js |
| +++ b/chrome/browser/resources/google_now/background.js |
| @@ -74,6 +74,11 @@ var MAXIMUM_DISMISSAL_AGE_MS = 24 * 60 * 60 * 1000; // 1 day |
| var DISMISS_RETENTION_TIME_MS = 20 * 60 * 1000; // 20 minutes |
| /** |
| + * Default period for checking whether the user is opted in to Google Now. |
| + */ |
| +var DEFAULT_OPTIN_CHECK_PERIOD_SECONDS = 60 * 60 * 24 * 7; // 1 week |
| + |
| +/** |
| * Names for tasks that can be created by the extension. |
| */ |
| var UPDATE_CARDS_TASK_NAME = 'update-cards'; |
| @@ -422,7 +427,8 @@ function mergeGroup(mergedCards, storageGroup) { |
| /** |
| * Schedules next cards poll. |
| * @param {Object.<string, StorageGroup>} groups Map from group name to group |
| - * information. |
| + * information. If this map is empty, the user is not opted in to Google |
|
skare_
2013/10/31 00:49:21
consider making signed-in state an explicit param
vadimt
2013/10/31 14:36:00
I thought it would make sense as is to keep the ex
rgustafson
2013/10/31 18:37:59
It's important to handle the possibility of no gro
vadimt
2013/10/31 19:59:57
Added parameter. Restored old behavior for opted-i
|
| + * Now. |
| */ |
| function scheduleNextPoll(groups) { |
| var nextPollTime = null; |
| @@ -435,13 +441,20 @@ function scheduleNextPoll(groups) { |
| } |
| } |
| - // At least one of the groups must have nextPollTime. |
| - verify(nextPollTime != null, 'scheduleNextPoll: nextPollTime is null'); |
| - |
| - var nextPollDelaySeconds = Math.max( |
| - (nextPollTime - Date.now()) / MS_IN_SECOND, |
| - MINIMUM_POLLING_PERIOD_SECONDS); |
| - updateCardsAttempts.start(nextPollDelaySeconds); |
| + if (nextPollTime != null) { |
| + var nextPollDelaySeconds = Math.max( |
| + (nextPollTime - Date.now()) / MS_IN_SECOND, |
| + MINIMUM_POLLING_PERIOD_SECONDS); |
| + updateCardsAttempts.start(nextPollDelaySeconds); |
| + } else { |
| + instrumented.metricsPrivate.getVariationParams( |
| + 'GoogleNow', function(params) { |
| + var optinPollPeriodSeconds = |
| + parseInt(params && params.optinPollPeriodSeconds, 10) || |
| + DEFAULT_OPTIN_CHECK_PERIOD_SECONDS; |
| + updateCardsAttempts.start(optinPollPeriodSeconds); |
| + }); |
| + } |
| } |
| /** |
| @@ -467,6 +480,13 @@ function parseAndShowNotificationCards(response) { |
| console.log('parseAndShowNotificationCards ' + response); |
| var parsedResponse = JSON.parse(response); |
| + if (parsedResponse.googleNowDisabled) { |
| + chrome.storage.local.set({googleNowEnabled: false}); |
| + // TODO(vadimt): Remove the line below once the server stops sending groups |
| + // with 'googleNowDisabled' responses. |
| + parsedResponse.groups = {}; |
| + } |
| + |
| var receivedGroups = parsedResponse.groups; |
| // Populate groups with corresponding cards. |
| @@ -567,13 +587,44 @@ function requestNotificationGroups(groupNames) { |
| } |
| /** |
| + * Requests the account opted-in state from the server. |
| + * @param {function()} successCallback Function that will be called if |
|
skare_
2013/10/31 00:49:21
maybe add param to make it successCallback(bool is
vadimt
2013/10/31 14:36:00
Done.
|
| + * opted-in state is 'true'. |
| + */ |
| +function requestOptedIn(successCallback) { |
| + console.log('requestOptedIn from ' + NOTIFICATION_CARDS_URL); |
| + |
| + var request = buildServerRequest('GET', 'settings/optin'); |
| + |
| + request.onloadend = function(event) { |
| + console.log( |
| + 'requestOptedIn-onloadend ' + request.status + ' ' + request.response); |
| + if (request.status == HTTP_OK) { |
| + var parsedResponse = JSON.parse(request.response); |
| + if (parsedResponse.value) { |
| + chrome.storage.local.set({googleNowEnabled: true}); |
| + successCallback(); |
| + } else { |
| + scheduleNextPoll({}); |
| + } |
| + } |
| + }; |
| + |
| + 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) { |
| + instrumented.storage.local.get( |
| + ['notificationGroups', 'googleNowEnabled'], function(items) { |
| console.log('requestNotificationCards-storage-get ' + |
| JSON.stringify(items)); |
| items = items || {}; |
| @@ -590,7 +641,13 @@ function requestNotificationCards(position) { |
| } |
| } |
| - requestNotificationGroups(groupsToRequest); |
| + if (items.googleNowEnabled) { |
| + requestNotificationGroups(groupsToRequest); |
| + } else { |
| + requestOptedIn(function() { |
| + requestNotificationGroups(groupsToRequest); |
| + }); |
| + } |
| }); |
| } |
| @@ -1082,7 +1139,8 @@ instrumented.pushMessaging.onMessage.addListener(function(message) { |
| console.log('pushMessaging.onMessage ' + JSON.stringify(message)); |
| if (message.payload.indexOf('REQUEST_CARDS') == 0) { |
| tasks.add(ON_PUSH_MESSAGE_START_TASK_NAME, function() { |
| - instrumented.storage.local.get('lastPollNowPayloads', function(items) { |
| + instrumented.storage.local.get( |
| + ['lastPollNowPayloads', 'notificationGroups'], function(items) { |
| // If storage.get fails, it's safer to do nothing, preventing polling |
| // the server when the payload really didn't change. |
| if (!items) |
| @@ -1094,13 +1152,19 @@ instrumented.pushMessaging.onMessage.addListener(function(message) { |
| if (items.lastPollNowPayloads[message.subchannelId] != |
| message.payload) { |
| items.lastPollNowPayloads[message.subchannelId] = message.payload; |
| - chrome.storage.local.set( |
| - {lastPollNowPayloads: items.lastPollNowPayloads}); |
| - updateCardsAttempts.isRunning(function(running) { |
| - if (running) |
| - requestNotificationGroups(['PUSH' + message.subchannelId]); |
| + items.notificationGroups = items.notificationGroups || {}; |
| + items.notificationGroups['PUSH' + message.subchannelId] = { |
| + cards: [], |
| + nextPollTime: Date.now() |
| + }; |
| + |
| + chrome.storage.local.set({ |
| + lastPollNowPayloads: items.lastPollNowPayloads, |
| + notificationGroups: items.notificationGroups |
| }); |
| + |
| + updateNotificationsCards(); |
| } |
| }); |
| }); |