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 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 ', ERROR=' + errorMessage); | 274 ', ERROR=' + errorMessage); |
275 } | 275 } |
276 }); | 276 }); |
277 } catch (error) { | 277 } catch (error) { |
278 console.error('Error in notifications.update: ' + error); | 278 console.error('Error in notifications.update: ' + error); |
279 } | 279 } |
280 } | 280 } |
281 | 281 |
282 notificationsData[card.notificationId] = { | 282 notificationsData[card.notificationId] = { |
283 actionUrls: card.actionUrls, | 283 actionUrls: card.actionUrls, |
284 version: card.version | 284 version: card.version, |
| 285 dismissalParameters: card.dismissal |
285 }; | 286 }; |
286 } | 287 } |
287 | 288 |
288 /** | 289 /** |
289 * Parses JSON response from the notification server, show notifications and | 290 * Parses JSON response from the notification server, show notifications and |
290 * schedule next update. | 291 * schedule next update. |
291 * @param {string} response Server response. | 292 * @param {string} response Server response. |
292 * @param {function()} callback Completion callback. | 293 * @param {function()} callback Completion callback. |
293 */ | 294 */ |
294 function parseAndShowNotificationCards(response, callback) { | 295 function parseAndShowNotificationCards(response, callback) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 } | 406 } |
406 | 407 |
407 recordEvent(DiagnosticEvent.REQUEST_FOR_CARDS_TOTAL); | 408 recordEvent(DiagnosticEvent.REQUEST_FOR_CARDS_TOTAL); |
408 | 409 |
409 // TODO(vadimt): Should we use 'q' as the parameter name? | 410 // TODO(vadimt): Should we use 'q' as the parameter name? |
410 var requestParameters = | 411 var requestParameters = |
411 'q=' + position.coords.latitude + | 412 'q=' + position.coords.latitude + |
412 ',' + position.coords.longitude + | 413 ',' + position.coords.longitude + |
413 ',' + position.coords.accuracy; | 414 ',' + position.coords.accuracy; |
414 | 415 |
415 var request = buildServerRequest('notifications'); | 416 var request = buildServerRequest('notifications', |
| 417 'application/x-www-form-urlencoded'); |
416 | 418 |
417 request.onloadend = function(event) { | 419 request.onloadend = function(event) { |
418 console.log('requestNotificationCards-onloadend ' + request.status); | 420 console.log('requestNotificationCards-onloadend ' + request.status); |
419 if (request.status == HTTP_OK) { | 421 if (request.status == HTTP_OK) { |
420 recordEvent(DiagnosticEvent.REQUEST_FOR_CARDS_SUCCESS); | 422 recordEvent(DiagnosticEvent.REQUEST_FOR_CARDS_SUCCESS); |
421 parseAndShowNotificationCards(request.response, callback); | 423 parseAndShowNotificationCards(request.response, callback); |
422 } else { | 424 } else { |
423 callback(); | 425 callback(); |
424 } | 426 } |
425 }; | 427 }; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 }); | 468 }); |
467 }); | 469 }); |
468 }); | 470 }); |
469 } | 471 } |
470 | 472 |
471 /** | 473 /** |
472 * Sends a server request to dismiss a card. | 474 * Sends a server request to dismiss a card. |
473 * @param {string} notificationId Unique identifier of the card. | 475 * @param {string} notificationId Unique identifier of the card. |
474 * @param {number} dismissalTimeMs Time of the user's dismissal of the card in | 476 * @param {number} dismissalTimeMs Time of the user's dismissal of the card in |
475 * milliseconds since epoch. | 477 * milliseconds since epoch. |
| 478 * @param {Object} dismissalParameters Dismissal parameters. |
476 * @param {function(boolean)} callbackBoolean Completion callback with 'done' | 479 * @param {function(boolean)} callbackBoolean Completion callback with 'done' |
477 * parameter. | 480 * parameter. |
478 */ | 481 */ |
479 function requestCardDismissal( | 482 function requestCardDismissal( |
480 notificationId, dismissalTimeMs, callbackBoolean) { | 483 notificationId, dismissalTimeMs, dismissalParameters, callbackBoolean) { |
481 console.log('requestDismissingCard ' + notificationId + ' from ' + | 484 console.log('requestDismissingCard ' + notificationId + ' from ' + |
482 NOTIFICATION_CARDS_URL); | 485 NOTIFICATION_CARDS_URL); |
483 | 486 |
484 var dismissalAge = Date.now() - dismissalTimeMs; | 487 var dismissalAge = Date.now() - dismissalTimeMs; |
485 | 488 |
486 if (dismissalAge > MAXIMUM_DISMISSAL_AGE_MS) { | 489 if (dismissalAge > MAXIMUM_DISMISSAL_AGE_MS) { |
487 callbackBoolean(true); | 490 callbackBoolean(true); |
488 return; | 491 return; |
489 } | 492 } |
490 | 493 |
491 recordEvent(DiagnosticEvent.DISMISS_REQUEST_TOTAL); | 494 recordEvent(DiagnosticEvent.DISMISS_REQUEST_TOTAL); |
492 // Send a dismiss request to the server. | 495 var request = buildServerRequest('dismiss', 'application/json'); |
493 var requestParameters = 'id=' + notificationId + | |
494 '&dismissalAge=' + dismissalAge; | |
495 var request = buildServerRequest('dismiss'); | |
496 request.onloadend = function(event) { | 496 request.onloadend = function(event) { |
497 console.log('requestDismissingCard-onloadend ' + request.status); | 497 console.log('requestDismissingCard-onloadend ' + request.status); |
498 if (request.status == HTTP_OK) | 498 if (request.status == HTTP_OK) |
499 recordEvent(DiagnosticEvent.DISMISS_REQUEST_SUCCESS); | 499 recordEvent(DiagnosticEvent.DISMISS_REQUEST_SUCCESS); |
500 | 500 |
501 // A dismissal doesn't require further retries if it was successful or | 501 // A dismissal doesn't require further retries if it was successful or |
502 // doesn't have a chance for successful completion. | 502 // doesn't have a chance for successful completion. |
503 var done = request.status == HTTP_OK || | 503 var done = request.status == HTTP_OK || |
504 request.status == HTTP_BAD_REQUEST || | 504 request.status == HTTP_BAD_REQUEST || |
505 request.status == HTTP_METHOD_NOT_ALLOWED; | 505 request.status == HTTP_METHOD_NOT_ALLOWED; |
506 callbackBoolean(done); | 506 callbackBoolean(done); |
507 }; | 507 }; |
508 | 508 |
509 setAuthorization(request, function(success) { | 509 setAuthorization(request, function(success) { |
510 if (success) { | 510 if (success) { |
511 tasks.debugSetStepName('requestCardDismissal-send-request'); | 511 tasks.debugSetStepName('requestCardDismissal-send-request'); |
512 request.send(requestParameters); | 512 |
| 513 var dismissalObject = { |
| 514 id: notificationId, |
| 515 age: dismissalAge, |
| 516 dismissal: dismissalParameters |
| 517 }; |
| 518 request.send(JSON.stringify(dismissalObject)); |
513 } else { | 519 } else { |
514 callbackBoolean(false); | 520 callbackBoolean(false); |
515 } | 521 } |
516 }); | 522 }); |
517 } | 523 } |
518 | 524 |
519 /** | 525 /** |
520 * Tries to send dismiss requests for all pending dismissals. | 526 * Tries to send dismiss requests for all pending dismissals. |
521 * @param {function(boolean)} callbackBoolean Completion callback with 'success' | 527 * @param {function(boolean)} callbackBoolean Completion callback with 'success' |
522 * parameter. Success means that no pending dismissals are left. | 528 * parameter. Success means that no pending dismissals are left. |
(...skipping 22 matching lines...) Expand all Loading... |
545 if (items.pendingDismissals.length == 0) { | 551 if (items.pendingDismissals.length == 0) { |
546 dismissalAttempts.stop(); | 552 dismissalAttempts.stop(); |
547 onFinish(true); | 553 onFinish(true); |
548 return; | 554 return; |
549 } | 555 } |
550 | 556 |
551 // Send dismissal for the first card, and if successful, repeat | 557 // Send dismissal for the first card, and if successful, repeat |
552 // recursively with the rest. | 558 // recursively with the rest. |
553 var dismissal = items.pendingDismissals[0]; | 559 var dismissal = items.pendingDismissals[0]; |
554 requestCardDismissal( | 560 requestCardDismissal( |
555 dismissal.notificationId, dismissal.time, function(done) { | 561 dismissal.notificationId, |
556 if (done) { | 562 dismissal.time, |
557 dismissalsChanged = true; | 563 dismissal.parameters, |
558 items.pendingDismissals.splice(0, 1); | 564 function(done) { |
559 items.recentDismissals[dismissal.notificationId] = Date.now(); | 565 if (done) { |
560 doProcessDismissals(); | 566 dismissalsChanged = true; |
561 } else { | 567 items.pendingDismissals.splice(0, 1); |
562 onFinish(false); | 568 items.recentDismissals[dismissal.notificationId] = Date.now(); |
563 } | 569 doProcessDismissals(); |
564 }); | 570 } else { |
| 571 onFinish(false); |
| 572 } |
| 573 }); |
565 } | 574 } |
566 | 575 |
567 doProcessDismissals(); | 576 doProcessDismissals(); |
568 }); | 577 }); |
569 } | 578 } |
570 | 579 |
571 /** | 580 /** |
572 * Submits a task to send pending dismissals. | 581 * Submits a task to send pending dismissals. |
573 */ | 582 */ |
574 function retryPendingDismissals() { | 583 function retryPendingDismissals() { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 | 667 |
659 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { | 668 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { |
660 dismissalAttempts.start(); | 669 dismissalAttempts.start(); |
661 | 670 |
662 // Deleting the notification in case it was re-added while this task was | 671 // Deleting the notification in case it was re-added while this task was |
663 // scheduled, waiting for execution. | 672 // scheduled, waiting for execution. |
664 chrome.notifications.clear( | 673 chrome.notifications.clear( |
665 notificationId, | 674 notificationId, |
666 function() {}); | 675 function() {}); |
667 | 676 |
668 tasks.debugSetStepName('onNotificationClosed-get-pendingDismissals'); | 677 tasks.debugSetStepName('onNotificationClosed-storage-get'); |
669 storage.get('pendingDismissals', function(items) { | 678 storage.get(['pendingDismissals', 'notificationsData'], function(items) { |
670 items.pendingDismissals = items.pendingDismissals || []; | 679 items.pendingDismissals = items.pendingDismissals || []; |
| 680 items.notificationsData = items.notificationsData || {}; |
| 681 |
| 682 var notificationData = items.notificationsData[notificationId]; |
671 | 683 |
672 var dismissal = { | 684 var dismissal = { |
673 notificationId: notificationId, | 685 notificationId: notificationId, |
674 time: Date.now() | 686 time: Date.now(), |
| 687 parameters: notificationData && notificationData.dismissalParameters |
675 }; | 688 }; |
676 items.pendingDismissals.push(dismissal); | 689 items.pendingDismissals.push(dismissal); |
677 storage.set({pendingDismissals: items.pendingDismissals}); | 690 storage.set({pendingDismissals: items.pendingDismissals}); |
678 processPendingDismissals(function(success) { callback(); }); | 691 processPendingDismissals(function(success) { callback(); }); |
679 }); | 692 }); |
680 }); | 693 }); |
681 } | 694 } |
682 | 695 |
683 /** | 696 /** |
684 * Initializes the polling system to start monitoring location and fetching | 697 * Initializes the polling system to start monitoring location and fetching |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
778 | 791 |
779 chrome.location.onLocationUpdate.addListener(function(position) { | 792 chrome.location.onLocationUpdate.addListener(function(position) { |
780 recordEvent(DiagnosticEvent.LOCATION_UPDATE); | 793 recordEvent(DiagnosticEvent.LOCATION_UPDATE); |
781 updateNotificationsCards(position); | 794 updateNotificationsCards(position); |
782 }); | 795 }); |
783 | 796 |
784 chrome.omnibox.onInputEntered.addListener(function(text) { | 797 chrome.omnibox.onInputEntered.addListener(function(text) { |
785 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 798 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
786 initialize(); | 799 initialize(); |
787 }); | 800 }); |
OLD | NEW |