Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / google_now / cards.js
index 00c9b5d..4eb2af4 100644 (file)
@@ -16,16 +16,17 @@ var Trigger;
 
 /**
  * ID of an individual (uncombined) notification.
+ * This ID comes directly from the server.
  *
  * @typedef {string}
  */
-var NotificationId;
+var ServerNotificationId;
 
 /**
  * Data to build a dismissal request for a card from a specific group.
  *
  * @typedef {{
- *   notificationId: NotificationId,
+ *   notificationId: ServerNotificationId,
  *   parameters: Object
  * }}
  */
@@ -42,8 +43,8 @@ var DismissalData;
 var ActionUrls;
 
 /**
- * ID of a combined notification. This is the ID used with chrome.notifications
- * API.
+ * ID of a combined notification.
+ * This is the ID used with chrome.notifications API.
  *
  * @typedef {string}
  */
@@ -53,7 +54,7 @@ var ChromeNotificationId;
  * Notification as sent by the server.
  *
  * @typedef {{
- *   notificationId: NotificationId,
+ *   notificationId: ServerNotificationId,
  *   chromeNotificationId: ChromeNotificationId,
  *   trigger: Trigger,
  *   chromeNotificationOptions: Object,
@@ -114,41 +115,45 @@ function buildCardSet() {
 
   /**
    * Creates/updates/deletes a Chrome notification.
-   * @param {ChromeNotificationId} cardId Card ID.
+   * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID
+   *     of the card.
    * @param {(ReceivedNotification|undefined)} receivedNotification Google Now
    *     card represented as a set of parameters for showing a Chrome
    *     notification, or null if the notification needs to be deleted.
    * @param {function(ReceivedNotification)=} onCardShown Optional parameter
    *     called when each card is shown.
    */
-  function updateNotification(cardId, receivedNotification, onCardShown) {
-    console.log('cardManager.updateNotification ' + cardId + ' ' +
-                JSON.stringify(receivedNotification));
+  function updateNotification(
+      chromeNotificationId, receivedNotification, onCardShown) {
+    console.log(
+        'cardManager.updateNotification ' + chromeNotificationId + ' ' +
+        JSON.stringify(receivedNotification));
 
     if (!receivedNotification) {
-      instrumented.notifications.clear(cardId, function() {});
+      instrumented.notifications.clear(chromeNotificationId, function() {});
       return;
     }
 
     // Try updating the notification.
     instrumented.notifications.update(
-        cardId,
+        chromeNotificationId,
         receivedNotification.chromeNotificationOptions,
         function(wasUpdated) {
           if (!wasUpdated) {
             // If the notification wasn't updated, it probably didn't exist.
             // Create it.
-            console.log('cardManager.updateNotification ' + cardId +
-                        ' failed to update, creating');
+            console.log(
+                'cardManager.updateNotification ' + chromeNotificationId +
+                ' failed to update, creating');
             instrumented.notifications.create(
-                cardId,
+                chromeNotificationId,
                 receivedNotification.chromeNotificationOptions,
-                function(newNotificationId) {
-                  if (!newNotificationId || chrome.runtime.lastError) {
+                function(newChromeNotificationId) {
+                  if (!newChromeNotificationId || chrome.runtime.lastError) {
                     var errorMessage = chrome.runtime.lastError &&
                                        chrome.runtime.lastError.message;
                     console.error('notifications.create: ID=' +
-                        newNotificationId + ', ERROR=' + errorMessage);
+                        newChromeNotificationId + ', ERROR=' + errorMessage);
                     return;
                   }
 
@@ -183,8 +188,10 @@ function buildCardSet() {
   /**
    * Refreshes (shows/hides) the notification corresponding to the combined card
    * based on the current time and show-hide intervals in the combined card.
-   * @param {ChromeNotificationId} cardId Card ID.
-   * @param {CombinedCard} combinedCard Combined cards with |cardId|.
+   * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID
+   *     of the card.
+   * @param {CombinedCard} combinedCard Combined cards with
+   *     |chromeNotificationId|.
    * @param {Object.<string, StoredNotificationGroup>} notificationGroups
    *     Map from group name to group information.
    * @param {function(ReceivedNotification)=} onCardShown Optional parameter
@@ -192,10 +199,11 @@ function buildCardSet() {
    * @return {(NotificationDataEntry|undefined)} Notification data entry for
    *     this card. It's 'undefined' if the card's life is over.
    */
-  function update(cardId, combinedCard, notificationGroups, onCardShown) {
+  function update(
+      chromeNotificationId, combinedCard, notificationGroups, onCardShown) {
     console.log('cardManager.update ' + JSON.stringify(combinedCard));
 
-    chrome.alarms.clear(alarmPrefix + cardId);
+    chrome.alarms.clear(alarmPrefix + chromeNotificationId);
     var now = Date.now();
     /** @type {(UncombinedNotification|undefined)} */
     var winningCard = undefined;
@@ -235,11 +243,14 @@ function buildCardSet() {
 
     // Show/hide the winning card.
     updateNotification(
-        cardId, winningCard && winningCard.receivedNotification, onCardShown);
+        chromeNotificationId,
+        winningCard && winningCard.receivedNotification,
+        onCardShown);
 
     if (nextEventTime) {
       // If we expect more events, create an alarm for the next one.
-      chrome.alarms.create(alarmPrefix + cardId, {when: nextEventTime});
+      chrome.alarms.create(
+          alarmPrefix + chromeNotificationId, {when: nextEventTime});
 
       // The trick with stringify/parse is to create a copy of action URLs,
       // otherwise notifications data with 2 pointers to the same object won't
@@ -260,7 +271,7 @@ function buildCardSet() {
       // If there are no more events, we are done with this card. Note that all
       // received notifications have hideTime.
       verify(!winningCard, 'No events left, but card is shown.');
-      clearCardFromGroups(cardId, notificationGroups);
+      clearCardFromGroups(chromeNotificationId, notificationGroups);
       return undefined;
     }
   }
@@ -268,7 +279,8 @@ function buildCardSet() {
   /**
    * Removes dismissed part of a card and refreshes the card. Returns remaining
    * dismissals for the combined card and updated notification data.
-   * @param {ChromeNotificationId} cardId Card ID.
+   * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID
+   *     of the card.
    * @param {NotificationDataEntry} notificationData Stored notification entry
    *     for this card.
    * @param {Object.<string, StoredNotificationGroup>} notificationGroups
@@ -278,8 +290,11 @@ function buildCardSet() {
    *   notificationData: (NotificationDataEntry|undefined)
    * }}
    */
-  function onDismissal(cardId, notificationData, notificationGroups) {
+  function onDismissal(
+      chromeNotificationId, notificationData, notificationGroups) {
+    /** @type {Array.<DismissalData>} */
     var dismissals = [];
+    /** @type {Array.<UncombinedNotification>} */
     var newCombinedCard = [];
 
     // Determine which parts of the combined card need to be dismissed or to be
@@ -301,22 +316,24 @@ function buildCardSet() {
 
     return {
       dismissals: dismissals,
-      notificationData: update(cardId, newCombinedCard, notificationGroups)
+      notificationData: update(
+          chromeNotificationId, newCombinedCard, notificationGroups)
     };
   }
 
   /**
    * Removes card information from |notificationGroups|.
-   * @param {ChromeNotificationId} cardId Card ID.
+   * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID
+   *     of the card.
    * @param {Object.<string, StoredNotificationGroup>} notificationGroups
    *     Map from group name to group information.
    */
-  function clearCardFromGroups(cardId, notificationGroups) {
-    console.log('cardManager.clearCardFromGroups ' + cardId);
+  function clearCardFromGroups(chromeNotificationId, notificationGroups) {
+    console.log('cardManager.clearCardFromGroups ' + chromeNotificationId);
     for (var groupName in notificationGroups) {
       var group = notificationGroups[groupName];
       for (var i = 0; i != group.cards.length; ++i) {
-        if (group.cards[i].chromeNotificationId == cardId) {
+        if (group.cards[i].chromeNotificationId == chromeNotificationId) {
           group.cards.splice(i, 1);
           break;
         }
@@ -330,9 +347,10 @@ function buildCardSet() {
     if (alarm.name.indexOf(alarmPrefix) == 0) {
       // Alarm to show the card.
       tasks.add(UPDATE_CARD_TASK_NAME, function() {
-        var cardId = alarm.name.substring(alarmPrefix.length);
+        /** @type {ChromeNotificationId} */
+        var chromeNotificationId = alarm.name.substring(alarmPrefix.length);
         fillFromChromeLocalStorage({
-          /** @type {Object.<string, NotificationDataEntry>} */
+          /** @type {Object.<ChromeNotificationId, NotificationDataEntry>} */
           notificationsData: {},
           /** @type {Object.<string, StoredNotificationGroup>} */
           notificationGroups: {}
@@ -340,8 +358,8 @@ function buildCardSet() {
           console.log('cardManager.onAlarm.get ' + JSON.stringify(items));
 
           var combinedCard =
-            (items.notificationsData[cardId] &&
-             items.notificationsData[cardId].combinedCard) || [];
+            (items.notificationsData[chromeNotificationId] &&
+             items.notificationsData[chromeNotificationId].combinedCard) || [];
 
           var cardShownCallback = undefined;
           if (localStorage['explanatoryCardsShown'] <
@@ -349,9 +367,9 @@ function buildCardSet() {
              cardShownCallback = countExplanatoryCard;
           }
 
-          items.notificationsData[cardId] =
+          items.notificationsData[chromeNotificationId] =
               update(
-                  cardId,
+                  chromeNotificationId,
                   combinedCard,
                   items.notificationGroups,
                   cardShownCallback);