Chromium Code Reviews| 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 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 ', ERROR=' + errorMessage); | 267 ', ERROR=' + errorMessage); |
| 268 } | 268 } |
| 269 }); | 269 }); |
| 270 } catch (error) { | 270 } catch (error) { |
| 271 console.error('Error in notifications.update: ' + error); | 271 console.error('Error in notifications.update: ' + error); |
| 272 } | 272 } |
| 273 } | 273 } |
| 274 | 274 |
| 275 notificationsData[card.notificationId] = { | 275 notificationsData[card.notificationId] = { |
| 276 actionUrls: card.actionUrls, | 276 actionUrls: card.actionUrls, |
| 277 version: card.version | 277 version: card.version, |
| 278 dismissalParameters: card.dismissal | |
| 278 }; | 279 }; |
| 279 } | 280 } |
| 280 | 281 |
| 281 /** | 282 /** |
| 282 * Parses JSON response from the notification server, show notifications and | 283 * Parses JSON response from the notification server, show notifications and |
| 283 * schedule next update. | 284 * schedule next update. |
| 284 * @param {string} response Server response. | 285 * @param {string} response Server response. |
| 285 * @param {function()} callback Completion callback. | 286 * @param {function()} callback Completion callback. |
| 286 */ | 287 */ |
| 287 function parseAndShowNotificationCards(response, callback) { | 288 function parseAndShowNotificationCards(response, callback) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 } | 399 } |
| 399 | 400 |
| 400 recordEvent(DiagnosticEvent.REQUEST_FOR_CARDS_TOTAL); | 401 recordEvent(DiagnosticEvent.REQUEST_FOR_CARDS_TOTAL); |
| 401 | 402 |
| 402 // TODO(vadimt): Should we use 'q' as the parameter name? | 403 // TODO(vadimt): Should we use 'q' as the parameter name? |
| 403 var requestParameters = | 404 var requestParameters = |
| 404 'q=' + position.coords.latitude + | 405 'q=' + position.coords.latitude + |
| 405 ',' + position.coords.longitude + | 406 ',' + position.coords.longitude + |
| 406 ',' + position.coords.accuracy; | 407 ',' + position.coords.accuracy; |
| 407 | 408 |
| 408 var request = buildServerRequest('notifications'); | 409 var request = buildServerRequest('notifications', |
| 410 'application/x-www-form-urlencoded'); | |
| 409 | 411 |
| 410 request.onloadend = function(event) { | 412 request.onloadend = function(event) { |
| 411 console.log('requestNotificationCards-onloadend ' + request.status); | 413 console.log('requestNotificationCards-onloadend ' + request.status); |
| 412 if (request.status == HTTP_OK) { | 414 if (request.status == HTTP_OK) { |
| 413 recordEvent(DiagnosticEvent.REQUEST_FOR_CARDS_SUCCESS); | 415 recordEvent(DiagnosticEvent.REQUEST_FOR_CARDS_SUCCESS); |
| 414 parseAndShowNotificationCards(request.response, callback); | 416 parseAndShowNotificationCards(request.response, callback); |
| 415 } else { | 417 } else { |
| 416 callback(); | 418 callback(); |
| 417 } | 419 } |
| 418 }; | 420 }; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 459 }); | 461 }); |
| 460 }); | 462 }); |
| 461 }); | 463 }); |
| 462 } | 464 } |
| 463 | 465 |
| 464 /** | 466 /** |
| 465 * Sends a server request to dismiss a card. | 467 * Sends a server request to dismiss a card. |
| 466 * @param {string} notificationId Unique identifier of the card. | 468 * @param {string} notificationId Unique identifier of the card. |
| 467 * @param {number} dismissalTimeMs Time of the user's dismissal of the card in | 469 * @param {number} dismissalTimeMs Time of the user's dismissal of the card in |
| 468 * milliseconds since epoch. | 470 * milliseconds since epoch. |
| 471 * @param {Object} dismissalParameters Dismissal parameters. | |
| 469 * @param {function(boolean)} callbackBoolean Completion callback with 'success' | 472 * @param {function(boolean)} callbackBoolean Completion callback with 'success' |
| 470 * parameter. | 473 * parameter. |
| 471 */ | 474 */ |
| 472 function requestCardDismissal( | 475 function requestCardDismissal( |
| 473 notificationId, dismissalTimeMs, callbackBoolean) { | 476 notificationId, dismissalTimeMs, dismissalParameters, callbackBoolean) { |
| 474 console.log('requestDismissingCard ' + notificationId + ' from ' + | 477 console.log('requestDismissingCard ' + notificationId + ' from ' + |
| 475 NOTIFICATION_CARDS_URL); | 478 NOTIFICATION_CARDS_URL); |
| 476 recordEvent(DiagnosticEvent.DISMISS_REQUEST_TOTAL); | 479 recordEvent(DiagnosticEvent.DISMISS_REQUEST_TOTAL); |
| 477 // Send a dismiss request to the server. | 480 var request = buildServerRequest('dismiss', 'application/json'); |
| 478 var requestParameters = 'id=' + notificationId + | |
| 479 '&dismissalAge=' + (Date.now() - dismissalTimeMs); | |
| 480 var request = buildServerRequest('dismiss'); | |
| 481 request.onloadend = function(event) { | 481 request.onloadend = function(event) { |
| 482 console.log('requestDismissingCard-onloadend ' + request.status); | 482 console.log('requestDismissingCard-onloadend ' + request.status); |
| 483 if (request.status == HTTP_OK) | 483 if (request.status == HTTP_OK) |
| 484 recordEvent(DiagnosticEvent.DISMISS_REQUEST_SUCCESS); | 484 recordEvent(DiagnosticEvent.DISMISS_REQUEST_SUCCESS); |
| 485 | 485 |
| 486 callbackBoolean(request.status == HTTP_OK); | 486 callbackBoolean(request.status == HTTP_OK); |
| 487 }; | 487 }; |
| 488 | 488 |
| 489 setAuthorization(request, function(success) { | 489 setAuthorization(request, function(success) { |
| 490 if (success) { | 490 if (success) { |
| 491 tasks.debugSetStepName('requestCardDismissal-send-request'); | 491 tasks.debugSetStepName('requestCardDismissal-send-request'); |
| 492 request.send(requestParameters); | 492 |
| 493 var dismissalObject = { | |
| 494 id: notificationId, | |
| 495 age: Date.now() - dismissalTimeMs, | |
| 496 dismissal: dismissalParameters | |
| 497 }; | |
| 498 request.send(JSON.stringify(dismissalObject)); | |
| 493 } else { | 499 } else { |
| 494 callbackBoolean(false); | 500 callbackBoolean(false); |
| 495 } | 501 } |
| 496 }); | 502 }); |
| 497 } | 503 } |
| 498 | 504 |
| 499 /** | 505 /** |
| 500 * Tries to send dismiss requests for all pending dismissals. | 506 * Tries to send dismiss requests for all pending dismissals. |
| 501 * @param {function(boolean)} callbackBoolean Completion callback with 'success' | 507 * @param {function(boolean)} callbackBoolean Completion callback with 'success' |
| 502 * parameter. Success means that no pending dismissals are left. | 508 * parameter. Success means that no pending dismissals are left. |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 524 function doProcessDismissals() { | 530 function doProcessDismissals() { |
| 525 if (items.pendingDismissals.length == 0) { | 531 if (items.pendingDismissals.length == 0) { |
| 526 dismissalAttempts.stop(); | 532 dismissalAttempts.stop(); |
| 527 onFinish(true); | 533 onFinish(true); |
| 528 return; | 534 return; |
| 529 } | 535 } |
| 530 | 536 |
| 531 // Send dismissal for the first card, and if successful, repeat | 537 // Send dismissal for the first card, and if successful, repeat |
| 532 // recursively with the rest. | 538 // recursively with the rest. |
| 533 var dismissal = items.pendingDismissals[0]; | 539 var dismissal = items.pendingDismissals[0]; |
| 534 requestCardDismissal( | 540 requestCardDismissal(dismissal.notificationId, |
| 535 dismissal.notificationId, dismissal.time, function(success) { | 541 dismissal.time, |
| 542 dismissal.parameters, | |
| 543 function(success) { | |
|
arv (Not doing code reviews)
2013/07/10 22:19:57
strange indentation
vadimt
2013/07/10 22:47:18
Done.
| |
| 536 if (success) { | 544 if (success) { |
| 537 dismissalsChanged = true; | 545 dismissalsChanged = true; |
| 538 items.pendingDismissals.splice(0, 1); | 546 items.pendingDismissals.splice(0, 1); |
| 539 items.recentDismissals[dismissal.notificationId] = Date.now(); | 547 items.recentDismissals[dismissal.notificationId] = Date.now(); |
| 540 doProcessDismissals(); | 548 doProcessDismissals(); |
| 541 } else { | 549 } else { |
| 542 onFinish(false); | 550 onFinish(false); |
| 543 } | 551 } |
| 544 }); | 552 }); |
| 545 } | 553 } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 638 | 646 |
| 639 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { | 647 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { |
| 640 dismissalAttempts.start(); | 648 dismissalAttempts.start(); |
| 641 | 649 |
| 642 // Deleting the notification in case it was re-added while this task was | 650 // Deleting the notification in case it was re-added while this task was |
| 643 // scheduled, waiting for execution. | 651 // scheduled, waiting for execution. |
| 644 chrome.notifications.clear( | 652 chrome.notifications.clear( |
| 645 notificationId, | 653 notificationId, |
| 646 function() {}); | 654 function() {}); |
| 647 | 655 |
| 648 tasks.debugSetStepName('onNotificationClosed-get-pendingDismissals'); | 656 tasks.debugSetStepName('onNotificationClosed-storage-get'); |
| 649 storage.get('pendingDismissals', function(items) { | 657 storage.get(['pendingDismissals', 'notificationsData'], function(items) { |
| 650 items.pendingDismissals = items.pendingDismissals || []; | 658 items.pendingDismissals = items.pendingDismissals || []; |
| 659 items.notificationsData = items.notificationsData || {}; | |
| 660 | |
| 661 var notificationData = items.notificationsData[notificationId]; | |
| 651 | 662 |
| 652 var dismissal = { | 663 var dismissal = { |
| 653 notificationId: notificationId, | 664 notificationId: notificationId, |
| 654 time: Date.now() | 665 time: Date.now(), |
| 666 parameters: notificationData && notificationData.dismissalParameters | |
| 655 }; | 667 }; |
| 656 items.pendingDismissals.push(dismissal); | 668 items.pendingDismissals.push(dismissal); |
| 657 storage.set({pendingDismissals: items.pendingDismissals}); | 669 storage.set({pendingDismissals: items.pendingDismissals}); |
| 658 processPendingDismissals(function(success) { callback(); }); | 670 processPendingDismissals(function(success) { callback(); }); |
| 659 }); | 671 }); |
| 660 }); | 672 }); |
| 661 } | 673 } |
| 662 | 674 |
| 663 /** | 675 /** |
| 664 * Initializes the polling system to start monitoring location and fetching | 676 * Initializes the polling system to start monitoring location and fetching |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 753 | 765 |
| 754 chrome.location.onLocationUpdate.addListener(function(position) { | 766 chrome.location.onLocationUpdate.addListener(function(position) { |
| 755 recordEvent(DiagnosticEvent.LOCATION_UPDATE); | 767 recordEvent(DiagnosticEvent.LOCATION_UPDATE); |
| 756 updateNotificationsCards(position); | 768 updateNotificationsCards(position); |
| 757 }); | 769 }); |
| 758 | 770 |
| 759 chrome.omnibox.onInputEntered.addListener(function(text) { | 771 chrome.omnibox.onInputEntered.addListener(function(text) { |
| 760 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 772 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
| 761 initialize(); | 773 initialize(); |
| 762 }); | 774 }); |
| OLD | NEW |