| 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 The event page for Google Now for Chrome implementation. | 8 * @fileoverview The event page for Google Now for Chrome implementation. |
| 9 * The Google Now event page gets Google Now cards from the server and shows | 9 * The Google Now event page gets Google Now cards from the server and shows |
| 10 * them as Chrome notifications. | 10 * them as Chrome notifications. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 * version: number, | 101 * version: number, |
| 102 * chromeNotificationOptions: Object, | 102 * chromeNotificationOptions: Object, |
| 103 * actionUrls: Object=, | 103 * actionUrls: Object=, |
| 104 * dismissal: Object | 104 * dismissal: Object |
| 105 * }} | 105 * }} |
| 106 */ | 106 */ |
| 107 var UnmergedNotification; | 107 var UnmergedNotification; |
| 108 | 108 |
| 109 /** | 109 /** |
| 110 * Notification group as the client stores it. |cardsTimestamp| and |rank| are | 110 * Notification group as the client stores it. |cardsTimestamp| and |rank| are |
| 111 * defined if |cards| is non-empty. | 111 * defined if |cards| is non-empty. |nextPollTime| is undefined if the server |
| 112 * (1) never sent 'nextPollSeconds' for the group or |
| 113 * (2) didn't send 'nextPollSeconds' with the last group update containing a |
| 114 * cards update and all the times after that. |
| 112 * | 115 * |
| 113 * @typedef {{ | 116 * @typedef {{ |
| 114 * cards: Array.<UnmergedNotification>, | 117 * cards: Array.<UnmergedNotification>, |
| 115 * cardsTimestamp: number=, | 118 * cardsTimestamp: number=, |
| 116 * nextPollTime: number, | 119 * nextPollTime: number=, |
| 117 * rank: number= | 120 * rank: number= |
| 118 * }} | 121 * }} |
| 119 */ | 122 */ |
| 120 var StorageGroup; | 123 var StorageGroup; |
| 121 | 124 |
| 122 /** | 125 /** |
| 123 * Checks if a new task can't be scheduled when another task is already | 126 * Checks if a new task can't be scheduled when another task is already |
| 124 * scheduled. | 127 * scheduled. |
| 125 * @param {string} newTaskName Name of the new task. | 128 * @param {string} newTaskName Name of the new task. |
| 126 * @param {string} scheduledTaskName Name of the scheduled task. | 129 * @param {string} scheduledTaskName Name of the scheduled task. |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 /** | 428 /** |
| 426 * Schedules next cards poll. | 429 * Schedules next cards poll. |
| 427 * @param {Object.<string, StorageGroup>} groups Map from group name to group | 430 * @param {Object.<string, StorageGroup>} groups Map from group name to group |
| 428 * information. | 431 * information. |
| 429 */ | 432 */ |
| 430 function scheduleNextPoll(groups) { | 433 function scheduleNextPoll(groups) { |
| 431 var nextPollTime = null; | 434 var nextPollTime = null; |
| 432 | 435 |
| 433 for (var groupName in groups) { | 436 for (var groupName in groups) { |
| 434 var group = groups[groupName]; | 437 var group = groups[groupName]; |
| 435 nextPollTime = nextPollTime == null ? | 438 if (group.nextPollTime !== undefined) { |
| 436 group.nextPollTime : Math.min(group.nextPollTime, nextPollTime); | 439 nextPollTime = nextPollTime == null ? |
| 440 group.nextPollTime : Math.min(group.nextPollTime, nextPollTime); |
| 441 } |
| 437 } | 442 } |
| 438 | 443 |
| 444 // At least one of the groups must have nextPollTime. |
| 439 verify(nextPollTime != null, 'scheduleNextPoll: nextPollTime is null'); | 445 verify(nextPollTime != null, 'scheduleNextPoll: nextPollTime is null'); |
| 440 | 446 |
| 441 var nextPollDelaySeconds = Math.max( | 447 var nextPollDelaySeconds = Math.max( |
| 442 (nextPollTime - Date.now()) / MS_IN_SECOND, | 448 (nextPollTime - Date.now()) / MS_IN_SECOND, |
| 443 MINIMUM_POLLING_PERIOD_SECONDS); | 449 MINIMUM_POLLING_PERIOD_SECONDS); |
| 444 updateCardsAttempts.start(nextPollDelaySeconds); | 450 updateCardsAttempts.start(nextPollDelaySeconds); |
| 445 } | 451 } |
| 446 | 452 |
| 447 /** | 453 /** |
| 448 * Merges notification groups into a set of Chrome notifications and shows them. | 454 * Merges notification groups into a set of Chrome notifications and shows them. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 var now = Date.now(); | 493 var now = Date.now(); |
| 488 | 494 |
| 489 // Build updated set of groups. | 495 // Build updated set of groups. |
| 490 var updatedGroups = {}; | 496 var updatedGroups = {}; |
| 491 | 497 |
| 492 for (var groupName in receivedGroups) { | 498 for (var groupName in receivedGroups) { |
| 493 var receivedGroup = receivedGroups[groupName]; | 499 var receivedGroup = receivedGroups[groupName]; |
| 494 var storageGroup = items.notificationGroups[groupName] || { | 500 var storageGroup = items.notificationGroups[groupName] || { |
| 495 cards: [], | 501 cards: [], |
| 496 cardsTimestamp: undefined, | 502 cardsTimestamp: undefined, |
| 497 nextPollTime: now, | 503 nextPollTime: undefined, |
| 498 rank: undefined | 504 rank: undefined |
| 499 }; | 505 }; |
| 500 | 506 |
| 501 if (receivedGroup.requested) | 507 if (receivedGroup.requested) |
| 502 receivedGroup.cards = receivedGroup.cards || []; | 508 receivedGroup.cards = receivedGroup.cards || []; |
| 503 | 509 |
| 504 if (receivedGroup.cards) { | 510 if (receivedGroup.cards) { |
| 511 // If the group contains a cards update, all its fields will get new |
| 512 // values. |
| 505 storageGroup.cards = receivedGroup.cards; | 513 storageGroup.cards = receivedGroup.cards; |
| 506 storageGroup.cardsTimestamp = now; | 514 storageGroup.cardsTimestamp = now; |
| 507 storageGroup.rank = receivedGroup.rank; | 515 storageGroup.rank = receivedGroup.rank; |
| 516 storageGroup.nextPollTime = undefined; |
| 517 // The code below assigns nextPollTime a defined value if |
| 518 // nextPollSeconds is specified in the received group. |
| 519 // If the group's cards are not updated, and nextPollSeconds is |
| 520 // unspecified, this method doesn't change group's nextPollTime. |
| 508 } | 521 } |
| 509 | 522 |
| 523 // 'nextPollSeconds' may be sent even for groups that don't contain cards |
| 524 // updates. |
| 510 if (receivedGroup.nextPollSeconds !== undefined) { | 525 if (receivedGroup.nextPollSeconds !== undefined) { |
| 511 storageGroup.nextPollTime = | 526 storageGroup.nextPollTime = |
| 512 now + receivedGroup.nextPollSeconds * MS_IN_SECOND; | 527 now + receivedGroup.nextPollSeconds * MS_IN_SECOND; |
| 513 } | 528 } |
| 514 | 529 |
| 515 updatedGroups[groupName] = storageGroup; | 530 updatedGroups[groupName] = storageGroup; |
| 516 } | 531 } |
| 517 | 532 |
| 518 scheduleNextPoll(updatedGroups); | 533 scheduleNextPoll(updatedGroups); |
| 519 chrome.storage.local.set({notificationGroups: updatedGroups}); | 534 chrome.storage.local.set({notificationGroups: updatedGroups}); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 JSON.stringify(items)); | 587 JSON.stringify(items)); |
| 573 items = items || {}; | 588 items = items || {}; |
| 574 | 589 |
| 575 var groupsToRequest = []; | 590 var groupsToRequest = []; |
| 576 | 591 |
| 577 if (items.notificationGroups) { | 592 if (items.notificationGroups) { |
| 578 var now = Date.now(); | 593 var now = Date.now(); |
| 579 | 594 |
| 580 for (var groupName in items.notificationGroups) { | 595 for (var groupName in items.notificationGroups) { |
| 581 var group = items.notificationGroups[groupName]; | 596 var group = items.notificationGroups[groupName]; |
| 582 if (group.nextPollTime <= now) | 597 if (group.nextPollTime !== undefined && group.nextPollTime <= now) |
| 583 groupsToRequest.push(groupName); | 598 groupsToRequest.push(groupName); |
| 584 } | 599 } |
| 585 } | 600 } |
| 586 | 601 |
| 587 requestNotificationGroups(groupsToRequest); | 602 requestNotificationGroups(groupsToRequest); |
| 588 }); | 603 }); |
| 589 } | 604 } |
| 590 | 605 |
| 591 /** | 606 /** |
| 592 * Starts getting location for a cards update. | 607 * Starts getting location for a cards update. |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1072 | 1087 |
| 1073 updateCardsAttempts.isRunning(function(running) { | 1088 updateCardsAttempts.isRunning(function(running) { |
| 1074 if (running) | 1089 if (running) |
| 1075 requestNotificationGroups([]); | 1090 requestNotificationGroups([]); |
| 1076 }); | 1091 }); |
| 1077 } | 1092 } |
| 1078 }); | 1093 }); |
| 1079 }); | 1094 }); |
| 1080 } | 1095 } |
| 1081 }); | 1096 }); |
| OLD | NEW |