/**
* 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
* }}
*/
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}
*/
* Notification as sent by the server.
*
* @typedef {{
- * notificationId: NotificationId,
+ * notificationId: ServerNotificationId,
* chromeNotificationId: ChromeNotificationId,
* trigger: Trigger,
* chromeNotificationOptions: Object,
/**
* 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;
}
/**
* 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
* @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;
// 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
// 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;
}
}
/**
* 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
* 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
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;
}
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: {}
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'] <
cardShownCallback = countExplanatoryCard;
}
- items.notificationsData[cardId] =
+ items.notificationsData[chromeNotificationId] =
update(
- cardId,
+ chromeNotificationId,
combinedCard,
items.notificationGroups,
cardShownCallback);