| 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'; | 5 'use strict'; |
| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 | 100 |
| 101 return true; | 101 return true; |
| 102 } | 102 } |
| 103 | 103 |
| 104 /** | 104 /** |
| 105 * Adds a new task. If another task is not running, runs the task immediately. | 105 * Adds a new task. If another task is not running, runs the task immediately. |
| 106 * If any task in the queue is not compatible with the task, ignores the new | 106 * If any task in the queue is not compatible with the task, ignores the new |
| 107 * task. Otherwise, stores the task for future execution. | 107 * task. Otherwise, stores the task for future execution. |
| 108 * @param {string} taskName Name of the task. | 108 * @param {string} taskName Name of the task. |
| 109 * @param {function(function())} task Function to run. Takes a callback | 109 * @param {function(function())} task Function to run. Takes a callback |
| 110 * parameter. | 110 * parameter. Call this callback on completion. |
| 111 */ | 111 */ |
| 112 function add(taskName, task) { | 112 function add(taskName, task) { |
| 113 console.log('Adding task ' + taskName); | 113 console.log('Adding task ' + taskName); |
| 114 if (!canQueue(taskName)) | 114 if (!canQueue(taskName)) |
| 115 return; | 115 return; |
| 116 | 116 |
| 117 queue.push({name: taskName, task: task}); | 117 queue.push({name: taskName, task: task}); |
| 118 | 118 |
| 119 if (queue.length == 1) { | 119 if (queue.length == 1) { |
| 120 startFirst(); | 120 startFirst(); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 */ | 284 */ |
| 285 function createAlarm(delaySeconds) { | 285 function createAlarm(delaySeconds) { |
| 286 var alarmInfo = { | 286 var alarmInfo = { |
| 287 delayInMinutes: delaySeconds / 60, | 287 delayInMinutes: delaySeconds / 60, |
| 288 periodInMinutes: maximumDelaySeconds / 60 | 288 periodInMinutes: maximumDelaySeconds / 60 |
| 289 }; | 289 }; |
| 290 chrome.alarms.create(alarmName, alarmInfo); | 290 chrome.alarms.create(alarmName, alarmInfo); |
| 291 } | 291 } |
| 292 | 292 |
| 293 /** | 293 /** |
| 294 * Indicates if this attempt manager has started. |
| 295 * @param {function(boolean)} callback The function's boolean parameter is |
| 296 * true if the attempt manager has started, false otherwise. |
| 297 */ |
| 298 function isRunning(callback) { |
| 299 chrome.alarms.get(alarmName, function(alarmInfo) { |
| 300 callback(!!alarmInfo); |
| 301 }); |
| 302 } |
| 303 |
| 304 /** |
| 294 * Schedules next attempt. | 305 * Schedules next attempt. |
| 295 * @param {number=} opt_previousDelaySeconds Previous delay in a sequence of | 306 * @param {number=} opt_previousDelaySeconds Previous delay in a sequence of |
| 296 * retry attempts, if specified. Not specified for scheduling first retry | 307 * retry attempts, if specified. Not specified for scheduling first retry |
| 297 * in the exponential sequence. | 308 * in the exponential sequence. |
| 298 */ | 309 */ |
| 299 function scheduleNextAttempt(opt_previousDelaySeconds) { | 310 function scheduleNextAttempt(opt_previousDelaySeconds) { |
| 300 var base = opt_previousDelaySeconds ? opt_previousDelaySeconds * 2 : | 311 var base = opt_previousDelaySeconds ? opt_previousDelaySeconds * 2 : |
| 301 initialDelaySeconds; | 312 initialDelaySeconds; |
| 302 var newRetryDelaySeconds = | 313 var newRetryDelaySeconds = |
| 303 Math.min(base * (1 + 0.2 * Math.random()), maximumDelaySeconds); | 314 Math.min(base * (1 + 0.2 * Math.random()), maximumDelaySeconds); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 tasks.debugSetStepName('planForNext-get-storage'); | 352 tasks.debugSetStepName('planForNext-get-storage'); |
| 342 storage.get(currentDelayStorageKey, function(items) { | 353 storage.get(currentDelayStorageKey, function(items) { |
| 343 console.log('planForNext-get-storage ' + JSON.stringify(items)); | 354 console.log('planForNext-get-storage ' + JSON.stringify(items)); |
| 344 scheduleNextAttempt(items[currentDelayStorageKey]); | 355 scheduleNextAttempt(items[currentDelayStorageKey]); |
| 345 callback(); | 356 callback(); |
| 346 }); | 357 }); |
| 347 } | 358 } |
| 348 | 359 |
| 349 chrome.alarms.onAlarm.addListener(function(alarm) { | 360 chrome.alarms.onAlarm.addListener(function(alarm) { |
| 350 if (alarm.name == alarmName) | 361 if (alarm.name == alarmName) |
| 351 attempt(); | 362 isRunning(function(running) { |
| 363 if (running) |
| 364 attempt(); |
| 365 }); |
| 352 }); | 366 }); |
| 353 | 367 |
| 354 return { | 368 return { |
| 355 start: start, | 369 start: start, |
| 356 planForNext: planForNext, | 370 planForNext: planForNext, |
| 357 stop: stop | 371 stop: stop, |
| 372 isRunning: isRunning |
| 358 }; | 373 }; |
| 359 } | 374 } |
| 375 |
| OLD | NEW |