| 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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 123 |
| 124 return true; | 124 return true; |
| 125 } | 125 } |
| 126 | 126 |
| 127 /** | 127 /** |
| 128 * Adds a new task. If another task is not running, runs the task immediately. | 128 * Adds a new task. If another task is not running, runs the task immediately. |
| 129 * If any task in the queue is not compatible with the task, ignores the new | 129 * If any task in the queue is not compatible with the task, ignores the new |
| 130 * task. Otherwise, stores the task for future execution. | 130 * task. Otherwise, stores the task for future execution. |
| 131 * @param {string} taskName Name of the task. | 131 * @param {string} taskName Name of the task. |
| 132 * @param {function(function())} task Function to run. Takes a callback | 132 * @param {function(function())} task Function to run. Takes a callback |
| 133 * parameter. | 133 * parameter. Call this callback on completion. |
| 134 */ | 134 */ |
| 135 function add(taskName, task) { | 135 function add(taskName, task) { |
| 136 console.log('Adding task ' + taskName); | 136 console.log('Adding task ' + taskName); |
| 137 if (!canQueue(taskName)) | 137 if (!canQueue(taskName)) |
| 138 return; | 138 return; |
| 139 | 139 |
| 140 queue.push({name: taskName, task: task}); | 140 queue.push({name: taskName, task: task}); |
| 141 | 141 |
| 142 if (queue.length == 1) { | 142 if (queue.length == 1) { |
| 143 startFirst(); | 143 startFirst(); |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 */ | 325 */ |
| 326 function createAlarm(delaySeconds) { | 326 function createAlarm(delaySeconds) { |
| 327 var alarmInfo = { | 327 var alarmInfo = { |
| 328 delayInMinutes: delaySeconds / 60, | 328 delayInMinutes: delaySeconds / 60, |
| 329 periodInMinutes: maximumDelaySeconds / 60 | 329 periodInMinutes: maximumDelaySeconds / 60 |
| 330 }; | 330 }; |
| 331 chrome.alarms.create(alarmName, alarmInfo); | 331 chrome.alarms.create(alarmName, alarmInfo); |
| 332 } | 332 } |
| 333 | 333 |
| 334 /** | 334 /** |
| 335 * Indicates if this attempt manager has started. |
| 336 * @param {function(boolean)} callback The function's boolean parameter is |
| 337 * true if the attempt manager has started, false otherwise. |
| 338 */ |
| 339 function isRunning(callback) { |
| 340 chrome.alarms.get(alarmName, function(alarmInfo) { |
| 341 callback(!!alarmInfo); |
| 342 }); |
| 343 } |
| 344 |
| 345 /** |
| 335 * Schedules next attempt. | 346 * Schedules next attempt. |
| 336 * @param {number=} opt_previousDelaySeconds Previous delay in a sequence of | 347 * @param {number=} opt_previousDelaySeconds Previous delay in a sequence of |
| 337 * retry attempts, if specified. Not specified for scheduling first retry | 348 * retry attempts, if specified. Not specified for scheduling first retry |
| 338 * in the exponential sequence. | 349 * in the exponential sequence. |
| 339 */ | 350 */ |
| 340 function scheduleNextAttempt(opt_previousDelaySeconds) { | 351 function scheduleNextAttempt(opt_previousDelaySeconds) { |
| 341 var base = opt_previousDelaySeconds ? opt_previousDelaySeconds * 2 : | 352 var base = opt_previousDelaySeconds ? opt_previousDelaySeconds * 2 : |
| 342 initialDelaySeconds; | 353 initialDelaySeconds; |
| 343 var newRetryDelaySeconds = | 354 var newRetryDelaySeconds = |
| 344 Math.min(base * (1 + 0.2 * Math.random()), maximumDelaySeconds); | 355 Math.min(base * (1 + 0.2 * Math.random()), maximumDelaySeconds); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 function planForNext(callback) { | 392 function planForNext(callback) { |
| 382 storage.get(currentDelayStorageKey, function(items) { | 393 storage.get(currentDelayStorageKey, function(items) { |
| 383 console.log('planForNext-get-storage ' + JSON.stringify(items)); | 394 console.log('planForNext-get-storage ' + JSON.stringify(items)); |
| 384 scheduleNextAttempt(items[currentDelayStorageKey]); | 395 scheduleNextAttempt(items[currentDelayStorageKey]); |
| 385 callback(); | 396 callback(); |
| 386 }); | 397 }); |
| 387 } | 398 } |
| 388 | 399 |
| 389 chrome.alarms.onAlarm.addListener(function(alarm) { | 400 chrome.alarms.onAlarm.addListener(function(alarm) { |
| 390 if (alarm.name == alarmName) | 401 if (alarm.name == alarmName) |
| 391 attempt(); | 402 isRunning(function(running) { |
| 403 if (running) |
| 404 attempt(); |
| 405 }); |
| 392 }); | 406 }); |
| 393 | 407 |
| 394 return { | 408 return { |
| 395 start: start, | 409 start: start, |
| 396 planForNext: planForNext, | 410 planForNext: planForNext, |
| 397 stop: stop | 411 stop: stop, |
| 412 isRunning: isRunning |
| 398 }; | 413 }; |
| 399 } | 414 } |
| 415 |
| OLD | NEW |