Chromium Code Reviews| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 var DISMISS_RETENTION_TIME_MS = 20 * 60 * 1000; // 20 minutes | 74 var DISMISS_RETENTION_TIME_MS = 20 * 60 * 1000; // 20 minutes |
| 75 | 75 |
| 76 /** | 76 /** |
| 77 * Names for tasks that can be created by the extension. | 77 * Names for tasks that can be created by the extension. |
| 78 */ | 78 */ |
| 79 var UPDATE_CARDS_TASK_NAME = 'update-cards'; | 79 var UPDATE_CARDS_TASK_NAME = 'update-cards'; |
| 80 var DISMISS_CARD_TASK_NAME = 'dismiss-card'; | 80 var DISMISS_CARD_TASK_NAME = 'dismiss-card'; |
| 81 var RETRY_DISMISS_TASK_NAME = 'retry-dismiss'; | 81 var RETRY_DISMISS_TASK_NAME = 'retry-dismiss'; |
| 82 var STATE_CHANGED_TASK_NAME = 'state-changed'; | 82 var STATE_CHANGED_TASK_NAME = 'state-changed'; |
| 83 var SHOW_ON_START_TASK_NAME = 'show-cards-on-start'; | 83 var SHOW_ON_START_TASK_NAME = 'show-cards-on-start'; |
| 84 var ON_PUSH_MESSAGE_START_TASK_NAME = 'on-push-message'; | |
| 84 | 85 |
| 85 var LOCATION_WATCH_NAME = 'location-watch'; | 86 var LOCATION_WATCH_NAME = 'location-watch'; |
| 86 | 87 |
| 87 var WELCOME_TOAST_NOTIFICATION_ID = 'enable-now-toast'; | 88 var WELCOME_TOAST_NOTIFICATION_ID = 'enable-now-toast'; |
| 88 | 89 |
| 89 /** | 90 /** |
| 91 * Chrome push messaging subchannel for messages causing an immediate poll. | |
| 92 */ | |
| 93 var SUBCHANNEL_ID_POLL_NOW = 0; | |
| 94 | |
| 95 /** | |
| 90 * The indices of the buttons that are displayed on the welcome toast. | 96 * The indices of the buttons that are displayed on the welcome toast. |
| 91 * @enum {number} | 97 * @enum {number} |
| 92 */ | 98 */ |
| 93 var ToastButtonIndex = {YES: 0, NO: 1}; | 99 var ToastButtonIndex = {YES: 0, NO: 1}; |
| 94 | 100 |
| 95 /** | 101 /** |
| 96 * Notification as it's sent by the server. | 102 * Notification as it's sent by the server. |
| 97 * | 103 * |
| 98 * @typedef {{ | 104 * @typedef {{ |
| 99 * notificationId: string, | 105 * notificationId: string, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); | 167 wrapper.instrumentChromeApiFunction('notifications.onClicked.addListener', 0); |
| 162 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); | 168 wrapper.instrumentChromeApiFunction('notifications.onClosed.addListener', 0); |
| 163 wrapper.instrumentChromeApiFunction('omnibox.onInputEntered.addListener', 0); | 169 wrapper.instrumentChromeApiFunction('omnibox.onInputEntered.addListener', 0); |
| 164 wrapper.instrumentChromeApiFunction( | 170 wrapper.instrumentChromeApiFunction( |
| 165 'preferencesPrivate.googleGeolocationAccessEnabled.get', | 171 'preferencesPrivate.googleGeolocationAccessEnabled.get', |
| 166 1); | 172 1); |
| 167 wrapper.instrumentChromeApiFunction( | 173 wrapper.instrumentChromeApiFunction( |
| 168 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', | 174 'preferencesPrivate.googleGeolocationAccessEnabled.onChange.addListener', |
| 169 0); | 175 0); |
| 170 wrapper.instrumentChromeApiFunction('permissions.contains', 1); | 176 wrapper.instrumentChromeApiFunction('permissions.contains', 1); |
| 177 wrapper.instrumentChromeApiFunction('pushMessaging.onMessage.addListener', 0); | |
| 171 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); | 178 wrapper.instrumentChromeApiFunction('runtime.onInstalled.addListener', 0); |
| 172 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); | 179 wrapper.instrumentChromeApiFunction('runtime.onStartup.addListener', 0); |
| 173 wrapper.instrumentChromeApiFunction('tabs.create', 1); | 180 wrapper.instrumentChromeApiFunction('tabs.create', 1); |
| 174 wrapper.instrumentChromeApiFunction('storage.local.get', 1); | 181 wrapper.instrumentChromeApiFunction('storage.local.get', 1); |
| 175 | 182 |
| 176 var updateCardsAttempts = buildAttemptManager( | 183 var updateCardsAttempts = buildAttemptManager( |
| 177 'cards-update', | 184 'cards-update', |
| 178 requestLocation, | 185 requestLocation, |
| 179 INITIAL_POLLING_PERIOD_SECONDS, | 186 INITIAL_POLLING_PERIOD_SECONDS, |
| 180 MAXIMUM_POLLING_PERIOD_SECONDS); | 187 MAXIMUM_POLLING_PERIOD_SECONDS); |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 520 } | 527 } |
| 521 | 528 |
| 522 scheduleNextPoll(items.notificationGroups); | 529 scheduleNextPoll(items.notificationGroups); |
| 523 chrome.storage.local.set({notificationGroups: items.notificationGroups}); | 530 chrome.storage.local.set({notificationGroups: items.notificationGroups}); |
| 524 mergeAndShowNotificationCards(); | 531 mergeAndShowNotificationCards(); |
| 525 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); | 532 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); |
| 526 }); | 533 }); |
| 527 } | 534 } |
| 528 | 535 |
| 529 /** | 536 /** |
| 537 * Requests notification cards from the server for specified groups. | |
| 538 * @param {Array.<string>} groupNames Names of groups that need to be regreshed. | |
| 539 */ | |
| 540 function requestNotificationGroups(groupNames) { | |
| 541 console.log('requestNotificationGroups from ' + NOTIFICATION_CARDS_URL + | |
| 542 ', groupNames=' + JSON.stringify(groupNames)); | |
| 543 | |
| 544 if (!NOTIFICATION_CARDS_URL) | |
| 545 return; | |
| 546 | |
| 547 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_TOTAL); | |
| 548 | |
| 549 var requestParameters = '?timeZoneOffsetMs=' + | |
| 550 (-new Date().getTimezoneOffset() * MS_IN_MINUTE); | |
| 551 | |
| 552 groupNames.forEach(function(groupName) { | |
| 553 requestParameters += ('&requestTypes=' + groupName); | |
| 554 }); | |
| 555 | |
| 556 console.log('requestNotificationGroups: request=' + requestParameters); | |
| 557 | |
| 558 var request = buildServerRequest('GET', 'notifications' + requestParameters); | |
| 559 | |
| 560 request.onloadend = function(event) { | |
| 561 console.log('requestNotificationGroups-onloadend ' + request.status); | |
| 562 if (request.status == HTTP_OK) { | |
| 563 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS); | |
| 564 parseAndShowNotificationCards(request.response); | |
| 565 } | |
| 566 }; | |
| 567 | |
| 568 setAuthorization(request, function(success) { | |
| 569 if (success) | |
| 570 request.send(); | |
| 571 }); | |
| 572 } | |
| 573 | |
| 574 /** | |
| 530 * Requests notification cards from the server. | 575 * Requests notification cards from the server. |
| 531 * @param {Location} position Location of this computer. | 576 * @param {Location} position Location of this computer. |
| 532 */ | 577 */ |
| 533 function requestNotificationCards(position) { | 578 function requestNotificationCards(position) { |
| 534 console.log('requestNotificationCards ' + JSON.stringify(position) + | 579 console.log('requestNotificationCards ' + JSON.stringify(position)); |
| 535 ' from ' + NOTIFICATION_CARDS_URL); | |
| 536 | |
| 537 if (!NOTIFICATION_CARDS_URL) | |
| 538 return; | |
| 539 | |
| 540 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_TOTAL); | |
| 541 | 580 |
| 542 instrumented.storage.local.get('notificationGroups', function(items) { | 581 instrumented.storage.local.get('notificationGroups', function(items) { |
| 543 console.log('requestNotificationCards-storage-get ' + | 582 console.log('requestNotificationCards-storage-get ' + |
| 544 JSON.stringify(items)); | 583 JSON.stringify(items)); |
| 545 items = items || {}; | 584 items = items || {}; |
| 546 | 585 |
| 547 var requestParameters = '?timeZoneOffsetMs=' + | 586 var requestParameters = '?timeZoneOffsetMs=' + |
| 548 (-new Date().getTimezoneOffset() * MS_IN_MINUTE); | 587 (-new Date().getTimezoneOffset() * MS_IN_MINUTE); |
| 549 | 588 |
| 589 var groupsToRequest = []; | |
| 590 | |
| 550 if (items.notificationGroups) { | 591 if (items.notificationGroups) { |
| 551 var now = Date.now(); | 592 var now = Date.now(); |
| 552 | 593 |
| 553 for (var groupName in items.notificationGroups) { | 594 for (var groupName in items.notificationGroups) { |
| 554 var group = items.notificationGroups[groupName]; | 595 var group = items.notificationGroups[groupName]; |
| 555 if (group.nextPollTime <= now) | 596 if (group.nextPollTime <= now) |
| 556 requestParameters += ('&requestTypes=' + groupName); | 597 groupsToRequest.push(groupName); |
| 557 } | 598 } |
| 558 } | 599 } |
| 559 | 600 |
| 560 console.log('requestNotificationCards: request=' + requestParameters); | 601 requestNotificationGroups(groupsToRequest); |
| 561 | |
| 562 var request = buildServerRequest('GET', | |
| 563 'notifications' + requestParameters); | |
| 564 | |
| 565 request.onloadend = function(event) { | |
| 566 console.log('requestNotificationCards-onloadend ' + request.status); | |
| 567 if (request.status == HTTP_OK) { | |
| 568 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS); | |
| 569 parseAndShowNotificationCards(request.response); | |
| 570 } | |
| 571 }; | |
| 572 | |
| 573 setAuthorization(request, function(success) { | |
| 574 if (success) | |
| 575 request.send(); | |
| 576 }); | |
| 577 }); | 602 }); |
| 578 } | 603 } |
| 579 | 604 |
| 580 /** | 605 /** |
| 581 * Starts getting location for a cards update. | 606 * Starts getting location for a cards update. |
| 582 */ | 607 */ |
| 583 function requestLocation() { | 608 function requestLocation() { |
| 584 console.log('requestLocation'); | 609 console.log('requestLocation'); |
| 585 recordEvent(GoogleNowEvent.LOCATION_REQUEST); | 610 recordEvent(GoogleNowEvent.LOCATION_REQUEST); |
| 586 // TODO(vadimt): Figure out location request options. | 611 // TODO(vadimt): Figure out location request options. |
| (...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1140 | 1165 |
| 1141 instrumented.location.onLocationUpdate.addListener(function(position) { | 1166 instrumented.location.onLocationUpdate.addListener(function(position) { |
| 1142 recordEvent(GoogleNowEvent.LOCATION_UPDATE); | 1167 recordEvent(GoogleNowEvent.LOCATION_UPDATE); |
| 1143 updateNotificationsCards(position); | 1168 updateNotificationsCards(position); |
| 1144 }); | 1169 }); |
| 1145 | 1170 |
| 1146 instrumented.omnibox.onInputEntered.addListener(function(text) { | 1171 instrumented.omnibox.onInputEntered.addListener(function(text) { |
| 1147 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; | 1172 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; |
| 1148 initialize(); | 1173 initialize(); |
| 1149 }); | 1174 }); |
| 1175 | |
| 1176 instrumented.pushMessaging.onMessage.addListener(function(message) { | |
| 1177 if (message.subchannelId == SUBCHANNEL_ID_POLL_NOW && message.payload) { | |
| 1178 tasks.add(ON_PUSH_MESSAGE_START_TASK_NAME, function() { | |
| 1179 instrumented.storage.local.get('lastPollNowPayload', function(items) { | |
| 1180 if (items && items.lastPollNowPayload != message.payload) { | |
| 1181 chrome.storage.local.set({lastPollNowPayload: message.payload}); | |
| 1182 | |
| 1183 authenticationManager.isSignedIn(function(token) { | |
| 1184 if (token) | |
| 1185 requestNotificationGroups([]); | |
| 1186 }); | |
| 1187 } | |
| 1188 }); | |
| 1189 }); | |
| 1190 } | |
| 1191 console.log(JSON.stringify(message)); | |
|
robliao
2013/10/15 00:04:55
This should either be removed or annotated with wh
vadimt
2013/10/15 01:25:47
Done.
| |
| 1192 }); | |
| OLD | NEW |