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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 /** | 427 /** |
425 * Schedules next cards poll. | 428 * Schedules next cards poll. |
426 * @param {Object.<string, StorageGroup>} groups Map from group name to group | 429 * @param {Object.<string, StorageGroup>} groups Map from group name to group |
427 * information. | 430 * information. |
428 */ | 431 */ |
429 function scheduleNextPoll(groups) { | 432 function scheduleNextPoll(groups) { |
430 var nextPollTime = null; | 433 var nextPollTime = null; |
431 | 434 |
432 for (var groupName in groups) { | 435 for (var groupName in groups) { |
433 var group = groups[groupName]; | 436 var group = groups[groupName]; |
434 nextPollTime = nextPollTime == null ? | 437 if (group.nextPollTime !== undefined) { |
435 group.nextPollTime : Math.min(group.nextPollTime, nextPollTime); | 438 nextPollTime = nextPollTime == null ? |
| 439 group.nextPollTime : Math.min(group.nextPollTime, nextPollTime); |
| 440 } |
436 } | 441 } |
437 | 442 |
| 443 // At least one of the groups must have nextPollTime. |
438 verify(nextPollTime != null, 'scheduleNextPoll: nextPollTime is null'); | 444 verify(nextPollTime != null, 'scheduleNextPoll: nextPollTime is null'); |
439 | 445 |
440 var nextPollDelaySeconds = Math.max( | 446 var nextPollDelaySeconds = Math.max( |
441 (nextPollTime - Date.now()) / MS_IN_SECOND, | 447 (nextPollTime - Date.now()) / MS_IN_SECOND, |
442 MINIMUM_POLLING_PERIOD_SECONDS); | 448 MINIMUM_POLLING_PERIOD_SECONDS); |
443 updateCardsAttempts.start(nextPollDelaySeconds); | 449 updateCardsAttempts.start(nextPollDelaySeconds); |
444 } | 450 } |
445 | 451 |
446 /** | 452 /** |
447 * Merges notification groups into a set of Chrome notifications and shows them. | 453 * 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... |
486 var now = Date.now(); | 492 var now = Date.now(); |
487 | 493 |
488 // Build updated set of groups. | 494 // Build updated set of groups. |
489 var updatedGroups = {}; | 495 var updatedGroups = {}; |
490 | 496 |
491 for (var groupName in receivedGroups) { | 497 for (var groupName in receivedGroups) { |
492 var receivedGroup = receivedGroups[groupName]; | 498 var receivedGroup = receivedGroups[groupName]; |
493 var storageGroup = items.notificationGroups[groupName] || { | 499 var storageGroup = items.notificationGroups[groupName] || { |
494 cards: [], | 500 cards: [], |
495 cardsTimestamp: undefined, | 501 cardsTimestamp: undefined, |
496 nextPollTime: now, | 502 nextPollTime: undefined, |
497 rank: undefined | 503 rank: undefined |
498 }; | 504 }; |
499 | 505 |
500 if (receivedGroup.requested) | 506 if (receivedGroup.requested) |
501 receivedGroup.cards = receivedGroup.cards || []; | 507 receivedGroup.cards = receivedGroup.cards || []; |
502 | 508 |
503 if (receivedGroup.cards) { | 509 if (receivedGroup.cards) { |
| 510 // If the group contains a cards update, all its fields will get new |
| 511 // values. |
504 storageGroup.cards = receivedGroup.cards; | 512 storageGroup.cards = receivedGroup.cards; |
505 storageGroup.cardsTimestamp = now; | 513 storageGroup.cardsTimestamp = now; |
506 storageGroup.rank = receivedGroup.rank; | 514 storageGroup.rank = receivedGroup.rank; |
| 515 storageGroup.nextPollTime = undefined; |
| 516 // The code below assigns nextPollTime a defined value if |
| 517 // nextPollSeconds is specified in the received group. |
| 518 // If the group's cards are not updated, and nextPollSeconds is |
| 519 // unspecified, this method doesn't change group's nextPollTime. |
507 } | 520 } |
508 | 521 |
| 522 // 'nextPollSeconds' may be sent even for groups that don't contain cards |
| 523 // updates. |
509 if (receivedGroup.nextPollSeconds !== undefined) { | 524 if (receivedGroup.nextPollSeconds !== undefined) { |
510 storageGroup.nextPollTime = | 525 storageGroup.nextPollTime = |
511 now + receivedGroup.nextPollSeconds * MS_IN_SECOND; | 526 now + receivedGroup.nextPollSeconds * MS_IN_SECOND; |
512 } | 527 } |
513 | 528 |
514 updatedGroups[groupName] = storageGroup; | 529 updatedGroups[groupName] = storageGroup; |
515 } | 530 } |
516 | 531 |
517 scheduleNextPoll(updatedGroups); | 532 scheduleNextPoll(updatedGroups); |
518 chrome.storage.local.set({notificationGroups: updatedGroups}); | 533 chrome.storage.local.set({notificationGroups: updatedGroups}); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 JSON.stringify(items)); | 583 JSON.stringify(items)); |
569 items = items || {}; | 584 items = items || {}; |
570 | 585 |
571 var groupsToRequest = []; | 586 var groupsToRequest = []; |
572 | 587 |
573 if (items.notificationGroups) { | 588 if (items.notificationGroups) { |
574 var now = Date.now(); | 589 var now = Date.now(); |
575 | 590 |
576 for (var groupName in items.notificationGroups) { | 591 for (var groupName in items.notificationGroups) { |
577 var group = items.notificationGroups[groupName]; | 592 var group = items.notificationGroups[groupName]; |
578 if (group.nextPollTime <= now) | 593 if (group.nextPollTime !== undefined && group.nextPollTime <= now) |
579 groupsToRequest.push(groupName); | 594 groupsToRequest.push(groupName); |
580 } | 595 } |
581 } | 596 } |
582 | 597 |
583 requestNotificationGroups(groupsToRequest); | 598 requestNotificationGroups(groupsToRequest); |
584 }); | 599 }); |
585 } | 600 } |
586 | 601 |
587 /** | 602 /** |
588 * Starts getting location for a cards update. | 603 * Starts getting location for a cards update. |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1063 | 1078 |
1064 updateCardsAttempts.isRunning(function(running) { | 1079 updateCardsAttempts.isRunning(function(running) { |
1065 if (running) | 1080 if (running) |
1066 requestNotificationGroups([]); | 1081 requestNotificationGroups([]); |
1067 }); | 1082 }); |
1068 } | 1083 } |
1069 }); | 1084 }); |
1070 }); | 1085 }); |
1071 } | 1086 } |
1072 }); | 1087 }); |
OLD | NEW |