| 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 = buildCardManager(); |
| 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 }; | |
| 286 } | |
| 287 | |
| 288 /** | |
| 289 * Parses JSON response from the notification server, show notifications and | 228 * Parses JSON response from the notification server, show notifications and |
| 290 * schedule next update. | 229 * schedule next update. |
| 291 * @param {string} response Server response. | 230 * @param {string} response Server response. |
| 292 * @param {function()} callback Completion callback. | 231 * @param {function()} callback Completion callback. |
| 293 */ | 232 */ |
| 294 function parseAndShowNotificationCards(response, callback) { | 233 function parseAndShowNotificationCards(response, callback) { |
| 295 console.log('parseAndShowNotificationCards ' + response); | 234 console.log('parseAndShowNotificationCards ' + response); |
| 296 try { | 235 try { |
| 297 var parsedResponse = JSON.parse(response); | 236 var parsedResponse = JSON.parse(response); |
| 298 } catch (error) { | 237 } catch (error) { |
| 299 console.error('parseAndShowNotificationCards parse error: ' + error); | 238 console.error('parseAndShowNotificationCards parse error: ' + error); |
| 300 callback(); | 239 callback(); |
| 301 return; | 240 return; |
| 302 } | 241 } |
| 303 | 242 |
| 304 var cards = parsedResponse.cards; | 243 var cards = parsedResponse.cards; |
| 305 | 244 |
| 306 if (!(cards instanceof Array)) { | 245 if (!(cards instanceof Array)) { |
| 307 callback(); | 246 callback(); |
| 308 return; | 247 return; |
| 309 } | 248 } |
| 310 | 249 |
| 311 if (typeof parsedResponse.expiration_timestamp_seconds != 'number') { | 250 if (typeof parsedResponse.next_poll_seconds != 'number') { |
| 312 callback(); | 251 callback(); |
| 313 return; | 252 return; |
| 314 } | 253 } |
| 315 | 254 |
| 316 tasks.debugSetStepName('parseAndShowNotificationCards-storage-get'); | 255 tasks.debugSetStepName('parseAndShowNotificationCards-storage-get'); |
| 317 storage.get(['notificationsData', 'recentDismissals'], function(items) { | 256 storage.get(['notificationsData', 'recentDismissals'], function(items) { |
| 318 console.log('parseAndShowNotificationCards-get ' + JSON.stringify(items)); | 257 console.log('parseAndShowNotificationCards-get ' + JSON.stringify(items)); |
| 319 items.notificationsData = items.notificationsData || {}; | 258 items.notificationsData = items.notificationsData || {}; |
| 320 items.recentDismissals = items.recentDismissals || {}; | 259 items.recentDismissals = items.recentDismissals || {}; |
| 321 | 260 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 351 updatedNotifications[notificationId] = true; | 290 updatedNotifications[notificationId] = true; |
| 352 } | 291 } |
| 353 } | 292 } |
| 354 | 293 |
| 355 // Delete notifications that didn't receive an update. | 294 // Delete notifications that didn't receive an update. |
| 356 for (var notificationId in notifications) { | 295 for (var notificationId in notifications) { |
| 357 console.log('parseAndShowNotificationCards-delete-check ' + | 296 console.log('parseAndShowNotificationCards-delete-check ' + |
| 358 notificationId); | 297 notificationId); |
| 359 if (!(notificationId in updatedNotifications)) { | 298 if (!(notificationId in updatedNotifications)) { |
| 360 console.log('parseAndShowNotificationCards-delete ' + notificationId); | 299 console.log('parseAndShowNotificationCards-delete ' + notificationId); |
| 361 chrome.notifications.clear( | 300 cardSet.clear(notificationId); |
| 362 notificationId, | |
| 363 function() {}); | |
| 364 } | 301 } |
| 365 } | 302 } |
| 366 | 303 |
| 367 recordEvent(DiagnosticEvent.CARDS_PARSE_SUCCESS); | 304 recordEvent(DiagnosticEvent.CARDS_PARSE_SUCCESS); |
| 368 | 305 |
| 369 // Create/update notifications and store their new properties. | 306 // Create/update notifications and store their new properties. |
| 370 var newNotificationsData = {}; | 307 var newNotificationsData = {}; |
| 371 for (var i = 0; i < cards.length; ++i) { | 308 for (var i = 0; i < cards.length; ++i) { |
| 372 var card = cards[i]; | 309 var card = cards[i]; |
| 373 if (!(card.notificationId in updatedRecentDismissals)) { | 310 if (!(card.notificationId in updatedRecentDismissals)) { |
| 374 var notificationData = items.notificationsData[card.notificationId]; | 311 var notificationData = items.notificationsData[card.notificationId]; |
| 375 var previousVersion = notifications[card.notificationId] && | 312 var previousVersion = notifications[card.notificationId] && |
| 376 notificationData && | 313 notificationData && |
| 377 notificationData.previousVersion; | 314 notificationData.cardCreateInfo && |
| 378 showNotification(card, newNotificationsData, previousVersion); | 315 notificationData.cardCreateInfo.version; |
| 316 newNotificationsData[card.notificationId] = |
| 317 cardSet.update(card, previousVersion); |
| 379 } | 318 } |
| 380 } | 319 } |
| 381 | 320 |
| 382 updateCardsAttempts.start(parsedResponse.expiration_timestamp_seconds); | 321 updateCardsAttempts.start(parsedResponse.next_poll_seconds); |
| 383 | 322 |
| 384 storage.set({ | 323 storage.set({ |
| 385 notificationsData: newNotificationsData, | 324 notificationsData: newNotificationsData, |
| 386 recentDismissals: updatedRecentDismissals | 325 recentDismissals: updatedRecentDismissals |
| 387 }); | 326 }); |
| 388 callback(); | 327 callback(); |
| 389 }); | 328 }); |
| 390 }); | 329 }); |
| 391 } | 330 } |
| 392 | 331 |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 } | 593 } |
| 655 | 594 |
| 656 // At this point we are guaranteed that the notification is a now card. | 595 // At this point we are guaranteed that the notification is a now card. |
| 657 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed'); | 596 chrome.metricsPrivate.recordUserAction('GoogleNow.Dismissed'); |
| 658 | 597 |
| 659 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { | 598 tasks.add(DISMISS_CARD_TASK_NAME, function(callback) { |
| 660 dismissalAttempts.start(); | 599 dismissalAttempts.start(); |
| 661 | 600 |
| 662 // Deleting the notification in case it was re-added while this task was | 601 // Deleting the notification in case it was re-added while this task was |
| 663 // scheduled, waiting for execution. | 602 // scheduled, waiting for execution. |
| 664 chrome.notifications.clear( | 603 cardSet.clear(notificationId); |
| 665 notificationId, | |
| 666 function() {}); | |
| 667 | 604 |
| 668 tasks.debugSetStepName('onNotificationClosed-get-pendingDismissals'); | 605 tasks.debugSetStepName('onNotificationClosed-get-pendingDismissals'); |
| 669 storage.get('pendingDismissals', function(items) { | 606 storage.get('pendingDismissals', function(items) { |
| 670 items.pendingDismissals = items.pendingDismissals || []; | 607 items.pendingDismissals = items.pendingDismissals || []; |
| 671 | 608 |
| 672 var dismissal = { | 609 var dismissal = { |
| 673 notificationId: notificationId, | 610 notificationId: notificationId, |
| 674 time: Date.now() | 611 time: Date.now() |
| 675 }; | 612 }; |
| 676 items.pendingDismissals.push(dismissal); | 613 items.pendingDismissals.push(dismissal); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 778 | 715 |
| 779 chrome.location.onLocationUpdate.addListener(function(position) { | 716 chrome.location.onLocationUpdate.addListener(function(position) { |
| 780 recordEvent(DiagnosticEvent.LOCATION_UPDATE); | 717 recordEvent(DiagnosticEvent.LOCATION_UPDATE); |
| 781 updateNotificationsCards(position); | 718 updateNotificationsCards(position); |
| 782 }); | 719 }); |
| 783 | 720 |
| 784 chrome.omnibox.onInputEntered.addListener(function(text) { | 721 chrome.omnibox.onInputEntered.addListener(function(text) { |
| 785 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 722 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
| 786 initialize(); | 723 initialize(); |
| 787 }); | 724 }); |
| OLD | NEW |