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 96 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]; |
| 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 |