OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 * @fileoverview The event page for Google Now for Chrome implementation. | 8 * @fileoverview The event page for Google Now for Chrome implementation. |
9 * The Google Now event page gets Google Now cards from the server and shows | 9 * The Google Now event page gets Google Now cards from the server and shows |
10 * them as Chrome notifications. | 10 * them as Chrome notifications. |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 var updateCardsAttempts = buildAttemptManager( | 140 var updateCardsAttempts = buildAttemptManager( |
141 'cards-update', | 141 'cards-update', |
142 requestLocation, | 142 requestLocation, |
143 INITIAL_POLLING_PERIOD_SECONDS, | 143 INITIAL_POLLING_PERIOD_SECONDS, |
144 MAXIMUM_POLLING_PERIOD_SECONDS); | 144 MAXIMUM_POLLING_PERIOD_SECONDS); |
145 var dismissalAttempts = buildAttemptManager( | 145 var dismissalAttempts = buildAttemptManager( |
146 'dismiss', | 146 'dismiss', |
147 retryPendingDismissals, | 147 retryPendingDismissals, |
148 INITIAL_RETRY_DISMISS_PERIOD_SECONDS, | 148 INITIAL_RETRY_DISMISS_PERIOD_SECONDS, |
149 MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS); | 149 MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS); |
| 150 var cardSet = buildCardSet(); |
150 | 151 |
151 /** | 152 /** |
152 * Diagnostic event identifier. | 153 * Diagnostic event identifier. |
153 * @enum {number} | 154 * @enum {number} |
154 */ | 155 */ |
155 var DiagnosticEvent = { | 156 var DiagnosticEvent = { |
156 REQUEST_FOR_CARDS_TOTAL: 0, | 157 REQUEST_FOR_CARDS_TOTAL: 0, |
157 REQUEST_FOR_CARDS_SUCCESS: 1, | 158 REQUEST_FOR_CARDS_SUCCESS: 1, |
158 CARDS_PARSE_SUCCESS: 2, | 159 CARDS_PARSE_SUCCESS: 2, |
159 DISMISS_REQUEST_TOTAL: 3, | 160 DISMISS_REQUEST_TOTAL: 3, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } else { | 218 } else { |
218 originalOnLoadEnd(event); | 219 originalOnLoadEnd(event); |
219 } | 220 } |
220 }); | 221 }); |
221 | 222 |
222 callbackBoolean(true); | 223 callbackBoolean(true); |
223 }); | 224 }); |
224 } | 225 } |
225 | 226 |
226 /** | 227 /** |
227 * Shows a notification and remembers information associated with it. | |
228 * @param {Object} card Google Now card represented as a set of parameters for | |
229 * showing a Chrome notification. | |
230 * @param {Object} notificationsData Map from notification id to the data | |
231 * associated with a notification. | |
232 * @param {number=} opt_previousVersion The version of the shown card with this | |
233 * id, if it exists, undefined otherwise. | |
234 */ | |
235 function showNotification(card, notificationsData, opt_previousVersion) { | |
236 console.log('showNotification ' + JSON.stringify(card) + ' ' + | |
237 opt_previousVersion); | |
238 | |
239 if (typeof card.version != 'number') { | |
240 console.error('card.version is not a number'); | |
241 // Fix card version. | |
242 card.version = opt_previousVersion !== undefined ? opt_previousVersion : 0; | |
243 } | |
244 | |
245 if (opt_previousVersion !== card.version) { | |
246 try { | |
247 // Delete a notification with the specified id if it already exists, and | |
248 // then create a notification. | |
249 chrome.notifications.create( | |
250 card.notificationId, | |
251 card.notification, | |
252 function(notificationId) { | |
253 if (!notificationId || chrome.runtime.lastError) { | |
254 var errorMessage = | |
255 chrome.runtime.lastError && chrome.runtime.lastError.message; | |
256 console.error('notifications.create: ID=' + notificationId + | |
257 ', ERROR=' + errorMessage); | |
258 } | |
259 }); | |
260 } catch (error) { | |
261 console.error('Error in notifications.create: ' + error); | |
262 } | |
263 } else { | |
264 try { | |
265 // Update existing notification. | |
266 chrome.notifications.update( | |
267 card.notificationId, | |
268 card.notification, | |
269 function(wasUpdated) { | |
270 if (!wasUpdated || chrome.runtime.lastError) { | |
271 var errorMessage = | |
272 chrome.runtime.lastError && chrome.runtime.lastError.message; | |
273 console.error('notifications.update: UPDATED=' + wasUpdated + | |
274 ', ERROR=' + errorMessage); | |
275 } | |
276 }); | |
277 } catch (error) { | |
278 console.error('Error in notifications.update: ' + error); | |
279 } | |
280 } | |
281 | |
282 notificationsData[card.notificationId] = { | |
283 actionUrls: card.actionUrls, | |
284 version: card.version, | |
285 dismissalParameters: card.dismissal | |
286 }; | |
287 } | |
288 | |
289 /** | |
290 * Parses JSON response from the notification server, show notifications and | 228 * Parses JSON response from the notification server, show notifications and |
291 * schedule next update. | 229 * schedule next update. |
292 * @param {string} response Server response. | 230 * @param {string} response Server response. |
293 * @param {function()} callback Completion callback. | 231 * @param {function()} callback Completion callback. |
294 */ | 232 */ |
295 function parseAndShowNotificationCards(response, callback) { | 233 function parseAndShowNotificationCards(response, callback) { |
296 console.log('parseAndShowNotificationCards ' + response); | 234 console.log('parseAndShowNotificationCards ' + response); |
297 try { | 235 try { |
298 var parsedResponse = JSON.parse(response); | 236 var parsedResponse = JSON.parse(response); |
299 } catch (error) { | 237 } catch (error) { |
300 console.error('parseAndShowNotificationCards parse error: ' + error); | 238 console.error('parseAndShowNotificationCards parse error: ' + error); |
301 callback(); | 239 callback(); |
302 return; | 240 return; |
303 } | 241 } |
304 | 242 |
305 var cards = parsedResponse.cards; | 243 var cards = parsedResponse.cards; |
306 | 244 |
307 if (!(cards instanceof Array)) { | 245 if (!(cards instanceof Array)) { |
308 callback(); | 246 callback(); |
309 return; | 247 return; |
310 } | 248 } |
311 | 249 |
312 if (typeof parsedResponse.expiration_timestamp_seconds != 'number') { | 250 if (typeof parsedResponse.next_poll_seconds != 'number') { |
313 callback(); | 251 callback(); |
314 return; | 252 return; |
315 } | 253 } |
316 | 254 |
317 tasks.debugSetStepName('parseAndShowNotificationCards-storage-get'); | 255 tasks.debugSetStepName('parseAndShowNotificationCards-storage-get'); |
318 storage.get(['notificationsData', 'recentDismissals'], function(items) { | 256 storage.get(['notificationsData', 'recentDismissals'], function(items) { |
319 console.log('parseAndShowNotificationCards-get ' + JSON.stringify(items)); | 257 console.log('parseAndShowNotificationCards-get ' + JSON.stringify(items)); |
320 items.notificationsData = items.notificationsData || {}; | 258 items.notificationsData = items.notificationsData || {}; |
321 items.recentDismissals = items.recentDismissals || {}; | 259 items.recentDismissals = items.recentDismissals || {}; |
322 | 260 |
(...skipping 29 matching lines...) Expand all Loading... |
352 updatedNotifications[notificationId] = true; | 290 updatedNotifications[notificationId] = true; |
353 } | 291 } |
354 } | 292 } |
355 | 293 |
356 // Delete notifications that didn't receive an update. | 294 // Delete notifications that didn't receive an update. |
357 for (var notificationId in notifications) { | 295 for (var notificationId in notifications) { |
358 console.log('parseAndShowNotificationCards-delete-check ' + | 296 console.log('parseAndShowNotificationCards-delete-check ' + |
359 notificationId); | 297 notificationId); |
360 if (!(notificationId in updatedNotifications)) { | 298 if (!(notificationId in updatedNotifications)) { |
361 console.log('parseAndShowNotificationCards-delete ' + notificationId); | 299 console.log('parseAndShowNotificationCards-delete ' + notificationId); |
362 chrome.notifications.clear( | 300 cardSet.clear(notificationId); |
363 notificationId, | |
364 function() {}); | |
365 } | 301 } |
366 } | 302 } |
367 | 303 |
368 recordEvent(DiagnosticEvent.CARDS_PARSE_SUCCESS); | 304 recordEvent(DiagnosticEvent.CARDS_PARSE_SUCCESS); |
369 | 305 |
370 // Create/update notifications and store their new properties. | 306 // Create/update notifications and store their new properties. |
371 var newNotificationsData = {}; | 307 var newNotificationsData = {}; |
372 for (var i = 0; i < cards.length; ++i) { | 308 for (var i = 0; i < cards.length; ++i) { |
373 var card = cards[i]; | 309 var card = cards[i]; |
374 if (!(card.notificationId in updatedRecentDismissals)) { | 310 if (!(card.notificationId in updatedRecentDismissals)) { |
375 var notificationData = items.notificationsData[card.notificationId]; | 311 var notificationData = items.notificationsData[card.notificationId]; |
376 var previousVersion = notifications[card.notificationId] && | 312 var previousVersion = notifications[card.notificationId] && |
377 notificationData && | 313 notificationData && |
378 notificationData.previousVersion; | 314 notificationData.cardCreateInfo && |
379 showNotification(card, newNotificationsData, previousVersion); | 315 notificationData.cardCreateInfo.version; |
| 316 newNotificationsData[card.notificationId] = |
| 317 cardSet.update(card, previousVersion); |
380 } | 318 } |
381 } | 319 } |
382 | 320 |
383 updateCardsAttempts.start(parsedResponse.expiration_timestamp_seconds); | 321 updateCardsAttempts.start(parsedResponse.next_poll_seconds); |
384 | 322 |
385 storage.set({ | 323 storage.set({ |
386 notificationsData: newNotificationsData, | 324 notificationsData: newNotificationsData, |
387 recentDismissals: updatedRecentDismissals | 325 recentDismissals: updatedRecentDismissals |
388 }); | 326 }); |
389 callback(); | 327 callback(); |
390 }); | 328 }); |
391 }); | 329 }); |
392 } | 330 } |
393 | 331 |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 } | 601 } |
664 | 602 |
665 // At this point we are guaranteed that the notification is a now card. | 603 // At this point we are guaranteed that the notification is a now card. |
666 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed'); | 604 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed'); |
667 | 605 |
668 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { | 606 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { |
669 dismissalAttempts.start(); | 607 dismissalAttempts.start(); |
670 | 608 |
671 // Deleting the notification in case it was re-added while this task was | 609 // Deleting the notification in case it was re-added while this task was |
672 // scheduled, waiting for execution. | 610 // scheduled, waiting for execution. |
673 chrome.notifications.clear( | 611 cardSet.clear(notificationId); |
674 notificationId, | |
675 function() {}); | |
676 | 612 |
677 tasks.debugSetStepName('onNotificationClosed-storage-get'); | 613 tasks.debugSetStepName('onNotificationClosed-storage-get'); |
678 storage.get(['pendingDismissals', 'notificationsData'], function(items) { | 614 storage.get(['pendingDismissals', 'notificationsData'], function(items) { |
679 items.pendingDismissals = items.pendingDismissals || []; | 615 items.pendingDismissals = items.pendingDismissals || []; |
680 items.notificationsData = items.notificationsData || {}; | 616 items.notificationsData = items.notificationsData || {}; |
681 | 617 |
682 var notificationData = items.notificationsData[notificationId]; | 618 var notificationData = items.notificationsData[notificationId]; |
683 | 619 |
684 var dismissal = { | 620 var dismissal = { |
685 notificationId: notificationId, | 621 notificationId: notificationId, |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 | 727 |
792 chrome.location.onLocationUpdate.addListener(function(position) { | 728 chrome.location.onLocationUpdate.addListener(function(position) { |
793 recordEvent(DiagnosticEvent.LOCATION_UPDATE); | 729 recordEvent(DiagnosticEvent.LOCATION_UPDATE); |
794 updateNotificationsCards(position); | 730 updateNotificationsCards(position); |
795 }); | 731 }); |
796 | 732 |
797 chrome.omnibox.onInputEntered.addListener(function(text) { | 733 chrome.omnibox.onInputEntered.addListener(function(text) { |
798 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 734 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
799 initialize(); | 735 initialize(); |
800 }); | 736 }); |
OLD | NEW |