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 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 var combinedCards = {}; | 435 var combinedCards = {}; |
436 | 436 |
437 for (var groupName in notificationGroups) | 437 for (var groupName in notificationGroups) |
438 combineGroup(combinedCards, notificationGroups[groupName]); | 438 combineGroup(combinedCards, notificationGroups[groupName]); |
439 | 439 |
440 showNotificationCards( | 440 showNotificationCards( |
441 notificationGroups, combinedCards, onSuccess, onCardShown); | 441 notificationGroups, combinedCards, onSuccess, onCardShown); |
442 } | 442 } |
443 | 443 |
444 /** | 444 /** |
445 * Parses JSON response from the notification server, shows notifications and | 445 * Based on a response from the notification server, shows notifications and |
446 * schedules next update. | 446 * schedules next update. |
447 * @param {string} response Server response. | 447 * @param {ServerResponse} response Server response. |
448 * @param {function(ReceivedNotification)=} onCardShown Optional parameter | 448 * @param {function(ReceivedNotification)=} onCardShown Optional parameter |
449 * called when each card is shown. | 449 * called when each card is shown. |
450 */ | 450 */ |
451 function parseAndShowNotificationCards(response, onCardShown) { | 451 function processServerResponse(response, onCardShown) { |
452 console.log('parseAndShowNotificationCards ' + response); | 452 console.log('processServerResponse ' + JSON.stringify(response)); |
453 /** @type {ServerResponse} */ | |
454 var parsedResponse = JSON.parse(response); | |
455 | 453 |
456 if (parsedResponse.googleNowDisabled) { | 454 if (response.googleNowDisabled) { |
457 chrome.storage.local.set({googleNowEnabled: false}); | 455 chrome.storage.local.set({googleNowEnabled: false}); |
458 // TODO(vadimt): Remove the line below once the server stops sending groups | 456 // TODO(vadimt): Remove the line below once the server stops sending groups |
459 // with 'googleNowDisabled' responses. | 457 // with 'googleNowDisabled' responses. |
460 parsedResponse.groups = {}; | 458 response.groups = {}; |
461 // Google Now was enabled; now it's disabled. This is a state change. | 459 // Google Now was enabled; now it's disabled. This is a state change. |
462 onStateChange(); | 460 onStateChange(); |
463 } | 461 } |
464 | 462 |
465 var receivedGroups = parsedResponse.groups; | 463 var receivedGroups = response.groups; |
466 | 464 |
467 instrumented.storage.local.get( | 465 instrumented.storage.local.get( |
468 ['notificationGroups', 'recentDismissals'], | 466 ['notificationGroups', 'recentDismissals'], |
469 function(items) { | 467 function(items) { |
470 console.log( | 468 console.log( |
471 'parseAndShowNotificationCards-get ' + JSON.stringify(items)); | 469 'processServerResponse-get ' + JSON.stringify(items)); |
472 items = items || {}; | 470 items = items || {}; |
473 /** @type {Object.<string, StoredNotificationGroup>} */ | 471 /** @type {Object.<string, StoredNotificationGroup>} */ |
474 items.notificationGroups = items.notificationGroups || {}; | 472 items.notificationGroups = items.notificationGroups || {}; |
475 /** @type {Object.<NotificationId, number>} */ | 473 /** @type {Object.<NotificationId, number>} */ |
476 items.recentDismissals = items.recentDismissals || {}; | 474 items.recentDismissals = items.recentDismissals || {}; |
477 | 475 |
478 // Build a set of non-expired recent dismissals. It will be used for | 476 // Build a set of non-expired recent dismissals. It will be used for |
479 // client-side filtering of cards. | 477 // client-side filtering of cards. |
480 /** @type {Object.<NotificationId, number>} */ | 478 /** @type {Object.<NotificationId, number>} */ |
481 var updatedRecentDismissals = {}; | 479 var updatedRecentDismissals = {}; |
482 var now = Date.now(); | 480 var now = Date.now(); |
483 for (var notificationId in items.recentDismissals) { | 481 for (var notificationId in items.recentDismissals) { |
484 var dismissalAge = now - items.recentDismissals[notificationId]; | 482 var dismissalAge = now - items.recentDismissals[notificationId]; |
485 if (dismissalAge < DISMISS_RETENTION_TIME_MS) { | 483 if (dismissalAge < DISMISS_RETENTION_TIME_MS) { |
486 updatedRecentDismissals[notificationId] = | 484 updatedRecentDismissals[notificationId] = |
487 items.recentDismissals[notificationId]; | 485 items.recentDismissals[notificationId]; |
488 } | 486 } |
489 } | 487 } |
490 | 488 |
491 // Populate groups with corresponding cards. | 489 // Populate groups with corresponding cards. |
492 if (parsedResponse.notifications) { | 490 if (response.notifications) { |
493 for (var i = 0; i < parsedResponse.notifications.length; ++i) { | 491 for (var i = 0; i < response.notifications.length; ++i) { |
494 /** @type {ReceivedNotification} */ | 492 /** @type {ReceivedNotification} */ |
495 var card = parsedResponse.notifications[i]; | 493 var card = response.notifications[i]; |
496 if (!(card.notificationId in updatedRecentDismissals)) { | 494 if (!(card.notificationId in updatedRecentDismissals)) { |
497 var group = receivedGroups[card.groupName]; | 495 var group = receivedGroups[card.groupName]; |
498 group.cards = group.cards || []; | 496 group.cards = group.cards || []; |
499 group.cards.push(card); | 497 group.cards.push(card); |
500 } | 498 } |
501 } | 499 } |
502 } | 500 } |
503 | 501 |
504 // Build updated set of groups. | 502 // Build updated set of groups. |
505 var updatedGroups = {}; | 503 var updatedGroups = {}; |
(...skipping 26 matching lines...) Expand all Loading... |
532 // 'nextPollSeconds' may be sent even for groups that don't contain | 530 // 'nextPollSeconds' may be sent even for groups that don't contain |
533 // cards updates. | 531 // cards updates. |
534 if (receivedGroup.nextPollSeconds !== undefined) { | 532 if (receivedGroup.nextPollSeconds !== undefined) { |
535 storedGroup.nextPollTime = | 533 storedGroup.nextPollTime = |
536 now + receivedGroup.nextPollSeconds * MS_IN_SECOND; | 534 now + receivedGroup.nextPollSeconds * MS_IN_SECOND; |
537 } | 535 } |
538 | 536 |
539 updatedGroups[groupName] = storedGroup; | 537 updatedGroups[groupName] = storedGroup; |
540 } | 538 } |
541 | 539 |
542 scheduleNextPoll(updatedGroups, !parsedResponse.googleNowDisabled); | 540 scheduleNextPoll(updatedGroups, !response.googleNowDisabled); |
543 combineAndShowNotificationCards( | 541 combineAndShowNotificationCards( |
544 updatedGroups, | 542 updatedGroups, |
545 function() { | 543 function() { |
546 chrome.storage.local.set({ | 544 chrome.storage.local.set({ |
547 notificationGroups: updatedGroups, | 545 notificationGroups: updatedGroups, |
548 recentDismissals: updatedRecentDismissals | 546 recentDismissals: updatedRecentDismissals |
549 }); | 547 }); |
550 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); | 548 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); |
551 }, | 549 }, |
552 onCardShown); | 550 onCardShown); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 }); | 586 }); |
589 | 587 |
590 console.log('requestNotificationGroups: request=' + requestParameters); | 588 console.log('requestNotificationGroups: request=' + requestParameters); |
591 | 589 |
592 var request = buildServerRequest('GET', 'notifications' + requestParameters); | 590 var request = buildServerRequest('GET', 'notifications' + requestParameters); |
593 | 591 |
594 request.onloadend = function(event) { | 592 request.onloadend = function(event) { |
595 console.log('requestNotificationGroups-onloadend ' + request.status); | 593 console.log('requestNotificationGroups-onloadend ' + request.status); |
596 if (request.status == HTTP_OK) { | 594 if (request.status == HTTP_OK) { |
597 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS); | 595 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS); |
598 parseAndShowNotificationCards(request.responseText, cardShownCallback); | 596 processServerResponse( |
| 597 JSON.parse(request.responseText), cardShownCallback); |
599 } | 598 } |
600 }; | 599 }; |
601 | 600 |
602 setAuthorization(request, function(success) { | 601 setAuthorization(request, function(success) { |
603 if (success) | 602 if (success) |
604 request.send(); | 603 request.send(); |
605 }); | 604 }); |
606 } | 605 } |
607 | 606 |
608 /** | 607 /** |
(...skipping 627 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1236 lastPollNowPayloads: items.lastPollNowPayloads, | 1235 lastPollNowPayloads: items.lastPollNowPayloads, |
1237 notificationGroups: items.notificationGroups | 1236 notificationGroups: items.notificationGroups |
1238 }); | 1237 }); |
1239 | 1238 |
1240 updateNotificationsCards(); | 1239 updateNotificationsCards(); |
1241 } | 1240 } |
1242 }); | 1241 }); |
1243 }); | 1242 }); |
1244 } | 1243 } |
1245 }); | 1244 }); |
OLD | NEW |