| 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 /** | 7 /** |
| 8 * @fileoverview Utility objects and functions for Google Now extension. | 8 * @fileoverview Utility objects and functions for Google Now extension. |
| 9 * Most important entities here: | 9 * Most important entities here: |
| 10 * (1) 'wrapper' is a module used to add error handling and other services to | 10 * (1) 'wrapper' is a module used to add error handling and other services to |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 * Builds the object to manage tasks (mutually exclusive chains of events). | 389 * Builds the object to manage tasks (mutually exclusive chains of events). |
| 390 * @param {function(string, string): boolean} areConflicting Function that | 390 * @param {function(string, string): boolean} areConflicting Function that |
| 391 * checks if a new task can't be added to a task queue that contains an | 391 * checks if a new task can't be added to a task queue that contains an |
| 392 * existing task. | 392 * existing task. |
| 393 * @return {Object} Task manager interface. | 393 * @return {Object} Task manager interface. |
| 394 */ | 394 */ |
| 395 function buildTaskManager(areConflicting) { | 395 function buildTaskManager(areConflicting) { |
| 396 /** | 396 /** |
| 397 * Queue of scheduled tasks. The first element, if present, corresponds to the | 397 * Queue of scheduled tasks. The first element, if present, corresponds to the |
| 398 * currently running task. | 398 * currently running task. |
| 399 * @type {Array.<Object.<string, function(function())>>} | 399 * @type {Array.<Object.<string, function()>>} |
| 400 */ | 400 */ |
| 401 var queue = []; | 401 var queue = []; |
| 402 | 402 |
| 403 /** | 403 /** |
| 404 * Count of unfinished callbacks of the current task. | 404 * Count of unfinished callbacks of the current task. |
| 405 * @type {number} | 405 * @type {number} |
| 406 */ | 406 */ |
| 407 var taskPendingCallbackCount = 0; | 407 var taskPendingCallbackCount = 0; |
| 408 | 408 |
| 409 /** | 409 /** |
| (...skipping 13 matching lines...) Expand all Loading... |
| 423 // Start the oldest queued task, but don't remove it from the queue. | 423 // Start the oldest queued task, but don't remove it from the queue. |
| 424 verify( | 424 verify( |
| 425 taskPendingCallbackCount == 0, | 425 taskPendingCallbackCount == 0, |
| 426 'tasks.startFirst: still have pending task callbacks: ' + | 426 'tasks.startFirst: still have pending task callbacks: ' + |
| 427 taskPendingCallbackCount + | 427 taskPendingCallbackCount + |
| 428 ', queue = ' + JSON.stringify(queue) + ', ' + | 428 ', queue = ' + JSON.stringify(queue) + ', ' + |
| 429 wrapper.debugGetStateString()); | 429 wrapper.debugGetStateString()); |
| 430 var entry = queue[0]; | 430 var entry = queue[0]; |
| 431 console.log('Starting task ' + entry.name); | 431 console.log('Starting task ' + entry.name); |
| 432 | 432 |
| 433 entry.task(function() {}); // TODO(vadimt): Don't pass parameter. | 433 entry.task(); |
| 434 | 434 |
| 435 verify(isInTask, 'startFirst: not in task at exit'); | 435 verify(isInTask, 'startFirst: not in task at exit'); |
| 436 isInTask = false; | 436 isInTask = false; |
| 437 if (taskPendingCallbackCount == 0) | 437 if (taskPendingCallbackCount == 0) |
| 438 finish(); | 438 finish(); |
| 439 } | 439 } |
| 440 | 440 |
| 441 /** | 441 /** |
| 442 * Checks if a new task can be added to the task queue. | 442 * Checks if a new task can be added to the task queue. |
| 443 * @param {string} taskName Name of the new task. | 443 * @param {string} taskName Name of the new task. |
| 444 * @return {boolean} Whether the new task can be added. | 444 * @return {boolean} Whether the new task can be added. |
| 445 */ | 445 */ |
| 446 function canQueue(taskName) { | 446 function canQueue(taskName) { |
| 447 for (var i = 0; i < queue.length; ++i) { | 447 for (var i = 0; i < queue.length; ++i) { |
| 448 if (areConflicting(taskName, queue[i].name)) { | 448 if (areConflicting(taskName, queue[i].name)) { |
| 449 console.log('Conflict: new=' + taskName + | 449 console.log('Conflict: new=' + taskName + |
| 450 ', scheduled=' + queue[i].name); | 450 ', scheduled=' + queue[i].name); |
| 451 return false; | 451 return false; |
| 452 } | 452 } |
| 453 } | 453 } |
| 454 | 454 |
| 455 return true; | 455 return true; |
| 456 } | 456 } |
| 457 | 457 |
| 458 /** | 458 /** |
| 459 * Adds a new task. If another task is not running, runs the task immediately. | 459 * Adds a new task. If another task is not running, runs the task immediately. |
| 460 * If any task in the queue is not compatible with the task, ignores the new | 460 * If any task in the queue is not compatible with the task, ignores the new |
| 461 * task. Otherwise, stores the task for future execution. | 461 * task. Otherwise, stores the task for future execution. |
| 462 * @param {string} taskName Name of the task. | 462 * @param {string} taskName Name of the task. |
| 463 * @param {function(function())} task Function to run. Takes a callback | 463 * @param {function()} task Function to run. |
| 464 * parameter. Call this callback on completion. | |
| 465 */ | 464 */ |
| 466 function add(taskName, task) { | 465 function add(taskName, task) { |
| 467 wrapper.checkInWrappedCallback(); | 466 wrapper.checkInWrappedCallback(); |
| 468 console.log('Adding task ' + taskName); | 467 console.log('Adding task ' + taskName); |
| 469 if (!canQueue(taskName)) | 468 if (!canQueue(taskName)) |
| 470 return; | 469 return; |
| 471 | 470 |
| 472 queue.push({name: taskName, task: task}); | 471 queue.push({name: taskName, task: task}); |
| 473 | 472 |
| 474 if (queue.length == 1) { | 473 if (queue.length == 1) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 finish(); | 529 finish(); |
| 531 } | 530 } |
| 532 } | 531 } |
| 533 }; | 532 }; |
| 534 | 533 |
| 535 wrapper.registerWrapperPluginFactory(function() { | 534 wrapper.registerWrapperPluginFactory(function() { |
| 536 return new TasksWrapperPlugin(); | 535 return new TasksWrapperPlugin(); |
| 537 }); | 536 }); |
| 538 | 537 |
| 539 return { | 538 return { |
| 540 add: add, | 539 add: add |
| 541 debugSetStepName: function() {} // TODO(vadimt): remove | |
| 542 }; | 540 }; |
| 543 } | 541 } |
| 544 | 542 |
| 545 /** | 543 /** |
| 546 * Builds an object to manage retrying activities with exponential backoff. | 544 * Builds an object to manage retrying activities with exponential backoff. |
| 547 * @param {string} name Name of this attempt manager. | 545 * @param {string} name Name of this attempt manager. |
| 548 * @param {function()} attempt Activity that the manager retries until it | 546 * @param {function()} attempt Activity that the manager retries until it |
| 549 * calls 'stop' method. | 547 * calls 'stop' method. |
| 550 * @param {number} initialDelaySeconds Default first delay until first retry. | 548 * @param {number} initialDelaySeconds Default first delay until first retry. |
| 551 * @param {number} maximumDelaySeconds Maximum delay between retries. | 549 * @param {number} maximumDelaySeconds Maximum delay between retries. |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 // Poll for the sign in state every hour. | 732 // Poll for the sign in state every hour. |
| 735 // One hour is just an arbitrary amount of time chosen. | 733 // One hour is just an arbitrary amount of time chosen. |
| 736 chrome.alarms.create(alarmName, {periodInMinutes: 60}); | 734 chrome.alarms.create(alarmName, {periodInMinutes: 60}); |
| 737 | 735 |
| 738 return { | 736 return { |
| 739 addListener: addListener, | 737 addListener: addListener, |
| 740 isSignedIn: isSignedIn, | 738 isSignedIn: isSignedIn, |
| 741 removeToken: removeToken | 739 removeToken: removeToken |
| 742 }; | 740 }; |
| 743 } | 741 } |
| OLD | NEW |