OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 'use strict'; | |
6 | |
7 var MS_IN_SECOND = 1000; | |
8 | |
9 /** | |
10 * Builds an object to manage notification cards. | |
11 * @return {Object} Card manager interface. | |
12 */ | |
13 function buildCardManager() { | |
14 var cardShowPrefix = 'card-show-'; | |
15 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.
| |
16 var cardHidePrefix = 'card-hide-'; | |
17 var cardHidePrefixLength = cardHidePrefix.length; | |
18 | |
19 /** | |
20 * Schedules hiding a notification. | |
21 * @param {string} cardId Card ID. | |
22 * @param {number=} opt_timeHide If specified, epoch time to hide the card. If | |
23 * undefined, the card will be kept shown at least until next update. | |
24 */ | |
25 function scheduleHiding(cardId, opt_timeHide) { | |
26 if (opt_timeHide === undefined) | |
27 return; | |
28 | |
29 var alarmName = cardHidePrefix + cardId; | |
30 var alarmInfo = {when: opt_timeHide}; | |
31 chrome.alarms.create(alarmName, alarmInfo); | |
32 } | |
33 | |
34 /** | |
35 * Shows a notification. | |
36 * @param {string} cardId Card ID. | |
37 * @param {Object} cardCreateInfo Google Now card represented as a set of | |
38 * parameters for showing a Chrome notification. | |
39 * @param {number=} opt_previousVersion The version of the shown card with | |
40 * this id, if it exists, undefined otherwise. | |
41 */ | |
42 function showNotification(cardId, cardCreateInfo, opt_previousVersion) { | |
43 console.log('cardManager.showNotification ' + cardId + ' ' + | |
44 JSON.stringify(cardCreateInfo) + ' ' + opt_previousVersion); | |
45 | |
46 if (opt_previousVersion !== cardCreateInfo.version) { | |
47 try { | |
48 // Delete a notification with the specified id if it already exists, and | |
49 // then create a notification. | |
50 chrome.notifications.create( | |
51 cardId, | |
52 cardCreateInfo.notification, | |
53 function(newNotificationId) { | |
54 if (!newNotificationId || chrome.runtime.lastError) { | |
55 var errorMessage = chrome.runtime.lastError && | |
56 chrome.runtime.lastError.message; | |
57 console.error('notifications.create: ID=' + newNotificationId + | |
58 ', ERROR=' + errorMessage); | |
59 return; | |
60 } | |
61 | |
62 scheduleHiding(cardId, cardCreateInfo.timeHide); | |
63 }); | |
64 } catch (error) { | |
65 console.error('Error in notifications.create: ' + error); | |
66 } | |
67 } else { | |
68 try { | |
69 // Update existing notification. | |
70 chrome.notifications.update( | |
71 cardId, | |
72 cardCreateInfo.notification, | |
73 function(wasUpdated) { | |
74 if (!wasUpdated || chrome.runtime.lastError) { | |
75 var errorMessage = chrome.runtime.lastError && | |
76 chrome.runtime.lastError.message; | |
77 console.error('notifications.update: UPDATED=' + wasUpdated + | |
78 ', ERROR=' + errorMessage); | |
79 return; | |
80 } | |
81 | |
82 scheduleHiding(cardId, cardCreateInfo.timeHide); | |
83 }); | |
84 } catch (error) { | |
85 console.error('Error in notifications.update: ' + error); | |
86 } | |
87 } | |
88 } | |
89 | |
90 /** | |
91 * Updates/creates a card notification with new data. | |
92 * @param {Object} card Google Now from the server. | |
93 * @param {number=} opt_previousVersion The version of the shown card with | |
94 * this id, if it exists, undefined otherwise. | |
95 * @return {Object} Notification data entry for this card. | |
96 */ | |
97 function update(card, opt_previousVersion) { | |
98 console.log('cardManager.update ' + JSON.stringify(card) + ' ' + | |
99 opt_previousVersion); | |
100 | |
101 if (typeof card.version != 'number') { | |
102 console.log('cardCreateInfo.version is not a number'); | |
103 // Fix card version. | |
104 card.version = opt_previousVersion || 0; | |
105 } | |
106 | |
107 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
| |
108 | |
109 var timeHide = card.trigger && card.trigger.hideTimeSec !== undefined ? | |
110 Date.now() + card.trigger.hideTimeSec * MS_IN_SECOND : undefined; | |
111 var cardCreateInfo = { | |
112 notification: card.notification, | |
113 timeHide: timeHide, | |
114 version: card.version | |
115 }; | |
116 | |
117 var cardShowAlarmName = cardShowPrefix + card.notificationId; | |
118 if (card.trigger && card.trigger.showTimeSec) { | |
119 // Card needs to be shown later. | |
120 console.log('cardManager.register: postponed'); | |
121 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.
| |
122 var alarmInfo = { | |
123 when: Date.now() + card.trigger.showTimeSec * MS_IN_SECOND | |
124 }; | |
125 chrome.alarms.create(cardShowAlarmName, alarmInfo); | |
126 } else { | |
127 // Card needs to be shown immediately. | |
128 console.log('cardManager.register: immediate'); | |
129 chrome.alarms.clear(cardShowAlarmName); | |
130 showNotification( | |
131 card.notificationId, cardCreateInfo, opt_previousVersion); | |
132 } | |
133 | |
134 return {actionUrls: card.actionUrls, cardCreateInfo: cardCreateInfo}; | |
135 } | |
136 | |
137 /** | |
138 * Removes a card notification. | |
139 * @param {string} cardId Card ID. | |
140 */ | |
141 function clear(cardId) { | |
142 console.log('cardManager.unregister ' + cardId); | |
143 | |
144 chrome.notifications.clear(cardId, function() {}); | |
145 chrome.alarms.clear(cardShowPrefix + cardId); | |
146 chrome.alarms.clear(cardHidePrefix + cardId); | |
147 } | |
148 | |
149 chrome.alarms.onAlarm.addListener(function(alarm) { | |
150 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); | |
151 | |
152 if (alarm.name.indexOf(cardShowPrefix) == 0) { | |
153 // Alarm to show the card. | |
154 var cardId = alarm.name.substring(cardShowPrefixLength); | |
155 storage.get('notificationsData', function(items) { | |
156 items.notificationsData = items.notificationsData || {}; | |
157 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); | |
158 var notificationData = items.notificationsData[cardId]; | |
159 if (!notificationData) | |
160 return; | |
161 | |
162 showNotification(cardId, notificationData.cardCreateInfo); | |
163 }); | |
164 } else if (alarm.name.indexOf(cardHidePrefix) == 0) { | |
165 // Alarm to hide the card. | |
166 var cardId = alarm.name.substring(cardHidePrefixLength); | |
167 chrome.notifications.clear(cardId, function() {}); | |
168 } | |
169 }); | |
170 | |
171 return { | |
172 update: update, | |
173 clear: clear | |
174 }; | |
175 } | |
OLD | NEW |