Chromium Code Reviews| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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. | 55 * Names for tasks that can be created by the this file. |
| 56 */ | 56 */ |
| 57 var SHOW_CARD_TASK_NAME = 'show-card'; | |
| 57 var CLEAR_CARD_TASK_NAME = 'clear-card'; | 58 var CLEAR_CARD_TASK_NAME = 'clear-card'; |
| 58 | 59 |
| 59 /** | 60 /** |
| 60 * Builds an object to manage notification card set. | 61 * Builds an object to manage notification card set. |
| 61 * @return {Object} Card set interface. | 62 * @return {Object} Card set interface. |
| 62 */ | 63 */ |
| 63 function buildCardSet() { | 64 function buildCardSet() { |
| 64 var cardShowPrefix = 'card-show-'; | 65 var cardShowPrefix = 'card-show-'; |
| 65 var cardHidePrefix = 'card-hide-'; | 66 var cardHidePrefix = 'card-hide-'; |
| 66 | 67 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 82 /** | 83 /** |
| 83 * Shows a notification. | 84 * Shows a notification. |
| 84 * @param {string} cardId Card ID. | 85 * @param {string} cardId Card ID. |
| 85 * @param {CardCreateInfo} cardCreateInfo Google Now card represented as a set | 86 * @param {CardCreateInfo} cardCreateInfo Google Now card represented as a set |
| 86 * of parameters for showing a Chrome notification. | 87 * of parameters for showing a Chrome notification. |
| 87 */ | 88 */ |
| 88 function showNotification(cardId, cardCreateInfo) { | 89 function showNotification(cardId, cardCreateInfo) { |
| 89 console.log('cardManager.showNotification ' + cardId + ' ' + | 90 console.log('cardManager.showNotification ' + cardId + ' ' + |
| 90 JSON.stringify(cardCreateInfo)); | 91 JSON.stringify(cardCreateInfo)); |
| 91 | 92 |
| 93 if (cardCreateInfo.hideTime <= Date.now()) { | |
| 94 console.log('cardManager.showNotification ' + cardId + ': expired'); | |
| 95 // Card has expired. Schedule hiding to delete asociated information. | |
| 96 scheduleHiding(cardId, cardCreateInfo.hideTime); | |
| 97 return; | |
| 98 } | |
| 99 | |
| 92 if (cardCreateInfo.previousVersion !== cardCreateInfo.version) { | 100 if (cardCreateInfo.previousVersion !== cardCreateInfo.version) { |
| 93 // Delete a notification with the specified id if it already exists, and | 101 // Delete a notification with the specified id if it already exists, and |
| 94 // then create a notification. | 102 // then create a notification. |
| 95 instrumented.notifications.create( | 103 instrumented.notifications.create( |
| 96 cardId, | 104 cardId, |
| 97 cardCreateInfo.notification, | 105 cardCreateInfo.notification, |
| 98 function(newNotificationId) { | 106 function(newNotificationId) { |
| 99 if (!newNotificationId || chrome.runtime.lastError) { | 107 if (!newNotificationId || chrome.runtime.lastError) { |
| 100 var errorMessage = chrome.runtime.lastError && | 108 var errorMessage = chrome.runtime.lastError && |
| 101 chrome.runtime.lastError.message; | 109 chrome.runtime.lastError.message; |
| 102 console.error('notifications.create: ID=' + newNotificationId + | 110 console.error('notifications.create: ID=' + newNotificationId + |
| 103 ', ERROR=' + errorMessage); | 111 ', ERROR=' + errorMessage); |
| 104 return; | 112 return; |
| 105 } | 113 } |
| 106 | 114 |
| 107 scheduleHiding(cardId, cardCreateInfo.hideTime); | 115 scheduleHiding(cardId, cardCreateInfo.hideTime); |
| 108 }); | 116 }); |
| 109 } else { | 117 } else { |
| 110 // Update existing notification. | 118 // Update existing notification. |
| 111 instrumented.notifications.update( | 119 instrumented.notifications.update( |
| 112 cardId, | 120 cardId, |
| 113 cardCreateInfo.notification, | 121 cardCreateInfo.notification, |
| 114 function(wasUpdated) { | 122 function(wasUpdated) { |
| 115 if (!wasUpdated || chrome.runtime.lastError) { | 123 if (!wasUpdated || chrome.runtime.lastError) { |
| 116 var errorMessage = chrome.runtime.lastError && | 124 var errorMessage = chrome.runtime.lastError && |
| 117 chrome.runtime.lastError.message; | 125 chrome.runtime.lastError.message; |
| 118 console.error('notifications.update: UPDATED=' + wasUpdated + | 126 console.error('notifications.update: UPDATED=' + wasUpdated + |
| 119 ', ERROR=' + errorMessage); | 127 ', ERROR=' + errorMessage); |
| 120 return; | 128 return; |
| 121 } | 129 } |
| 122 | 130 |
| 123 scheduleHiding(cardId, cardCreateInfo.hideTime); | 131 scheduleHiding(cardId, cardCreateInfo.hideTime); |
| 124 }); | 132 }); |
| 125 } | 133 } |
| 126 } | 134 } |
| 127 | 135 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 chrome.storage.local.set(items); | 212 chrome.storage.local.set(items); |
| 205 }); | 213 }); |
| 206 } | 214 } |
| 207 } | 215 } |
| 208 | 216 |
| 209 instrumented.alarms.onAlarm.addListener(function(alarm) { | 217 instrumented.alarms.onAlarm.addListener(function(alarm) { |
| 210 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); | 218 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); |
| 211 | 219 |
| 212 if (alarm.name.indexOf(cardShowPrefix) == 0) { | 220 if (alarm.name.indexOf(cardShowPrefix) == 0) { |
| 213 // Alarm to show the card. | 221 // Alarm to show the card. |
| 214 var cardId = alarm.name.substring(cardShowPrefix.length); | 222 tasks.add(SHOW_CARD_TASK_NAME, function() { |
|
robliao
2013/10/14 21:05:31
Why is this going to a task?
vadimt
2013/10/14 23:51:57
It manipulates with storage and can create notific
| |
| 215 instrumented.storage.local.get('notificationsData', function(items) { | 223 var cardId = alarm.name.substring(cardShowPrefix.length); |
| 216 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); | 224 instrumented.storage.local.get('notificationsData', function(items) { |
| 217 if (!items || !items.notificationsData) | 225 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); |
| 218 return; | 226 if (!items || !items.notificationsData) |
| 219 var notificationData = items.notificationsData[cardId]; | 227 return; |
| 220 if (!notificationData) | 228 var notificationData = items.notificationsData[cardId]; |
| 221 return; | 229 if (!notificationData) |
| 230 return; | |
| 222 | 231 |
| 223 showNotification(cardId, notificationData.cardCreateInfo); | 232 showNotification(cardId, notificationData.cardCreateInfo); |
| 233 }); | |
| 224 }); | 234 }); |
| 225 } else if (alarm.name.indexOf(cardHidePrefix) == 0) { | 235 } else if (alarm.name.indexOf(cardHidePrefix) == 0) { |
| 226 // Alarm to hide the card. | 236 // Alarm to hide the card. |
| 227 tasks.add(CLEAR_CARD_TASK_NAME, function() { | 237 tasks.add(CLEAR_CARD_TASK_NAME, function() { |
| 228 var cardId = alarm.name.substring(cardHidePrefix.length); | 238 var cardId = alarm.name.substring(cardHidePrefix.length); |
| 229 clear(cardId, true); | 239 clear(cardId, true); |
| 230 }); | 240 }); |
| 231 } | 241 } |
| 232 }); | 242 }); |
| 233 | 243 |
| 234 return { | 244 return { |
| 235 update: update, | 245 update: update, |
| 236 clear: clear | 246 clear: clear |
| 237 }; | 247 }; |
| 238 } | 248 } |
| OLD | NEW |