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 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 type: 'histogram-linear', | 241 type: 'histogram-linear', |
242 min: 1, | 242 min: 1, |
243 max: GoogleNowEvent.EVENTS_TOTAL, | 243 max: GoogleNowEvent.EVENTS_TOTAL, |
244 buckets: GoogleNowEvent.EVENTS_TOTAL + 1 | 244 buckets: GoogleNowEvent.EVENTS_TOTAL + 1 |
245 }; | 245 }; |
246 | 246 |
247 chrome.metricsPrivate.recordValue(metricDescription, event); | 247 chrome.metricsPrivate.recordValue(metricDescription, event); |
248 } | 248 } |
249 | 249 |
250 /** | 250 /** |
251 * Adds authorization behavior to the request. | 251 * Builds and sends an authenticated request to the notification server. |
252 * @param {XMLHttpRequest} request Server request. | 252 * @param {string} method Request method. |
253 * @param {function(boolean)} callbackBoolean Completion callback with 'success' | 253 * @param {string} handlerName Server handler to send the request to. |
rgustafson
2014/03/07 00:53:06
This is more than the handler. It includes params
robliao
2014/03/07 01:39:40
Unfortunately there is no real name for the path+q
| |
254 * parameter. | 254 * @param {string=} opt_contentType Value for the Content-type header. |
255 * @return {Promise} A promise to issue a request to the server. | |
256 * The promise rejects if there is a client-side authentication issue. | |
255 */ | 257 */ |
256 function setAuthorization(request, callbackBoolean) { | 258 function requestFromServer(method, handlerName, opt_contentType) { |
257 authenticationManager.getAuthToken().then(function(token) { | 259 return authenticationManager.getAuthToken().then(function(token) { |
260 var request = buildServerRequest(method, handlerName, opt_contentType); | |
258 request.setRequestHeader('Authorization', 'Bearer ' + token); | 261 request.setRequestHeader('Authorization', 'Bearer ' + token); |
259 | 262 var requestPromise = new Promise(function(resolve) { |
260 // Instrument onloadend to remove stale auth tokens. | 263 request.addEventListener('loadend', function() { |
rgustafson
2014/03/07 00:53:06
any difference between this and doing request.onlo
robliao
2014/03/07 01:39:40
It doesn't overwrite any existing onloadend that w
| |
261 var originalOnLoadEnd = request.onloadend; | 264 resolve(request); |
262 request.onloadend = wrapper.wrapCallback(function(event) { | 265 }, false); |
rgustafson
2014/03/07 00:53:06
This parameter is optional and looks like it defau
robliao
2014/03/07 01:39:40
Given that the default is not consistent (false in
rgustafson
2014/03/11 21:29:30
It's false for Firefox: https://developer.mozilla.
robliao
2014/03/12 00:13:11
Instead we'll wonder what the default is :-) for t
| |
266 request.send(); | |
267 }); | |
268 requestPromise.then(function() { | |
263 if (request.status == HTTP_FORBIDDEN || | 269 if (request.status == HTTP_FORBIDDEN || |
264 request.status == HTTP_UNAUTHORIZED) { | 270 request.status == HTTP_UNAUTHORIZED) { |
265 authenticationManager.removeToken(token).then(function() { | 271 authenticationManager.removeToken(token); |
266 originalOnLoadEnd(event); | |
267 }); | |
268 } else { | |
269 originalOnLoadEnd(event); | |
270 } | 272 } |
271 }); | 273 }); |
272 | 274 return requestPromise; |
273 callbackBoolean(true); | |
274 }).catch(function() { | |
275 callbackBoolean(false); | |
276 }); | 275 }); |
277 } | 276 } |
278 | 277 |
279 /** | 278 /** |
280 * Shows parsed and combined cards as notifications. | 279 * Shows parsed and combined cards as notifications. |
281 * @param {Object.<string, StoredNotificationGroup>} notificationGroups Map from | 280 * @param {Object.<string, StoredNotificationGroup>} notificationGroups Map from |
282 * group name to group information. | 281 * group name to group information. |
283 * @param {Object.<ChromeNotificationId, CombinedCard>} cards Map from | 282 * @param {Object.<ChromeNotificationId, CombinedCard>} cards Map from |
284 * chromeNotificationId to the combined card, containing cards to show. | 283 * chromeNotificationId to the combined card, containing cards to show. |
285 * @param {function()} onSuccess Called on success. | 284 * @param {function()} onSuccess Called on success. |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
560 } | 559 } |
561 | 560 |
562 groupNames.forEach(function(groupName) { | 561 groupNames.forEach(function(groupName) { |
563 requestParameters += ('&requestTypes=' + groupName); | 562 requestParameters += ('&requestTypes=' + groupName); |
564 }); | 563 }); |
565 | 564 |
566 requestParameters += '&uiLocale=' + navigator.language; | 565 requestParameters += '&uiLocale=' + navigator.language; |
567 | 566 |
568 console.log('requestNotificationGroups: request=' + requestParameters); | 567 console.log('requestNotificationGroups: request=' + requestParameters); |
569 | 568 |
570 var request = buildServerRequest('GET', 'notifications' + requestParameters); | 569 requestFromServer('GET', 'notifications' + requestParameters).then( |
571 | 570 function(request) { |
572 request.onloadend = function(event) { | 571 console.log('requestNotificationGroups-received ' + request.status); |
573 console.log('requestNotificationGroups-onloadend ' + request.status); | 572 if (request.status == HTTP_OK) { |
574 if (request.status == HTTP_OK) { | 573 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS); |
575 recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS); | 574 processServerResponse( |
576 processServerResponse( | 575 JSON.parse(request.responseText), cardShownCallback); |
577 JSON.parse(request.responseText), cardShownCallback); | 576 } |
578 } | 577 }); |
579 }; | |
580 | |
581 setAuthorization(request, function(success) { | |
582 if (success) | |
583 request.send(); | |
584 }); | |
585 } | 578 } |
586 | 579 |
587 /** | 580 /** |
588 * Requests the account opted-in state from the server. | 581 * Requests the account opted-in state from the server. |
589 * @param {function()} optedInCallback Function that will be called if | 582 * @param {function()} optedInCallback Function that will be called if |
590 * opted-in state is 'true'. | 583 * opted-in state is 'true'. |
591 */ | 584 */ |
592 function requestOptedIn(optedInCallback) { | 585 function requestOptedIn(optedInCallback) { |
593 console.log('requestOptedIn from ' + NOTIFICATION_CARDS_URL); | 586 console.log('requestOptedIn from ' + NOTIFICATION_CARDS_URL); |
594 | 587 |
595 var request = buildServerRequest('GET', 'settings/optin'); | 588 requestFromServer('GET', 'settings/optin').then(function(request) { |
596 | |
597 request.onloadend = function(event) { | |
598 console.log( | 589 console.log( |
599 'requestOptedIn-onloadend ' + request.status + ' ' + request.response); | 590 'requestOptedIn-received ' + request.status + ' ' + request.response); |
600 if (request.status == HTTP_OK) { | 591 if (request.status == HTTP_OK) { |
601 var parsedResponse = JSON.parse(request.responseText); | 592 var parsedResponse = JSON.parse(request.responseText); |
602 if (parsedResponse.value) { | 593 if (parsedResponse.value) { |
603 chrome.storage.local.set({googleNowEnabled: true}); | 594 chrome.storage.local.set({googleNowEnabled: true}); |
604 optedInCallback(); | 595 optedInCallback(); |
605 // Google Now was disabled, now it's enabled. This is a state change. | 596 // Google Now was disabled, now it's enabled. This is a state change. |
606 onStateChange(); | 597 onStateChange(); |
607 } else { | 598 } else { |
608 scheduleNextPoll({}, false); | 599 scheduleNextPoll({}, false); |
609 } | 600 } |
610 } | 601 } |
611 }; | |
612 | |
613 setAuthorization(request, function(success) { | |
614 if (success) | |
615 request.send(); | |
616 }); | 602 }); |
617 } | 603 } |
618 | 604 |
619 /** | 605 /** |
620 * Requests notification cards from the server. | 606 * Requests notification cards from the server. |
621 */ | 607 */ |
622 function requestNotificationCards() { | 608 function requestNotificationCards() { |
623 console.log('requestNotificationCards'); | 609 console.log('requestNotificationCards'); |
624 | 610 |
625 fillFromChromeLocalStorage({ | 611 fillFromChromeLocalStorage({ |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
704 var requestParameters = 'notifications/' + dismissalData.notificationId + | 690 var requestParameters = 'notifications/' + dismissalData.notificationId + |
705 '?age=' + dismissalAge + | 691 '?age=' + dismissalAge + |
706 '&chromeNotificationId=' + chromeNotificationId; | 692 '&chromeNotificationId=' + chromeNotificationId; |
707 | 693 |
708 for (var paramField in dismissalData.parameters) | 694 for (var paramField in dismissalData.parameters) |
709 requestParameters += ('&' + paramField + | 695 requestParameters += ('&' + paramField + |
710 '=' + dismissalData.parameters[paramField]); | 696 '=' + dismissalData.parameters[paramField]); |
711 | 697 |
712 console.log('requestCardDismissal: requestParameters=' + requestParameters); | 698 console.log('requestCardDismissal: requestParameters=' + requestParameters); |
713 | 699 |
714 var request = buildServerRequest('DELETE', requestParameters); | 700 requestFromServer('DELETE', requestParameters).then(function(request) { |
715 request.onloadend = function(event) { | |
716 console.log('requestDismissingCard-onloadend ' + request.status); | 701 console.log('requestDismissingCard-onloadend ' + request.status); |
717 if (request.status == HTTP_NOCONTENT) | 702 if (request.status == HTTP_NOCONTENT) |
718 recordEvent(GoogleNowEvent.DISMISS_REQUEST_SUCCESS); | 703 recordEvent(GoogleNowEvent.DISMISS_REQUEST_SUCCESS); |
719 | 704 |
720 // A dismissal doesn't require further retries if it was successful or | 705 // A dismissal doesn't require further retries if it was successful or |
721 // doesn't have a chance for successful completion. | 706 // doesn't have a chance for successful completion. |
722 var done = request.status == HTTP_NOCONTENT || | 707 var done = request.status == HTTP_NOCONTENT || |
723 request.status == HTTP_BAD_REQUEST || | 708 request.status == HTTP_BAD_REQUEST || |
724 request.status == HTTP_METHOD_NOT_ALLOWED; | 709 request.status == HTTP_METHOD_NOT_ALLOWED; |
725 callbackBoolean(done); | 710 callbackBoolean(done); |
726 }; | 711 }).catch(function() { |
727 | 712 callbackBoolean(false); |
728 setAuthorization(request, function(success) { | |
729 if (success) | |
730 request.send(); | |
731 else | |
732 callbackBoolean(false); | |
733 }); | 713 }); |
734 } | 714 } |
735 | 715 |
736 /** | 716 /** |
737 * Tries to send dismiss requests for all pending dismissals. | 717 * Tries to send dismiss requests for all pending dismissals. |
738 * @param {function(boolean)} callbackBoolean Completion callback with 'success' | 718 * @param {function(boolean)} callbackBoolean Completion callback with 'success' |
739 * parameter. Success means that no pending dismissals are left. | 719 * parameter. Success means that no pending dismissals are left. |
740 */ | 720 */ |
741 function processPendingDismissals(callbackBoolean) { | 721 function processPendingDismissals(callbackBoolean) { |
742 fillFromChromeLocalStorage({ | 722 fillFromChromeLocalStorage({ |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1164 lastPollNowPayloads: items.lastPollNowPayloads, | 1144 lastPollNowPayloads: items.lastPollNowPayloads, |
1165 notificationGroups: items.notificationGroups | 1145 notificationGroups: items.notificationGroups |
1166 }); | 1146 }); |
1167 | 1147 |
1168 requestCards(); | 1148 requestCards(); |
1169 } | 1149 } |
1170 }); | 1150 }); |
1171 }); | 1151 }); |
1172 } | 1152 } |
1173 }); | 1153 }); |
OLD | NEW |