| 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 var MS_IN_SECOND = 1000; | 7 var MS_IN_SECOND = 1000; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Builds an object to manage notification card set. | 10 * Builds an object to manage notification card set. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 * Shows a notification. | 33 * Shows a notification. |
| 34 * @param {string} cardId Card ID. | 34 * @param {string} cardId Card ID. |
| 35 * @param {Object} cardCreateInfo Google Now card represented as a set of | 35 * @param {Object} cardCreateInfo Google Now card represented as a set of |
| 36 * parameters for showing a Chrome notification. | 36 * parameters for showing a Chrome notification. |
| 37 */ | 37 */ |
| 38 function showNotification(cardId, cardCreateInfo) { | 38 function showNotification(cardId, cardCreateInfo) { |
| 39 console.log('cardManager.showNotification ' + cardId + ' ' + | 39 console.log('cardManager.showNotification ' + cardId + ' ' + |
| 40 JSON.stringify(cardCreateInfo)); | 40 JSON.stringify(cardCreateInfo)); |
| 41 | 41 |
| 42 if (cardCreateInfo.previousVersion !== cardCreateInfo.version) { | 42 if (cardCreateInfo.previousVersion !== cardCreateInfo.version) { |
| 43 try { | 43 // Delete a notification with the specified id if it already exists, and |
| 44 // Delete a notification with the specified id if it already exists, and | 44 // then create a notification. |
| 45 // then create a notification. | 45 instrumented.notifications.create( |
| 46 instrumented.notifications.create( | 46 cardId, |
| 47 cardId, | 47 cardCreateInfo.notification, |
| 48 cardCreateInfo.notification, | 48 function(newNotificationId) { |
| 49 function(newNotificationId) { | 49 if (!newNotificationId || chrome.runtime.lastError) { |
| 50 if (!newNotificationId || chrome.runtime.lastError) { | 50 var errorMessage = chrome.runtime.lastError && |
| 51 var errorMessage = chrome.runtime.lastError && | 51 chrome.runtime.lastError.message; |
| 52 chrome.runtime.lastError.message; | 52 console.error('notifications.create: ID=' + newNotificationId + |
| 53 console.error('notifications.create: ID=' + newNotificationId + | 53 ', ERROR=' + errorMessage); |
| 54 ', ERROR=' + errorMessage); | 54 return; |
| 55 return; | 55 } |
| 56 } | |
| 57 | 56 |
| 58 scheduleHiding(cardId, cardCreateInfo.timeHide); | 57 scheduleHiding(cardId, cardCreateInfo.timeHide); |
| 59 }); | 58 }); |
| 60 } catch (error) { | |
| 61 console.error('Error in notifications.create: ' + error); | |
| 62 } | |
| 63 } else { | 59 } else { |
| 64 try { | 60 // Update existing notification. |
| 65 // Update existing notification. | 61 instrumented.notifications.update( |
| 66 instrumented.notifications.update( | 62 cardId, |
| 67 cardId, | 63 cardCreateInfo.notification, |
| 68 cardCreateInfo.notification, | 64 function(wasUpdated) { |
| 69 function(wasUpdated) { | 65 if (!wasUpdated || chrome.runtime.lastError) { |
| 70 if (!wasUpdated || chrome.runtime.lastError) { | 66 var errorMessage = chrome.runtime.lastError && |
| 71 var errorMessage = chrome.runtime.lastError && | 67 chrome.runtime.lastError.message; |
| 72 chrome.runtime.lastError.message; | 68 console.error('notifications.update: UPDATED=' + wasUpdated + |
| 73 console.error('notifications.update: UPDATED=' + wasUpdated + | 69 ', ERROR=' + errorMessage); |
| 74 ', ERROR=' + errorMessage); | 70 return; |
| 75 return; | 71 } |
| 76 } | |
| 77 | 72 |
| 78 scheduleHiding(cardId, cardCreateInfo.timeHide); | 73 scheduleHiding(cardId, cardCreateInfo.timeHide); |
| 79 }); | 74 }); |
| 80 } catch (error) { | |
| 81 console.error('Error in notifications.update: ' + error); | |
| 82 } | |
| 83 } | 75 } |
| 84 } | 76 } |
| 85 | 77 |
| 86 /** | 78 /** |
| 87 * Updates/creates a card notification with new data. | 79 * Updates/creates a card notification with new data. |
| 88 * @param {Object} card Google Now from the server. | 80 * @param {Object} card Google Now from the server. |
| 89 * @param {number=} previousVersion The version of the shown card with | 81 * @param {number=} previousVersion The version of the shown card with |
| 90 * this id, if it exists, undefined otherwise. | 82 * this id, if it exists, undefined otherwise. |
| 91 * @return {Object} Notification data entry for this card. | 83 * @return {Object} Notification data entry for this card. |
| 92 */ | 84 */ |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 chrome.alarms.clear(cardHidePrefix + cardId); | 139 chrome.alarms.clear(cardHidePrefix + cardId); |
| 148 } | 140 } |
| 149 | 141 |
| 150 instrumented.alarms.onAlarm.addListener(function(alarm) { | 142 instrumented.alarms.onAlarm.addListener(function(alarm) { |
| 151 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); | 143 console.log('cardManager.onAlarm ' + JSON.stringify(alarm)); |
| 152 | 144 |
| 153 if (alarm.name.indexOf(cardShowPrefix) == 0) { | 145 if (alarm.name.indexOf(cardShowPrefix) == 0) { |
| 154 // Alarm to show the card. | 146 // Alarm to show the card. |
| 155 var cardId = alarm.name.substring(cardShowPrefix.length); | 147 var cardId = alarm.name.substring(cardShowPrefix.length); |
| 156 instrumented.storage.local.get('notificationsData', function(items) { | 148 instrumented.storage.local.get('notificationsData', function(items) { |
| 157 items.notificationsData = items.notificationsData || {}; | |
| 158 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); | 149 console.log('cardManager.onAlarm.get ' + JSON.stringify(items)); |
| 150 if (!items || !items.notificationsData) |
| 151 return; |
| 159 var notificationData = items.notificationsData[cardId]; | 152 var notificationData = items.notificationsData[cardId]; |
| 160 if (!notificationData) | 153 if (!notificationData) |
| 161 return; | 154 return; |
| 162 | 155 |
| 163 showNotification(cardId, notificationData.cardCreateInfo); | 156 showNotification(cardId, notificationData.cardCreateInfo); |
| 164 }); | 157 }); |
| 165 } else if (alarm.name.indexOf(cardHidePrefix) == 0) { | 158 } else if (alarm.name.indexOf(cardHidePrefix) == 0) { |
| 166 // Alarm to hide the card. | 159 // Alarm to hide the card. |
| 167 var cardId = alarm.name.substring(cardHidePrefix.length); | 160 var cardId = alarm.name.substring(cardHidePrefix.length); |
| 168 chrome.notifications.clear(cardId, function() {}); | 161 chrome.notifications.clear(cardId, function() {}); |
| 169 } | 162 } |
| 170 }); | 163 }); |
| 171 | 164 |
| 172 return { | 165 return { |
| 173 update: update, | 166 update: update, |
| 174 clear: clear | 167 clear: clear |
| 175 }; | 168 }; |
| 176 } | 169 } |
| OLD | NEW |