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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 | 176 |
177 callback(uncombinedNotification, shouldShow && !shouldHide); | 177 callback(uncombinedNotification, shouldShow && !shouldHide); |
178 } | 178 } |
179 } | 179 } |
180 | 180 |
181 /** | 181 /** |
182 * Refreshes (shows/hides) the notification corresponding to the combined card | 182 * Refreshes (shows/hides) the notification corresponding to the combined card |
183 * based on the current time and show-hide intervals in the combined card. | 183 * based on the current time and show-hide intervals in the combined card. |
184 * @param {ChromeNotificationId} cardId Card ID. | 184 * @param {ChromeNotificationId} cardId Card ID. |
185 * @param {CombinedCard} combinedCard Combined cards with |cardId|. | 185 * @param {CombinedCard} combinedCard Combined cards with |cardId|. |
| 186 * @param {Object.<string, StoredNotificationGroup>} notificationGroups |
| 187 * Map from group name to group information. |
186 * @param {function(ReceivedNotification)=} onCardShown Optional parameter | 188 * @param {function(ReceivedNotification)=} onCardShown Optional parameter |
187 * called when each card is shown. | 189 * called when each card is shown. |
188 * @return {(NotificationDataEntry|undefined)} Notification data entry for | 190 * @return {(NotificationDataEntry|undefined)} Notification data entry for |
189 * this card. It's 'undefined' if the card's life is over. | 191 * this card. It's 'undefined' if the card's life is over. |
190 */ | 192 */ |
191 function update(cardId, combinedCard, onCardShown) { | 193 function update(cardId, combinedCard, notificationGroups, onCardShown) { |
192 console.log('cardManager.update ' + JSON.stringify(combinedCard)); | 194 console.log('cardManager.update ' + JSON.stringify(combinedCard)); |
193 | 195 |
194 chrome.alarms.clear(alarmPrefix + cardId); | 196 chrome.alarms.clear(alarmPrefix + cardId); |
195 var now = Date.now(); | 197 var now = Date.now(); |
196 /** @type {?UncombinedNotification} */ | 198 /** @type {?UncombinedNotification} */ |
197 var winningCard = null; | 199 var winningCard = null; |
198 // Next moment of time when winning notification selection algotithm can | 200 // Next moment of time when winning notification selection algotithm can |
199 // potentially return a different notification. | 201 // potentially return a different notification. |
200 /** @type {?number} */ | 202 /** @type {?number} */ |
201 var nextEventTime = null; | 203 var nextEventTime = null; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 | 249 |
248 return { | 250 return { |
249 actionUrls: winningActionUrls, | 251 actionUrls: winningActionUrls, |
250 timestamp: now, | 252 timestamp: now, |
251 combinedCard: combinedCard | 253 combinedCard: combinedCard |
252 }; | 254 }; |
253 } else { | 255 } else { |
254 // If there are no more events, we are done with this card. Note that all | 256 // If there are no more events, we are done with this card. Note that all |
255 // received notifications have hideTime. | 257 // received notifications have hideTime. |
256 verify(!winningCard, 'No events left, but card is shown.'); | 258 verify(!winningCard, 'No events left, but card is shown.'); |
257 clearCardFromGroups(cardId); | 259 clearCardFromGroups(cardId, notificationGroups); |
258 return undefined; | 260 return undefined; |
259 } | 261 } |
260 } | 262 } |
261 | 263 |
262 /** | 264 /** |
263 * Removes dismissed part of a card and refreshes the card. Returns remaining | 265 * Removes dismissed part of a card and refreshes the card. Returns remaining |
264 * dismissals for the combined card and updated notification data. | 266 * dismissals for the combined card and updated notification data. |
265 * @param {ChromeNotificationId} cardId Card ID. | 267 * @param {ChromeNotificationId} cardId Card ID. |
266 * @param {NotificationDataEntry} notificationData Stored notification entry | 268 * @param {NotificationDataEntry} notificationData Stored notification entry |
267 * for this card. | 269 * for this card. |
| 270 * @param {Object.<string, StoredNotificationGroup>} notificationGroups |
| 271 * Map from group name to group information. |
268 * @return {{ | 272 * @return {{ |
269 * dismissals: Array.<DismissalData>, | 273 * dismissals: Array.<DismissalData>, |
270 * notificationData: (NotificationDataEntry|undefined) | 274 * notificationData: (NotificationDataEntry|undefined) |
271 * }} | 275 * }} |
272 */ | 276 */ |
273 function onDismissal(cardId, notificationData) { | 277 function onDismissal(cardId, notificationData, notificationGroups) { |
274 var dismissals = []; | 278 var dismissals = []; |
275 var newCombinedCard = []; | 279 var newCombinedCard = []; |
276 | 280 |
277 // Determine which parts of the combined card need to be dismissed or to be | 281 // Determine which parts of the combined card need to be dismissed or to be |
278 // preserved. We dismiss parts that were visible at the moment when the card | 282 // preserved. We dismiss parts that were visible at the moment when the card |
279 // was last updated. | 283 // was last updated. |
280 iterateUncombinedNotifications( | 284 iterateUncombinedNotifications( |
281 notificationData.combinedCard, | 285 notificationData.combinedCard, |
282 notificationData.timestamp, | 286 notificationData.timestamp, |
283 function(uncombinedCard, visible) { | 287 function(uncombinedCard, visible) { |
284 if (visible) { | 288 if (visible) { |
285 dismissals.push({ | 289 dismissals.push({ |
286 notificationId: uncombinedCard.receivedNotification.notificationId, | 290 notificationId: uncombinedCard.receivedNotification.notificationId, |
287 parameters: uncombinedCard.receivedNotification.dismissal | 291 parameters: uncombinedCard.receivedNotification.dismissal |
288 }); | 292 }); |
289 } else { | 293 } else { |
290 newCombinedCard.push(uncombinedCard); | 294 newCombinedCard.push(uncombinedCard); |
291 } | 295 } |
292 }); | 296 }); |
293 | 297 |
294 return { | 298 return { |
295 dismissals: dismissals, | 299 dismissals: dismissals, |
296 notificationData: update(cardId, newCombinedCard) | 300 notificationData: update(cardId, newCombinedCard, notificationGroups) |
297 }; | 301 }; |
298 } | 302 } |
299 | 303 |
300 /** | 304 /** |
301 * Removes card information from 'notificationGroups'. | 305 * Removes card information from |notificationGroups|. |
302 * @param {ChromeNotificationId} cardId Card ID. | 306 * @param {ChromeNotificationId} cardId Card ID. |
| 307 * @param {Object.<string, StoredNotificationGroup>} notificationGroups |
| 308 * Map from group name to group information. |
303 */ | 309 */ |
304 function clearCardFromGroups(cardId) { | 310 function clearCardFromGroups(cardId, notificationGroups) { |
305 console.log('cardManager.clearCardFromGroups ' + cardId); | 311 console.log('cardManager.clearCardFromGroups ' + cardId); |
306 | 312 for (var groupName in notificationGroups) { |
307 instrumented.storage.local.get('notificationGroups', function(items) { | 313 var group = notificationGroups[groupName]; |
308 items = items || {}; | 314 for (var i = 0; i != group.cards.length; ++i) { |
309 /** @type {Object.<string, StoredNotificationGroup>} */ | 315 if (group.cards[i].chromeNotificationId == cardId) { |
310 items.notificationGroups = items.notificationGroups || {}; | 316 group.cards.splice(i, 1); |
311 | 317 break; |
312 for (var groupName in items.notificationGroups) { | |
313 var group = items.notificationGroups[groupName]; | |
314 for (var i = 0; i != group.cards.length; ++i) { | |
315 if (group.cards[i].chromeNotificationId == cardId) { | |
316 group.cards.splice(i, 1); | |
317 break; | |
318 } | |
319 } | 318 } |
320 } | 319 } |
321 | 320 } |
322 chrome.storage.local.set(items); | |
323 }); | |
324 } | 321 } |
325 | 322 |
326 instrumented.alarms.onAlarm.addListener(function(alarm) { | 323 instrumented.alarms.onAlarm.addListener(function(alarm) { |
327 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); | 324 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); |
328 | 325 |
329 if (alarm.name.indexOf(alarmPrefix) == 0) { | 326 if (alarm.name.indexOf(alarmPrefix) == 0) { |
330 // Alarm to show the card. | 327 // Alarm to show the card. |
331 tasks.add(UPDATE_CARD_TASK_NAME, function() { | 328 tasks.add(UPDATE_CARD_TASK_NAME, function() { |
332 var cardId = alarm.name.substring(alarmPrefix.length); | 329 var cardId = alarm.name.substring(alarmPrefix.length); |
333 instrumented.storage.local.get('notificationsData', function(items) { | 330 instrumented.storage.local.get( |
334 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); | 331 ['notificationsData', 'notificationGroups'], |
335 items = items || {}; | 332 function(items) { |
336 /** @type {Object.<string, NotificationDataEntry>} */ | 333 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); |
337 items.notificationsData = items.notificationsData || {}; | 334 items = items || {}; |
338 var combinedCard = | 335 /** @type {Object.<string, NotificationDataEntry>} */ |
339 (items.notificationsData[cardId] && | 336 items.notificationsData = items.notificationsData || {}; |
340 items.notificationsData[cardId].combinedCard) || []; | 337 /** @type {Object.<string, StoredNotificationGroup>} */ |
| 338 items.notificationGroups = items.notificationGroups || {}; |
341 | 339 |
342 var cardShownCallback = undefined; | 340 var combinedCard = |
343 if (localStorage['locationCardsShown'] < | 341 (items.notificationsData[cardId] && |
344 LOCATION_CARDS_LINK_THRESHOLD) { | 342 items.notificationsData[cardId].combinedCard) || []; |
345 cardShownCallback = countLocationCard; | |
346 } | |
347 | 343 |
348 items.notificationsData[cardId] = | 344 var cardShownCallback = undefined; |
349 update(cardId, combinedCard, cardShownCallback); | 345 if (localStorage['locationCardsShown'] < |
| 346 LOCATION_CARDS_LINK_THRESHOLD) { |
| 347 cardShownCallback = countLocationCard; |
| 348 } |
350 | 349 |
351 chrome.storage.local.set(items); | 350 items.notificationsData[cardId] = |
352 }); | 351 update( |
| 352 cardId, |
| 353 combinedCard, |
| 354 items.notificationGroups, |
| 355 cardShownCallback); |
| 356 |
| 357 chrome.storage.local.set(items); |
| 358 }); |
353 }); | 359 }); |
354 } | 360 } |
355 }); | 361 }); |
356 | 362 |
357 return { | 363 return { |
358 update: update, | 364 update: update, |
359 onDismissal: onDismissal | 365 onDismissal: onDismissal |
360 }; | 366 }; |
361 } | 367 } |
OLD | NEW |