| 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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 'cardManager.updateNotification ' + chromeNotificationId + ' ' + | 129 'cardManager.updateNotification ' + chromeNotificationId + ' ' + |
| 130 JSON.stringify(receivedNotification)); | 130 JSON.stringify(receivedNotification)); |
| 131 | 131 |
| 132 if (!receivedNotification) { | 132 if (!receivedNotification) { |
| 133 instrumented.notifications.clear(chromeNotificationId, function() {}); | 133 instrumented.notifications.clear(chromeNotificationId, function() {}); |
| 134 return; | 134 return; |
| 135 } | 135 } |
| 136 | 136 |
| 137 // Try updating the notification. | 137 // Try updating the notification. |
| 138 instrumented.notifications.update( | 138 instrumented.notifications.update( |
| 139 chromeNotificationId, | 139 chromeNotificationId, receivedNotification.chromeNotificationOptions, |
| 140 receivedNotification.chromeNotificationOptions, | |
| 141 function(wasUpdated) { | 140 function(wasUpdated) { |
| 142 if (!wasUpdated) { | 141 if (!wasUpdated) { |
| 143 // If the notification wasn't updated, it probably didn't exist. | 142 // If the notification wasn't updated, it probably didn't exist. |
| 144 // Create it. | 143 // Create it. |
| 145 console.log( | 144 console.log( |
| 146 'cardManager.updateNotification ' + chromeNotificationId + | 145 'cardManager.updateNotification ' + chromeNotificationId + |
| 147 ' not updated, creating instead'); | 146 ' not updated, creating instead'); |
| 148 instrumented.notifications.create( | 147 instrumented.notifications.create( |
| 149 chromeNotificationId, | 148 chromeNotificationId, |
| 150 receivedNotification.chromeNotificationOptions, | 149 receivedNotification.chromeNotificationOptions, |
| 151 function(newChromeNotificationId) { | 150 function(newChromeNotificationId) { |
| 152 if (!newChromeNotificationId || chrome.runtime.lastError) { | 151 if (!newChromeNotificationId || chrome.runtime.lastError) { |
| 153 var errorMessage = chrome.runtime.lastError && | 152 var errorMessage = chrome.runtime.lastError && |
| 154 chrome.runtime.lastError.message; | 153 chrome.runtime.lastError.message; |
| 155 console.error('notifications.create: ID=' + | 154 console.error( |
| 156 newChromeNotificationId + ', ERROR=' + errorMessage); | 155 'notifications.create: ID=' + newChromeNotificationId + |
| 156 ', ERROR=' + errorMessage); |
| 157 return; | 157 return; |
| 158 } | 158 } |
| 159 | 159 |
| 160 if (onCardShown !== undefined) | 160 if (onCardShown !== undefined) |
| 161 onCardShown(receivedNotification); | 161 onCardShown(receivedNotification); |
| 162 }); | 162 }); |
| 163 } | 163 } |
| 164 }); | 164 }); |
| 165 } | 165 } |
| 166 | 166 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 /** @type {(UncombinedNotification|undefined)} */ | 208 /** @type {(UncombinedNotification|undefined)} */ |
| 209 var winningCard = undefined; | 209 var winningCard = undefined; |
| 210 // Next moment of time when winning notification selection algotithm can | 210 // Next moment of time when winning notification selection algotithm can |
| 211 // potentially return a different notification. | 211 // potentially return a different notification. |
| 212 /** @type {?number} */ | 212 /** @type {?number} */ |
| 213 var nextEventTime = null; | 213 var nextEventTime = null; |
| 214 | 214 |
| 215 // Find a winning uncombined notification: a highest-priority notification | 215 // Find a winning uncombined notification: a highest-priority notification |
| 216 // that needs to be shown now. | 216 // that needs to be shown now. |
| 217 iterateUncombinedNotifications( | 217 iterateUncombinedNotifications( |
| 218 combinedCard, | 218 combinedCard, now, function(uncombinedCard, visible) { |
| 219 now, | |
| 220 function(uncombinedCard, visible) { | |
| 221 // If the uncombined notification is visible now and set the winning | 219 // If the uncombined notification is visible now and set the winning |
| 222 // card to it if its priority is higher. | 220 // card to it if its priority is higher. |
| 223 if (visible) { | 221 if (visible) { |
| 224 if (!winningCard || | 222 if (!winningCard || |
| 225 uncombinedCard.receivedNotification.chromeNotificationOptions. | 223 uncombinedCard.receivedNotification.chromeNotificationOptions |
| 226 priority > | 224 .priority > winningCard.receivedNotification |
| 227 winningCard.receivedNotification.chromeNotificationOptions. | 225 .chromeNotificationOptions.priority) { |
| 228 priority) { | |
| 229 winningCard = uncombinedCard; | 226 winningCard = uncombinedCard; |
| 230 } | 227 } |
| 231 } | 228 } |
| 232 | 229 |
| 233 // Next event time is the closest hide or show event. | 230 // Next event time is the closest hide or show event. |
| 234 if (uncombinedCard.showTime && uncombinedCard.showTime > now) { | 231 if (uncombinedCard.showTime && uncombinedCard.showTime > now) { |
| 235 if (!nextEventTime || nextEventTime > uncombinedCard.showTime) | 232 if (!nextEventTime || nextEventTime > uncombinedCard.showTime) |
| 236 nextEventTime = uncombinedCard.showTime; | 233 nextEventTime = uncombinedCard.showTime; |
| 237 } | 234 } |
| 238 if (uncombinedCard.hideTime > now) { | 235 if (uncombinedCard.hideTime > now) { |
| 239 if (!nextEventTime || nextEventTime > uncombinedCard.hideTime) | 236 if (!nextEventTime || nextEventTime > uncombinedCard.hideTime) |
| 240 nextEventTime = uncombinedCard.hideTime; | 237 nextEventTime = uncombinedCard.hideTime; |
| 241 } | 238 } |
| 242 }); | 239 }); |
| 243 | 240 |
| 244 // Show/hide the winning card. | 241 // Show/hide the winning card. |
| 245 updateNotification( | 242 updateNotification( |
| 246 chromeNotificationId, | 243 chromeNotificationId, winningCard && winningCard.receivedNotification, |
| 247 winningCard && winningCard.receivedNotification, | |
| 248 onCardShown); | 244 onCardShown); |
| 249 | 245 |
| 250 if (nextEventTime) { | 246 if (nextEventTime) { |
| 251 // If we expect more events, create an alarm for the next one. | 247 // If we expect more events, create an alarm for the next one. |
| 252 chrome.alarms.create( | 248 chrome.alarms.create( |
| 253 alarmPrefix + chromeNotificationId, {when: nextEventTime}); | 249 alarmPrefix + chromeNotificationId, {when: nextEventTime}); |
| 254 | 250 |
| 255 // The trick with stringify/parse is to create a copy of action URLs, | 251 // The trick with stringify/parse is to create a copy of action URLs, |
| 256 // otherwise notifications data with 2 pointers to the same object won't | 252 // otherwise notifications data with 2 pointers to the same object won't |
| 257 // be stored correctly to chrome.storage. | 253 // be stored correctly to chrome.storage. |
| 258 var winningActionUrls = winningCard && | 254 var winningActionUrls = winningCard && |
| 259 winningCard.receivedNotification.actionUrls && | 255 winningCard.receivedNotification.actionUrls && |
| 260 JSON.parse(JSON.stringify( | 256 JSON.parse( |
| 261 winningCard.receivedNotification.actionUrls)); | 257 JSON.stringify(winningCard.receivedNotification.actionUrls)); |
| 262 var winningCardTypeId = winningCard && | 258 var winningCardTypeId = |
| 263 winningCard.receivedNotification.cardTypeId; | 259 winningCard && winningCard.receivedNotification.cardTypeId; |
| 264 return { | 260 return { |
| 265 actionUrls: winningActionUrls, | 261 actionUrls: winningActionUrls, |
| 266 cardTypeId: winningCardTypeId, | 262 cardTypeId: winningCardTypeId, |
| 267 timestamp: now, | 263 timestamp: now, |
| 268 combinedCard: combinedCard | 264 combinedCard: combinedCard |
| 269 }; | 265 }; |
| 270 } else { | 266 } else { |
| 271 // If there are no more events, we are done with this card. Note that all | 267 // If there are no more events, we are done with this card. Note that all |
| 272 // received notifications have hideTime. | 268 // received notifications have hideTime. |
| 273 verify(!winningCard, 'No events left, but card is shown.'); | 269 verify(!winningCard, 'No events left, but card is shown.'); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 294 chromeNotificationId, notificationData, notificationGroups) { | 290 chromeNotificationId, notificationData, notificationGroups) { |
| 295 /** @type {Array<DismissalData>} */ | 291 /** @type {Array<DismissalData>} */ |
| 296 var dismissals = []; | 292 var dismissals = []; |
| 297 /** @type {Array<UncombinedNotification>} */ | 293 /** @type {Array<UncombinedNotification>} */ |
| 298 var newCombinedCard = []; | 294 var newCombinedCard = []; |
| 299 | 295 |
| 300 // Determine which parts of the combined card need to be dismissed or to be | 296 // Determine which parts of the combined card need to be dismissed or to be |
| 301 // preserved. We dismiss parts that were visible at the moment when the card | 297 // preserved. We dismiss parts that were visible at the moment when the card |
| 302 // was last updated. | 298 // was last updated. |
| 303 iterateUncombinedNotifications( | 299 iterateUncombinedNotifications( |
| 304 notificationData.combinedCard, | 300 notificationData.combinedCard, notificationData.timestamp, |
| 305 notificationData.timestamp, | 301 function(uncombinedCard, visible) { |
| 306 function(uncombinedCard, visible) { | 302 if (visible) { |
| 307 if (visible) { | 303 dismissals.push({ |
| 308 dismissals.push({ | 304 notificationId: |
| 309 notificationId: uncombinedCard.receivedNotification.notificationId, | 305 uncombinedCard.receivedNotification.notificationId, |
| 310 parameters: uncombinedCard.receivedNotification.dismissal | 306 parameters: uncombinedCard.receivedNotification.dismissal |
| 311 }); | 307 }); |
| 312 } else { | 308 } else { |
| 313 newCombinedCard.push(uncombinedCard); | 309 newCombinedCard.push(uncombinedCard); |
| 314 } | 310 } |
| 315 }); | 311 }); |
| 316 | 312 |
| 317 return { | 313 return { |
| 318 dismissals: dismissals, | 314 dismissals: dismissals, |
| 319 notificationData: update( | 315 notificationData: |
| 320 chromeNotificationId, newCombinedCard, notificationGroups) | 316 update(chromeNotificationId, newCombinedCard, notificationGroups) |
| 321 }; | 317 }; |
| 322 } | 318 } |
| 323 | 319 |
| 324 /** | 320 /** |
| 325 * Removes card information from |notificationGroups|. | 321 * Removes card information from |notificationGroups|. |
| 326 * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID | 322 * @param {ChromeNotificationId} chromeNotificationId chrome.notifications ID |
| 327 * of the card. | 323 * of the card. |
| 328 * @param {Object<StoredNotificationGroup>} notificationGroups Map from group | 324 * @param {Object<StoredNotificationGroup>} notificationGroups Map from group |
| 329 * name to group information. | 325 * name to group information. |
| 330 */ | 326 */ |
| (...skipping 20 matching lines...) Expand all Loading... |
| 351 var chromeNotificationId = alarm.name.substring(alarmPrefix.length); | 347 var chromeNotificationId = alarm.name.substring(alarmPrefix.length); |
| 352 fillFromChromeLocalStorage({ | 348 fillFromChromeLocalStorage({ |
| 353 /** @type {Object<ChromeNotificationId, NotificationDataEntry>} */ | 349 /** @type {Object<ChromeNotificationId, NotificationDataEntry>} */ |
| 354 notificationsData: {}, | 350 notificationsData: {}, |
| 355 /** @type {Object<StoredNotificationGroup>} */ | 351 /** @type {Object<StoredNotificationGroup>} */ |
| 356 notificationGroups: {} | 352 notificationGroups: {} |
| 357 }).then(function(items) { | 353 }).then(function(items) { |
| 358 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); | 354 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); |
| 359 | 355 |
| 360 var combinedCard = | 356 var combinedCard = |
| 361 (items.notificationsData[chromeNotificationId] && | 357 (items.notificationsData[chromeNotificationId] && |
| 362 items.notificationsData[chromeNotificationId].combinedCard) || []; | 358 items.notificationsData[chromeNotificationId].combinedCard) || |
| 359 []; |
| 363 | 360 |
| 364 var cardShownCallback = undefined; | 361 var cardShownCallback = undefined; |
| 365 if (localStorage['explanatoryCardsShown'] < | 362 if (localStorage['explanatoryCardsShown'] < |
| 366 EXPLANATORY_CARDS_LINK_THRESHOLD) { | 363 EXPLANATORY_CARDS_LINK_THRESHOLD) { |
| 367 cardShownCallback = countExplanatoryCard; | 364 cardShownCallback = countExplanatoryCard; |
| 368 } | 365 } |
| 369 | 366 |
| 370 items.notificationsData[chromeNotificationId] = | 367 items.notificationsData[chromeNotificationId] = update( |
| 371 update( | 368 chromeNotificationId, combinedCard, items.notificationGroups, |
| 372 chromeNotificationId, | 369 cardShownCallback); |
| 373 combinedCard, | |
| 374 items.notificationGroups, | |
| 375 cardShownCallback); | |
| 376 | 370 |
| 377 chrome.storage.local.set(items); | 371 chrome.storage.local.set(items); |
| 378 }); | 372 }); |
| 379 }); | 373 }); |
| 380 } | 374 } |
| 381 }); | 375 }); |
| 382 | 376 |
| 383 return { | 377 return {update: update, onDismissal: onDismissal}; |
| 384 update: update, | |
| 385 onDismissal: onDismissal | |
| 386 }; | |
| 387 } | 378 } |
| OLD | NEW |