 Chromium Code Reviews
 Chromium Code Reviews Issue 19749007:
  Processing timefences from the server.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 19749007:
  Processing timefences from the server.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: chrome/browser/resources/google_now/cards.js | 
| diff --git a/chrome/browser/resources/google_now/cards.js b/chrome/browser/resources/google_now/cards.js | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..c38f1eacee1d3b0987927c4092c304ffcf92d68f | 
| --- /dev/null | 
| +++ b/chrome/browser/resources/google_now/cards.js | 
| @@ -0,0 +1,175 @@ | 
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +'use strict'; | 
| + | 
| +var MS_IN_SECOND = 1000; | 
| + | 
| +/** | 
| + * Builds an object to manage notification cards. | 
| + * @return {Object} Card manager interface. | 
| + */ | 
| +function buildCardManager() { | 
| + var cardShowPrefix = 'card-show-'; | 
| + var cardShowPrefixLength = cardShowPrefix.length; | 
| 
rgustafson
2013/07/19 23:37:12
Does this actually save anything with the page bei
 
vadimt
2013/07/22 19:22:52
Done.
 | 
| + var cardHidePrefix = 'card-hide-'; | 
| + var cardHidePrefixLength = cardHidePrefix.length; | 
| + | 
| + /** | 
| + * Schedules hiding a notification. | 
| + * @param {string} cardId Card ID. | 
| + * @param {number=} opt_timeHide If specified, epoch time to hide the card. If | 
| + * undefined, the card will be kept shown at least until next update. | 
| + */ | 
| + function scheduleHiding(cardId, opt_timeHide) { | 
| + if (opt_timeHide === undefined) | 
| + return; | 
| + | 
| + var alarmName = cardHidePrefix + cardId; | 
| + var alarmInfo = {when: opt_timeHide}; | 
| + chrome.alarms.create(alarmName, alarmInfo); | 
| + } | 
| + | 
| + /** | 
| + * Shows a notification. | 
| + * @param {string} cardId Card ID. | 
| + * @param {Object} cardCreateInfo Google Now card represented as a set of | 
| + * parameters for showing a Chrome notification. | 
| + * @param {number=} opt_previousVersion The version of the shown card with | 
| + * this id, if it exists, undefined otherwise. | 
| + */ | 
| + function showNotification(cardId, cardCreateInfo, opt_previousVersion) { | 
| + console.log('cardManager.showNotification ' + cardId + ' ' + | 
| + JSON.stringify(cardCreateInfo) + ' ' + opt_previousVersion); | 
| + | 
| + if (opt_previousVersion !== cardCreateInfo.version) { | 
| + try { | 
| + // Delete a notification with the specified id if it already exists, and | 
| + // then create a notification. | 
| + chrome.notifications.create( | 
| + cardId, | 
| + cardCreateInfo.notification, | 
| + function(newNotificationId) { | 
| + if (!newNotificationId || chrome.runtime.lastError) { | 
| + var errorMessage = chrome.runtime.lastError && | 
| + chrome.runtime.lastError.message; | 
| + console.error('notifications.create: ID=' + newNotificationId + | 
| + ', ERROR=' + errorMessage); | 
| + return; | 
| + } | 
| + | 
| + scheduleHiding(cardId, cardCreateInfo.timeHide); | 
| + }); | 
| + } catch (error) { | 
| + console.error('Error in notifications.create: ' + error); | 
| + } | 
| + } else { | 
| + try { | 
| + // Update existing notification. | 
| + chrome.notifications.update( | 
| + cardId, | 
| + cardCreateInfo.notification, | 
| + function(wasUpdated) { | 
| + if (!wasUpdated || chrome.runtime.lastError) { | 
| + var errorMessage = chrome.runtime.lastError && | 
| + chrome.runtime.lastError.message; | 
| + console.error('notifications.update: UPDATED=' + wasUpdated + | 
| + ', ERROR=' + errorMessage); | 
| + return; | 
| + } | 
| + | 
| + scheduleHiding(cardId, cardCreateInfo.timeHide); | 
| + }); | 
| + } catch (error) { | 
| + console.error('Error in notifications.update: ' + error); | 
| + } | 
| + } | 
| + } | 
| + | 
| + /** | 
| + * Updates/creates a card notification with new data. | 
| + * @param {Object} card Google Now from the server. | 
| + * @param {number=} opt_previousVersion The version of the shown card with | 
| + * this id, if it exists, undefined otherwise. | 
| + * @return {Object} Notification data entry for this card. | 
| + */ | 
| + function update(card, opt_previousVersion) { | 
| + console.log('cardManager.update ' + JSON.stringify(card) + ' ' + | 
| + opt_previousVersion); | 
| + | 
| + if (typeof card.version != 'number') { | 
| + console.log('cardCreateInfo.version is not a number'); | 
| + // Fix card version. | 
| + card.version = opt_previousVersion || 0; | 
| + } | 
| + | 
| + chrome.alarms.clear(cardHidePrefix + card.notificationId); | 
| 
rgustafson
2013/07/19 23:37:12
I don't like the fact that this (and a few other p
 
vadimt
2013/07/22 19:22:52
Added TODO item. With status quo this is hard to d
 | 
| + | 
| + var timeHide = card.trigger && card.trigger.hideTimeSec !== undefined ? | 
| + Date.now() + card.trigger.hideTimeSec * MS_IN_SECOND : undefined; | 
| + var cardCreateInfo = { | 
| + notification: card.notification, | 
| + timeHide: timeHide, | 
| + version: card.version | 
| + }; | 
| + | 
| + var cardShowAlarmName = cardShowPrefix + card.notificationId; | 
| + if (card.trigger && card.trigger.showTimeSec) { | 
| + // Card needs to be shown later. | 
| + console.log('cardManager.register: postponed'); | 
| + chrome.notifications.clear(card.notificationId, function() {}); | 
| 
rgustafson
2013/07/19 23:37:12
This is a weird case. What if there is a low prior
 
vadimt
2013/07/22 19:22:52
Done.
 | 
| + var alarmInfo = { | 
| + when: Date.now() + card.trigger.showTimeSec * MS_IN_SECOND | 
| + }; | 
| + chrome.alarms.create(cardShowAlarmName, alarmInfo); | 
| + } else { | 
| + // Card needs to be shown immediately. | 
| + console.log('cardManager.register: immediate'); | 
| + chrome.alarms.clear(cardShowAlarmName); | 
| + showNotification( | 
| + card.notificationId, cardCreateInfo, opt_previousVersion); | 
| + } | 
| + | 
| + return {actionUrls: card.actionUrls, cardCreateInfo: cardCreateInfo}; | 
| + } | 
| + | 
| + /** | 
| + * Removes a card notification. | 
| + * @param {string} cardId Card ID. | 
| + */ | 
| + function clear(cardId) { | 
| + console.log('cardManager.unregister ' + cardId); | 
| + | 
| + chrome.notifications.clear(cardId, function() {}); | 
| + chrome.alarms.clear(cardShowPrefix + cardId); | 
| + chrome.alarms.clear(cardHidePrefix + cardId); | 
| + } | 
| + | 
| + chrome.alarms.onAlarm.addListener(function(alarm) { | 
| + console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); | 
| + | 
| + if (alarm.name.indexOf(cardShowPrefix) == 0) { | 
| + // Alarm to show the card. | 
| + var cardId = alarm.name.substring(cardShowPrefixLength); | 
| + storage.get('notificationsData', function(items) { | 
| + items.notificationsData = items.notificationsData || {}; | 
| + console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); | 
| + var notificationData = items.notificationsData[cardId]; | 
| + if (!notificationData) | 
| + return; | 
| + | 
| + showNotification(cardId, notificationData.cardCreateInfo); | 
| + }); | 
| + } else if (alarm.name.indexOf(cardHidePrefix) == 0) { | 
| + // Alarm to hide the card. | 
| + var cardId = alarm.name.substring(cardHidePrefixLength); | 
| + chrome.notifications.clear(cardId, function() {}); | 
| + } | 
| + }); | 
| + | 
| + return { | 
| + update: update, | 
| + clear: clear | 
| + }; | 
| +} |