Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: chrome/browser/resources/google_now/background.js

Issue 27223006: Requesting cards on push messages (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase and robliao comments Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 } 522 }
516 523
517 scheduleNextPoll(items.notificationGroups); 524 scheduleNextPoll(items.notificationGroups);
518 chrome.storage.local.set({notificationGroups: items.notificationGroups}); 525 chrome.storage.local.set({notificationGroups: items.notificationGroups});
519 mergeAndShowNotificationCards(items.notificationGroups); 526 mergeAndShowNotificationCards(items.notificationGroups);
520 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS); 527 recordEvent(GoogleNowEvent.CARDS_PARSE_SUCCESS);
521 }); 528 });
522 } 529 }
523 530
524 /** 531 /**
532 * Requests notification cards from the server for specified groups.
533 * @param {Array.<string>} groupNames Names of groups that need to be refreshed.
534 */
535 function requestNotificationGroups(groupNames) {
536 console.log('requestNotificationGroups from ' + NOTIFICATION_CARDS_URL +
537 ', groupNames=' + JSON.stringify(groupNames));
538
539 if (!NOTIFICATION_CARDS_URL)
540 return;
541
542 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_TOTAL);
543
544 var requestParameters = '?timeZoneOffsetMs=' +
545 (-new Date().getTimezoneOffset() * MS_IN_MINUTE);
546
547 groupNames.forEach(function(groupName) {
548 requestParameters += ('&requestTypes=' + groupName);
549 });
550
551 console.log('requestNotificationGroups: request=' + requestParameters);
552
553 var request = buildServerRequest('GET', 'notifications' + requestParameters);
554
555 request.onloadend = function(event) {
556 console.log('requestNotificationGroups-onloadend ' + request.status);
557 if (request.status == HTTP_OK) {
558 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS);
559 parseAndShowNotificationCards(request.response);
560 }
561 };
562
563 setAuthorization(request, function(success) {
rgustafson 2013/10/15 20:56:54 But the signed-in condition isn't missing, it's ri
vadimt 2013/10/15 21:14:09 I'm not saying I'm changing the behavior. The chec
rgustafson 2013/10/16 17:14:32 There are two problems here: (1) Duplicate code be
vadimt 2013/10/16 20:10:20 I've changed implementation to use updateCardsAtte
564 if (success)
565 request.send();
566 });
567 }
568
569 /**
525 * Requests notification cards from the server. 570 * Requests notification cards from the server.
526 * @param {Location} position Location of this computer. 571 * @param {Location} position Location of this computer.
527 */ 572 */
528 function requestNotificationCards(position) { 573 function requestNotificationCards(position) {
529 console.log('requestNotificationCards ' + JSON.stringify(position) + 574 console.log('requestNotificationCards ' + JSON.stringify(position));
530 ' from ' + NOTIFICATION_CARDS_URL);
531
532 if (!NOTIFICATION_CARDS_URL)
533 return;
534
535 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_TOTAL);
536 575
537 instrumented.storage.local.get('notificationGroups', function(items) { 576 instrumented.storage.local.get('notificationGroups', function(items) {
538 console.log('requestNotificationCards-storage-get ' + 577 console.log('requestNotificationCards-storage-get ' +
539 JSON.stringify(items)); 578 JSON.stringify(items));
540 items = items || {}; 579 items = items || {};
541 580
542 var requestParameters = '?timeZoneOffsetMs=' + 581 var requestParameters = '?timeZoneOffsetMs=' +
rgustafson 2013/10/15 20:56:54 Remove this. It was moved.
vadimt 2013/10/15 21:14:09 Done.
543 (-new Date().getTimezoneOffset() * MS_IN_MINUTE); 582 (-new Date().getTimezoneOffset() * MS_IN_MINUTE);
544 583
584 var groupsToRequest = [];
585
545 if (items.notificationGroups) { 586 if (items.notificationGroups) {
546 var now = Date.now(); 587 var now = Date.now();
547 588
548 for (var groupName in items.notificationGroups) { 589 for (var groupName in items.notificationGroups) {
549 var group = items.notificationGroups[groupName]; 590 var group = items.notificationGroups[groupName];
550 if (group.nextPollTime <= now) 591 if (group.nextPollTime <= now)
551 requestParameters += ('&requestTypes=' + groupName); 592 groupsToRequest.push(groupName);
552 } 593 }
553 } 594 }
554 595
555 console.log('requestNotificationCards: request=' + requestParameters); 596 requestNotificationGroups(groupsToRequest);
556
557 var request = buildServerRequest('GET',
558 'notifications' + requestParameters);
559
560 request.onloadend = function(event) {
561 console.log('requestNotificationCards-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 }); 597 });
573 } 598 }
574 599
575 /** 600 /**
576 * Starts getting location for a cards update. 601 * Starts getting location for a cards update.
577 */ 602 */
578 function requestLocation() { 603 function requestLocation() {
579 console.log('requestLocation'); 604 console.log('requestLocation');
580 recordEvent(GoogleNowEvent.LOCATION_REQUEST); 605 recordEvent(GoogleNowEvent.LOCATION_REQUEST);
581 // TODO(vadimt): Figure out location request options. 606 // TODO(vadimt): Figure out location request options.
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after
1145 1170
1146 instrumented.location.onLocationUpdate.addListener(function(position) { 1171 instrumented.location.onLocationUpdate.addListener(function(position) {
1147 recordEvent(GoogleNowEvent.LOCATION_UPDATE); 1172 recordEvent(GoogleNowEvent.LOCATION_UPDATE);
1148 updateNotificationsCards(position); 1173 updateNotificationsCards(position);
1149 }); 1174 });
1150 1175
1151 instrumented.omnibox.onInputEntered.addListener(function(text) { 1176 instrumented.omnibox.onInputEntered.addListener(function(text) {
1152 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; 1177 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text;
1153 initialize(); 1178 initialize();
1154 }); 1179 });
1180
1181 instrumented.pushMessaging.onMessage.addListener(function(message) {
1182 console.log('pushMessaging.onMessage ' + JSON.stringify(message));
1183 if (message.subchannelId == SUBCHANNEL_ID_POLL_NOW && message.payload) {
1184 tasks.add(ON_PUSH_MESSAGE_START_TASK_NAME, function() {
1185 instrumented.storage.local.get('lastPollNowPayload', function(items) {
1186 if (items && items.lastPollNowPayload != message.payload) {
rgustafson 2013/10/15 17:57:51 What are you expecting in the payload and why does
vadimt 2013/10/15 19:41:43 When the extension start for the first time, you g
rgustafson 2013/10/15 20:56:54 Some comments of that explanation, at least what y
vadimt 2013/10/15 21:14:09 Done.
1187 chrome.storage.local.set({lastPollNowPayload: message.payload});
1188
1189 authenticationManager.isSignedIn(function(token) {
rgustafson 2013/10/15 17:57:51 You can receive push messages without being signed
vadimt 2013/10/15 19:41:43 It's more for readability and solidity of the code
1190 if (token)
1191 requestNotificationGroups([]);
rgustafson 2013/10/16 17:14:32 Why isn't this (and the surrounding sign in check)
vadimt 2013/10/16 20:10:20 The fact that we received a push notification does
1192 });
1193 }
1194 });
1195 });
1196 }
1197 });
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/manifest.json » ('j') | chrome/browser/resources/google_now/manifest.json » ('J')

Powered by Google App Engine
This is Rietveld 408576698