OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 * Show/hide trigger in a card. | 8 * Show/hide trigger in a card. |
9 * | 9 * |
10 * @typedef {{ | 10 * @typedef {{ |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 * @typedef {{ | 45 * @typedef {{ |
46 * notification: Object, | 46 * notification: Object, |
47 * hideTime: number=, | 47 * hideTime: number=, |
48 * version: number, | 48 * version: number, |
49 * previousVersion: number= | 49 * previousVersion: number= |
50 * }} | 50 * }} |
51 */ | 51 */ |
52 var CardCreateInfo; | 52 var CardCreateInfo; |
53 | 53 |
54 /** | 54 /** |
55 * Names for tasks that can be created by the this file. | |
56 */ | |
57 var CLEAR_CARD_TASK_NAME = 'clear-card'; | |
58 | |
59 /** | |
55 * Builds an object to manage notification card set. | 60 * Builds an object to manage notification card set. |
56 * @return {Object} Card set interface. | 61 * @return {Object} Card set interface. |
57 */ | 62 */ |
58 function buildCardSet() { | 63 function buildCardSet() { |
59 var cardShowPrefix = 'card-show-'; | 64 var cardShowPrefix = 'card-show-'; |
60 var cardHidePrefix = 'card-hide-'; | 65 var cardHidePrefix = 'card-hide-'; |
61 | 66 |
62 /** | 67 /** |
63 * Schedules hiding a notification. | 68 * Schedules hiding a notification. |
64 * @param {string} cardId Card ID. | 69 * @param {string} cardId Card ID. |
(...skipping 27 matching lines...) Expand all Loading... | |
92 cardCreateInfo.notification, | 97 cardCreateInfo.notification, |
93 function(newNotificationId) { | 98 function(newNotificationId) { |
94 if (!newNotificationId || chrome.runtime.lastError) { | 99 if (!newNotificationId || chrome.runtime.lastError) { |
95 var errorMessage = chrome.runtime.lastError && | 100 var errorMessage = chrome.runtime.lastError && |
96 chrome.runtime.lastError.message; | 101 chrome.runtime.lastError.message; |
97 console.error('notifications.create: ID=' + newNotificationId + | 102 console.error('notifications.create: ID=' + newNotificationId + |
98 ', ERROR=' + errorMessage); | 103 ', ERROR=' + errorMessage); |
99 return; | 104 return; |
100 } | 105 } |
101 | 106 |
102 scheduleHiding(cardId, cardCreateInfo.hideTime); | 107 scheduleHiding(cardId, cardCreateInfo.hideTime); |
103 }); | 108 }); |
104 } else { | 109 } else { |
105 // Update existing notification. | 110 // Update existing notification. |
106 instrumented.notifications.update( | 111 instrumented.notifications.update( |
107 cardId, | 112 cardId, |
108 cardCreateInfo.notification, | 113 cardCreateInfo.notification, |
109 function(wasUpdated) { | 114 function(wasUpdated) { |
110 if (!wasUpdated || chrome.runtime.lastError) { | 115 if (!wasUpdated || chrome.runtime.lastError) { |
111 var errorMessage = chrome.runtime.lastError && | 116 var errorMessage = chrome.runtime.lastError && |
112 chrome.runtime.lastError.message; | 117 chrome.runtime.lastError.message; |
113 console.error('notifications.update: UPDATED=' + wasUpdated + | 118 console.error('notifications.update: UPDATED=' + wasUpdated + |
114 ', ERROR=' + errorMessage); | 119 ', ERROR=' + errorMessage); |
115 return; | 120 return; |
116 } | 121 } |
117 | 122 |
118 scheduleHiding(cardId, cardCreateInfo.hideTime); | 123 scheduleHiding(cardId, cardCreateInfo.hideTime); |
119 }); | 124 }); |
120 } | 125 } |
121 } | 126 } |
122 | 127 |
123 /** | 128 /** |
124 * Updates/creates a card notification with new data. | 129 * Updates/creates a card notification with new data. |
125 * @param {string} cardId Card ID. | 130 * @param {string} cardId Card ID. |
126 * @param {MergedCard} card Google Now from the server. | 131 * @param {MergedCard} card Google Now from the server. |
127 * @param {number=} previousVersion The version of the shown card with | 132 * @param {number=} previousVersion The version of the shown card with |
128 * this id, if it exists, undefined otherwise. | 133 * this id, if it exists, undefined otherwise. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 return { | 166 return { |
162 actionUrls: card.actionUrls, | 167 actionUrls: card.actionUrls, |
163 cardCreateInfo: cardCreateInfo, | 168 cardCreateInfo: cardCreateInfo, |
164 dismissals: card.dismissals | 169 dismissals: card.dismissals |
165 }; | 170 }; |
166 } | 171 } |
167 | 172 |
168 /** | 173 /** |
169 * Removes a card notification. | 174 * Removes a card notification. |
170 * @param {string} cardId Card ID. | 175 * @param {string} cardId Card ID. |
176 * @param {boolean} clearStorage True if the information associated with the | |
177 * card should be erased from chrome.storage. | |
171 */ | 178 */ |
172 function clear(cardId) { | 179 function clear(cardId, clearStorage) { |
173 console.log('cardManager.clear ' + cardId); | 180 console.log('cardManager.clear ' + cardId); |
174 | 181 |
175 chrome.notifications.clear(cardId, function() {}); | 182 chrome.notifications.clear(cardId, function() {}); |
176 chrome.alarms.clear(cardShowPrefix + cardId); | 183 chrome.alarms.clear(cardShowPrefix + cardId); |
177 chrome.alarms.clear(cardHidePrefix + cardId); | 184 chrome.alarms.clear(cardHidePrefix + cardId); |
185 | |
186 if (clearStorage) { | |
187 instrumented.storage.local.get( | |
188 ['notificationsData', 'notificationGroups'], | |
189 function(items) { | |
190 items = items || {}; | |
191 items.notificationsData = items.notificationsData || {}; | |
192 items.notificationGroups = items.notificationGroups || {}; | |
193 | |
194 delete items.notificationsData[cardId]; | |
rgustafson
2013/09/27 18:57:20
Do you have to use delete vs setting to null in th
vadimt
2013/09/27 20:10:56
We'll never attempt to access this data after clea
rgustafson
2013/09/27 20:19:28
Then style says to set to null instead of delete.
vadimt
2013/09/27 20:25:24
"The delete keyword should be avoided except when
rgustafson
2013/09/27 22:53:01
It has a list of keys. It isn't necessary to remov
robliao
2013/09/27 22:59:13
The concern here is that deleting is slower than s
vadimt
2013/09/30 16:56:17
FWIW: I'm not null-ing it.
This is a map. No one d
rgustafson
2013/09/30 18:44:02
You still send reviews to arv, right? If the style
| |
195 | |
196 for (var groupName in items.notificationGroups) { | |
197 var group = items.notificationGroups[groupName]; | |
198 for (var i = 0; i != group.cards.length; ++i) { | |
199 if (group.cards[i].chromeNotificationId == cardId) { | |
200 group.cards.splice(i, 1); | |
201 break; | |
202 } | |
203 } | |
204 } | |
205 | |
206 chrome.storage.local.set(items); | |
207 }); | |
208 } | |
178 } | 209 } |
179 | 210 |
180 instrumented.alarms.onAlarm.addListener(function(alarm) { | 211 instrumented.alarms.onAlarm.addListener(function(alarm) { |
181 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); | 212 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); |
182 | 213 |
183 if (alarm.name.indexOf(cardShowPrefix) == 0) { | 214 if (alarm.name.indexOf(cardShowPrefix) == 0) { |
184 // Alarm to show the card. | 215 // Alarm to show the card. |
185 var cardId = alarm.name.substring(cardShowPrefix.length); | 216 var cardId = alarm.name.substring(cardShowPrefix.length); |
186 instrumented.storage.local.get('notificationsData', function(items) { | 217 instrumented.storage.local.get('notificationsData', function(items) { |
187 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); | 218 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); |
188 if (!items || !items.notificationsData) | 219 if (!items || !items.notificationsData) |
189 return; | 220 return; |
190 var notificationData = items.notificationsData[cardId]; | 221 var notificationData = items.notificationsData[cardId]; |
191 if (!notificationData) | 222 if (!notificationData) |
192 return; | 223 return; |
193 | 224 |
194 showNotification(cardId, notificationData.cardCreateInfo); | 225 showNotification(cardId, notificationData.cardCreateInfo); |
195 }); | 226 }); |
196 } else if (alarm.name.indexOf(cardHidePrefix) == 0) { | 227 } else if (alarm.name.indexOf(cardHidePrefix) == 0) { |
197 // Alarm to hide the card. | 228 // Alarm to hide the card. |
198 var cardId = alarm.name.substring(cardHidePrefix.length); | 229 tasks.add(CLEAR_CARD_TASK_NAME, function() { |
199 clear(cardId); | 230 var cardId = alarm.name.substring(cardHidePrefix.length); |
231 clear(cardId, true); | |
232 }); | |
200 } | 233 } |
201 }); | 234 }); |
202 | 235 |
203 return { | 236 return { |
204 update: update, | 237 update: update, |
205 clear: clear | 238 clear: clear |
206 }; | 239 }; |
207 } | 240 } |
OLD | NEW |