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 | |
skare_
2013/05/14 02:03:52
nit: expand till->until
vadimt
2013/05/14 18:47:50
Done.
| |
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=} opt_previousDelaySeconds Previous delay in a sequence of | |
257 * retry attemps, if specified. Not specified for scheduling first retry | |
rgustafson
2013/05/14 01:25:17
attempts
vadimt
2013/05/14 18:47:50
Done.
| |
258 * in the exponential sequence. | |
259 */ | |
260 function scheduleNextAttempt(opt_previousDelaySeconds) { | |
261 var base = opt_previousDelaySeconds ? opt_previousDelaySeconds * 2 : | |
skare_
2013/05/14 02:03:52
it's likely this isn't a concern since this isn't
vadimt
2013/05/14 18:47:50
No. Anyways, we'd need to special-case 0, otherwis
| |
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); | |
271 } | |
272 | |
273 /** | |
274 * Starts repeated attempts. | |
275 * @param {number=} opt_firstDelaySeconds Time until the first attempt, if | |
276 * specified. Otherwise, initialDelaySeconds will be used for the first | |
277 * attempt. | |
278 */ | |
279 function start(opt_firstDelaySeconds) { | |
280 if (opt_firstDelaySeconds) { | |
281 createAlarm(opt_firstDelaySeconds); | |
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) { | |
rgustafson
2013/05/14 01:25:17
The wording of this confused me too. At the least,
vadimt
2013/05/14 18:47:50
Done.
| |
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]); | |
rgustafson
2013/05/14 01:25:17
This value isn't going to exist if you call this a
vadimt
2013/05/14 18:47:50
Yes, in this case, we start from the default value
| |
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 |