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 3c4bf8f5c5e95148f1d39da830bb396fc8de8779..0fdbd6724c10e7292d530e9fbf3dc12adacdf8ec 100644 |
| --- a/chrome/browser/resources/google_now/background.js |
| +++ b/chrome/browser/resources/google_now/background.js |
| @@ -53,22 +53,21 @@ var INITIAL_POLLING_PERIOD_SECONDS = 5 * 60; // 5 minutes |
| */ |
| var MAXIMUM_POLLING_PERIOD_SECONDS = 60 * 60; // 1 hour |
| -var UPDATE_NOTIFICATIONS_ALARM_NAME = 'UPDATE'; |
| - |
| /** |
| - * Period for retrying the server request for dismissing cards. |
| + * Initial period for retrying the server request for dismissing cards. |
| */ |
| -var RETRY_DISMISS_PERIOD_SECONDS = 60; // 1 minute |
| +var INITIAL_RETRY_DISMISS_PERIOD_SECONDS = 60; // 1 minute |
| -var RETRY_DISMISS_ALARM_NAME = 'RETRY_DISMISS'; |
| +/** |
| + * Maximum period for retrying the server request for dismissing cards. |
| + */ |
| +var MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS = 60 * 60; // 1 hour |
| /** |
| * Time we keep dismissals after successful server dismiss requests. |
| */ |
| var DISMISS_RETENTION_TIME_MS = 20 * 60 * 1000; // 20 minutes |
| -var storage = chrome.storage.local; |
| - |
| /** |
| * Names for tasks that can be created by the extension. |
| */ |
| @@ -121,6 +120,17 @@ tasks.instrumentApiFunction(chrome.runtime.onStartup, 'addListener', 0); |
| tasks.instrumentApiFunction(chrome.tabs, 'create', 1); |
| tasks.instrumentApiFunction(storage, 'get', 1); |
| +var updateCardsAttempts = buildAttemptManager( |
| + 'cards-update', |
| + requestLocation, |
| + INITIAL_POLLING_PERIOD_SECONDS, |
| + MAXIMUM_POLLING_PERIOD_SECONDS); |
| +var dismissalAttempts = buildAttemptManager( |
| + 'dismiss', |
| + retryPendingDismissals, |
| + INITIAL_RETRY_DISMISS_PERIOD_SECONDS, |
| + MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS); |
| + |
| /** |
| * Diagnostic event identifier. |
| * @enum {number} |
| @@ -293,13 +303,9 @@ function parseAndShowNotificationCards(response, callback) { |
| } |
| } |
| - scheduleNextUpdate(parsedResponse.expiration_timestamp_seconds); |
| + updateCardsAttempts.start(parsedResponse.expiration_timestamp_seconds); |
|
skare_
2013/05/14 02:03:52
just as an opinion as a first-time reader, name fo
vadimt
2013/05/14 18:47:50
'Request' has connotations with a single network r
|
| - // Now that we got a valid response from the server, reset the retry period |
| - // to the initial value. This retry period will be used the next time we |
| - // fail to get the server-provided period. |
| storage.set({ |
| - retryDelaySeconds: INITIAL_POLLING_PERIOD_SECONDS, |
| activeNotifications: notificationsData, |
| recentDismissals: updatedRecentDismissals |
| }); |
| @@ -372,23 +378,7 @@ function updateNotificationsCards(position) { |
| ' @' + new Date()); |
| tasks.add(UPDATE_CARDS_TASK_NAME, function(callback) { |
| console.log('updateNotificationsCards-task-begin'); |
| - tasks.debugSetStepName('updateNotificationsCards-get-retryDelaySeconds'); |
| - storage.get('retryDelaySeconds', function(items) { |
| - console.log('updateNotificationsCards-get-retryDelaySeconds ' + |
| - JSON.stringify(items)); |
| - // Immediately schedule the update after the current retry period. Then, |
| - // we'll use update time from the server if available. |
| - scheduleNextUpdate(items.retryDelaySeconds); |
| - |
| - // TODO(vadimt): Consider interrupting waiting for the next update if we |
| - // detect that the network conditions have changed. Also, decide whether |
| - // the exponential backoff is needed both when we are offline and when |
| - // there are failures on the server side. |
| - var newRetryDelaySeconds = |
| - Math.min(items.retryDelaySeconds * 2 * (1 + 0.2 * Math.random()), |
| - MAXIMUM_POLLING_PERIOD_SECONDS); |
| - storage.set({retryDelaySeconds: newRetryDelaySeconds}); |
| - |
| + updateCardsAttempts.planForNext(function() { |
| processPendingDismissals(function(success) { |
| if (success) { |
| // The cards are requested only if there are no unsent dismissals. |
| @@ -460,7 +450,7 @@ function processPendingDismissals(callbackBoolean) { |
| function doProcessDismissals() { |
| if (items.pendingDismissals.length == 0) { |
| - chrome.alarms.clear(RETRY_DISMISS_ALARM_NAME); |
| + dismissalAttempts.stop(); |
| onFinish(true); |
| return; |
| } |
| @@ -490,7 +480,9 @@ function processPendingDismissals(callbackBoolean) { |
| */ |
| function retryPendingDismissals() { |
| tasks.add(RETRY_DISMISS_TASK_NAME, function(callback) { |
| - processPendingDismissals(function(success) { callback(); }); |
| + dismissalAttempts.planForNext(function() { |
| + processPendingDismissals(function(success) { callback(); }); |
| + }); |
| }); |
| } |
| @@ -538,15 +530,7 @@ function onNotificationClosed(notificationId, byUser) { |
| chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed'); |
| tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { |
| - // Schedule retrying dismissing until all dismissals go through. |
| - // TODO(vadimt): Implement exponential backoff and unify it with getting |
| - // cards. |
| - var alarmInfo = { |
| - delayInMinutes: RETRY_DISMISS_PERIOD_SECONDS / 60, |
| - periodInMinutes: RETRY_DISMISS_PERIOD_SECONDS / 60 |
| - }; |
| - |
| - chrome.alarms.create(RETRY_DISMISS_ALARM_NAME, alarmInfo); |
| + dismissalAttempts.start(); |
| // Deleting the notification in case it was re-added while this task was |
| // scheduled, waiting for execution. |
| @@ -568,37 +552,19 @@ function onNotificationClosed(notificationId, byUser) { |
| } |
| /** |
| - * Schedules next update for notification cards. |
| - * @param {int} delaySeconds Length of time in seconds after which the alarm |
| - * event should fire. |
| - */ |
| -function scheduleNextUpdate(delaySeconds) { |
| - console.log('scheduleNextUpdate ' + delaySeconds); |
| - // Schedule an alarm after the specified delay. 'periodInMinutes' is for the |
| - // case when we fail to re-register the alarm. |
| - var alarmInfo = { |
| - delayInMinutes: delaySeconds / 60, |
| - periodInMinutes: MAXIMUM_POLLING_PERIOD_SECONDS / 60 |
| - }; |
| - |
| - chrome.alarms.create(UPDATE_NOTIFICATIONS_ALARM_NAME, alarmInfo); |
| -} |
| - |
| -/** |
| * Initializes the event page on install or on browser startup. |
| */ |
| function initialize() { |
| + // Create an update timer for a case when for some reason location request |
| + // gets stuck. |
| + updateCardsAttempts.start(MAXIMUM_POLLING_PERIOD_SECONDS); |
| + |
| var initialStorage = { |
| activeNotifications: {}, |
| - recentDismissals: {}, |
| - retryDelaySeconds: INITIAL_POLLING_PERIOD_SECONDS |
| + recentDismissals: {} |
| }; |
| storage.set(initialStorage); |
| - // Create an update timer for a case when for some reason location request |
| - // gets stuck. |
| - scheduleNextUpdate(MAXIMUM_POLLING_PERIOD_SECONDS); |
| - |
| requestLocation(); |
| } |
| @@ -615,13 +581,6 @@ chrome.runtime.onStartup.addListener(function() { |
| initialize(); |
| }); |
| -chrome.alarms.onAlarm.addListener(function(alarm) { |
| - if (alarm.name == UPDATE_NOTIFICATIONS_ALARM_NAME) |
| - requestLocation(); |
| - else if (alarm.name == RETRY_DISMISS_ALARM_NAME) |
| - retryPendingDismissals(); |
| -}); |
| - |
| chrome.notifications.onClicked.addListener( |
| function(notificationId) { |
| chrome.metricsPrivate.recordUserAction('GoogleNow.MessageClicked'); |