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

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

Issue 171713007: Revert of Convert Google Now's Authentication Manager to use Promises (https://codereview.chromium.… (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 6 years, 10 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
« no previous file with comments | « no previous file | trunk/src/chrome/browser/resources/google_now/background_unittest.gtestjs » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 chrome.metricsPrivate.recordValue(metricDescription, event); 258 chrome.metricsPrivate.recordValue(metricDescription, event);
259 } 259 }
260 260
261 /** 261 /**
262 * Adds authorization behavior to the request. 262 * Adds authorization behavior to the request.
263 * @param {XMLHttpRequest} request Server request. 263 * @param {XMLHttpRequest} request Server request.
264 * @param {function(boolean)} callbackBoolean Completion callback with 'success' 264 * @param {function(boolean)} callbackBoolean Completion callback with 'success'
265 * parameter. 265 * parameter.
266 */ 266 */
267 function setAuthorization(request, callbackBoolean) { 267 function setAuthorization(request, callbackBoolean) {
268 authenticationManager.getAuthToken().then(function(token) { 268 authenticationManager.getAuthToken(function(token) {
269 if (!token) {
270 callbackBoolean(false);
271 return;
272 }
273
269 request.setRequestHeader('Authorization', 'Bearer ' + token); 274 request.setRequestHeader('Authorization', 'Bearer ' + token);
270 275
271 // Instrument onloadend to remove stale auth tokens. 276 // Instrument onloadend to remove stale auth tokens.
272 var originalOnLoadEnd = request.onloadend; 277 var originalOnLoadEnd = request.onloadend;
273 request.onloadend = wrapper.wrapCallback(function(event) { 278 request.onloadend = wrapper.wrapCallback(function(event) {
274 if (request.status == HTTP_FORBIDDEN || 279 if (request.status == HTTP_FORBIDDEN ||
275 request.status == HTTP_UNAUTHORIZED) { 280 request.status == HTTP_UNAUTHORIZED) {
276 authenticationManager.removeToken(token).then(function() { 281 authenticationManager.removeToken(token, function() {
277 originalOnLoadEnd(event); 282 originalOnLoadEnd(event);
278 }); 283 });
279 } else { 284 } else {
280 originalOnLoadEnd(event); 285 originalOnLoadEnd(event);
281 } 286 }
282 }); 287 });
283 288
284 callbackBoolean(true); 289 callbackBoolean(true);
285 }).catch(function() {
286 callbackBoolean(false);
287 }); 290 });
288 } 291 }
289 292
290 /** 293 /**
291 * Shows parsed and combined cards as notifications. 294 * Shows parsed and combined cards as notifications.
292 * @param {Object.<string, StoredNotificationGroup>} notificationGroups Map from 295 * @param {Object.<string, StoredNotificationGroup>} notificationGroups Map from
293 * group name to group information. 296 * group name to group information.
294 * @param {Object.<ChromeNotificationId, CombinedCard>} cards Map from 297 * @param {Object.<ChromeNotificationId, CombinedCard>} cards Map from
295 * chromeNotificationId to the combined card, containing cards to show. 298 * chromeNotificationId to the combined card, containing cards to show.
296 * @param {function()} onSuccess Called on success. 299 * @param {function()} onSuccess Called on success.
(...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after
1078 setShouldPollCards(shouldPollCards); 1081 setShouldPollCards(shouldPollCards);
1079 } 1082 }
1080 1083
1081 /** 1084 /**
1082 * Coordinates the behavior of Google Now for Chrome depending on 1085 * Coordinates the behavior of Google Now for Chrome depending on
1083 * Chrome and extension state. 1086 * Chrome and extension state.
1084 */ 1087 */
1085 function onStateChange() { 1088 function onStateChange() {
1086 tasks.add(STATE_CHANGED_TASK_NAME, function() { 1089 tasks.add(STATE_CHANGED_TASK_NAME, function() {
1087 Promise.all([ 1090 Promise.all([
1088 authenticationManager.isSignedIn(), 1091 isSignedIn(),
1089 isGeolocationEnabled(), 1092 isGeolocationEnabled(),
1090 canEnableBackground(), 1093 canEnableBackground(),
1091 isNotificationsEnabled(), 1094 isNotificationsEnabled(),
1092 isGoogleNowEnabled()]) 1095 isGoogleNowEnabled()])
1093 .then(function(results) { 1096 .then(function(results) {
1094 updateRunningState.apply(null, results); 1097 updateRunningState.apply(null, results);
1095 }); 1098 });
1096 }); 1099 });
1097 } 1100 }
1098 1101
1099 /** 1102 /**
1103 * Determines if the user is signed in.
1104 * @return {Promise} A promise to evaluate the signed in state.
1105 */
1106 function isSignedIn() {
1107 return new Promise(function(resolve) {
1108 authenticationManager.isSignedIn(function(signedIn) {
1109 resolve(signedIn);
1110 });
1111 });
1112 }
1113
1114 /**
1100 * Gets the geolocation enabled preference. 1115 * Gets the geolocation enabled preference.
1101 * @return {Promise} A promise to get the geolocation enabled preference. 1116 * @return {Promise} A promise to get the geolocation enabled preference.
1102 */ 1117 */
1103 function isGeolocationEnabled() { 1118 function isGeolocationEnabled() {
1104 return new Promise(function(resolve) { 1119 return new Promise(function(resolve) {
1105 instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get( 1120 instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get(
1106 {}, 1121 {},
1107 function(prefValue) { 1122 function(prefValue) {
1108 resolve(!!prefValue.value); 1123 resolve(!!prefValue.value);
1109 }); 1124 });
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
1263 lastPollNowPayloads: items.lastPollNowPayloads, 1278 lastPollNowPayloads: items.lastPollNowPayloads,
1264 notificationGroups: items.notificationGroups 1279 notificationGroups: items.notificationGroups
1265 }); 1280 });
1266 1281
1267 updateNotificationsCards(); 1282 updateNotificationsCards();
1268 } 1283 }
1269 }); 1284 });
1270 }); 1285 });
1271 } 1286 }
1272 }); 1287 });
OLDNEW
« no previous file with comments | « no previous file | trunk/src/chrome/browser/resources/google_now/background_unittest.gtestjs » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698