Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // 'use strict'; TODO(vadimt): Uncomment once crbug.com/237617 is fixed. | 5 // 'use strict'; TODO(vadimt): Uncomment once crbug.com/237617 is fixed. |
| 6 | 6 |
| 7 // TODO(vadimt): Remove alerts. | 7 // TODO(vadimt): Remove alerts. |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * @fileoverview Utility objects and functions for Google Now extension. | 10 * @fileoverview Utility objects and functions for Google Now extension. |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 }); | 214 }); |
| 215 | 215 |
| 216 return { | 216 return { |
| 217 add: add, | 217 add: add, |
| 218 // TODO(vadimt): Replace with instrumenting callbacks. | 218 // TODO(vadimt): Replace with instrumenting callbacks. |
| 219 debugSetStepName: debugSetStepName, | 219 debugSetStepName: debugSetStepName, |
| 220 instrumentApiFunction: instrumentApiFunction, | 220 instrumentApiFunction: instrumentApiFunction, |
| 221 wrapCallback: wrapCallback | 221 wrapCallback: wrapCallback |
| 222 }; | 222 }; |
| 223 } | 223 } |
| 224 | |
| 225 var storage = chrome.storage.local; | |
| 226 | |
| 227 /** | |
| 228 * Builds an object to manage retrying activities with exponential backoff. | |
| 229 * @param {string} name Name of this attempt manager. | |
| 230 * @param {function()} attempt Activity that the manager retries till it | |
| 231 * calls 'stop' method. | |
| 232 * @param {number} initialDelaySeconds Default first delay until first retry. | |
| 233 * @param {number} maximumDelaySeconds Maximum delay between retries. | |
| 234 * @return {Object} Attempt manager interface. | |
| 235 */ | |
| 236 function buildAttemptManager( | |
| 237 name, attempt, initialDelaySeconds, maximumDelaySeconds) { | |
| 238 var alarmName = name + '-scheduler'; | |
| 239 var currentDelayStorageKey = name + '-current-delay'; | |
| 240 | |
| 241 /** | |
| 242 * Creates an alarm for the next attempt. The alarm is repeating for the case | |
| 243 * when the next attempt crashes before registering next alarm. | |
| 244 * @param {number} delaySeconds Delay until next retry. | |
| 245 */ | |
| 246 function createAlarm(delaySeconds) { | |
| 247 var alarmInfo = { | |
| 248 delayInMinutes: delaySeconds / 60, | |
| 249 periodInMinutes: maximumDelaySeconds / 60 | |
| 250 }; | |
| 251 chrome.alarms.create(alarmName, alarmInfo); | |
| 252 } | |
| 253 | |
| 254 /** | |
| 255 * Schedules next attempt. | |
| 256 * @param {number=} optionalPreviousDelaySeconds Previous delay in a sequence | |
| 257 * of retry attemps, if specified. Not specified for scheduling first | |
| 258 * retry in the exponential sequence. | |
| 259 */ | |
| 260 function scheduleNextAttempt(optionalPreviousDelaySeconds) { | |
| 261 var base = optionalPreviousDelaySeconds ? optionalPreviousDelaySeconds * 2 : | |
| 262 initialDelaySeconds; | |
| 263 var newRetryDelaySeconds = | |
| 264 Math.min(base * (1 + 0.2 * Math.random()), maximumDelaySeconds); | |
| 265 | |
| 266 createAlarm(newRetryDelaySeconds); | |
| 267 | |
| 268 var items = {}; | |
| 269 items[currentDelayStorageKey] = newRetryDelaySeconds; | |
| 270 storage.set(items); | |
|
skare_
2013/05/11 00:56:23
this is just syntax but either
var items = {cDSK:
vadimt
2013/05/11 01:55:15
This would create a field 'currentDelayStorageKey'
skare_
2013/05/11 02:07:19
[ok]
oops, thought this was a literal.
Might want
| |
| 271 } | |
| 272 | |
| 273 /** | |
| 274 * Starts repeated attempts. | |
| 275 * @param {number=} optionalFirstDelaySeconds Time until the first attempt, if | |
| 276 * specified. Otherwise, initialDelaySeconds will be used for the first | |
| 277 * attempt. | |
| 278 */ | |
| 279 function start(optionalFirstDelaySeconds) { | |
|
skare_
2013/05/11 00:56:23
opt_FirstDelaySeconds
(s/optional/opt_ unless some
skare_
2013/05/11 01:03:05
typo -- opt_firstDelaySeconds actually.
vadimt
2013/05/11 01:55:15
Done.
| |
| 280 if (optionalFirstDelaySeconds) { | |
| 281 createAlarm(optionalFirstDelaySeconds); | |
| 282 storage.remove(currentDelayStorageKey); | |
| 283 } else { | |
| 284 scheduleNextAttempt(); | |
| 285 } | |
| 286 } | |
| 287 | |
| 288 /** | |
| 289 * Stops repeated attempts. | |
| 290 */ | |
| 291 function stop() { | |
| 292 chrome.alarms.clear(alarmName); | |
| 293 storage.remove(currentDelayStorageKey); | |
| 294 } | |
| 295 | |
| 296 /** | |
| 297 * Plans for the next attempt. | |
| 298 * @param {function()} callback Completion callback. | |
| 299 */ | |
| 300 function planForNext(callback) { | |
| 301 tasks.debugSetStepName('planForNext-get-storage'); | |
| 302 storage.get(currentDelayStorageKey, function(items) { | |
| 303 console.log('planForNext-get-storage ' + JSON.stringify(items)); | |
| 304 scheduleNextAttempt(items[currentDelayStorageKey]); | |
| 305 callback(); | |
| 306 }); | |
| 307 } | |
| 308 | |
| 309 chrome.alarms.onAlarm.addListener(function(alarm) { | |
| 310 if (alarm.name == alarmName) | |
| 311 attempt(); | |
| 312 }); | |
| 313 | |
| 314 return { | |
| 315 start: start, | |
| 316 planForNext: planForNext, | |
| 317 stop: stop | |
| 318 }; | |
| 319 } | |
| OLD | NEW |