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

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

Issue 162273002: Convert Google Now's Authentication Manager to use Promises (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
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
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(function(token) { 268 authenticationManager.getAuthToken().then(function(token) {
269 if (!token) {
270 callbackBoolean(false);
271 return;
272 }
273
274 request.setRequestHeader('Authorization', 'Bearer ' + token); 269 request.setRequestHeader('Authorization', 'Bearer ' + token);
275 270
276 // Instrument onloadend to remove stale auth tokens. 271 // Instrument onloadend to remove stale auth tokens.
277 var originalOnLoadEnd = request.onloadend; 272 var originalOnLoadEnd = request.onloadend;
278 request.onloadend = wrapper.wrapCallback(function(event) { 273 request.onloadend = wrapper.wrapCallback(function(event) {
279 if (request.status == HTTP_FORBIDDEN || 274 if (request.status == HTTP_FORBIDDEN ||
280 request.status == HTTP_UNAUTHORIZED) { 275 request.status == HTTP_UNAUTHORIZED) {
281 authenticationManager.removeToken(token, function() { 276 authenticationManager.removeToken(token, function() {
rgustafson 2014/02/13 21:31:40 No more callback.
robliao 2014/02/13 21:41:33 Done.
282 originalOnLoadEnd(event); 277 originalOnLoadEnd(event);
283 }); 278 });
284 } else { 279 } else {
285 originalOnLoadEnd(event); 280 originalOnLoadEnd(event);
286 } 281 }
287 }); 282 });
288 283
289 callbackBoolean(true); 284 callbackBoolean(true);
285 }).catch(function() {
286 callbackBoolean(false);
290 }); 287 });
291 } 288 }
292 289
293 /** 290 /**
294 * Shows parsed and combined cards as notifications. 291 * Shows parsed and combined cards as notifications.
295 * @param {Object.<string, StoredNotificationGroup>} notificationGroups Map from 292 * @param {Object.<string, StoredNotificationGroup>} notificationGroups Map from
296 * group name to group information. 293 * group name to group information.
297 * @param {Object.<ChromeNotificationId, CombinedCard>} cards Map from 294 * @param {Object.<ChromeNotificationId, CombinedCard>} cards Map from
298 * chromeNotificationId to the combined card, containing cards to show. 295 * chromeNotificationId to the combined card, containing cards to show.
299 * @param {function()} onSuccess Called on success. 296 * @param {function()} onSuccess Called on success.
(...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after
1081 setShouldPollCards(shouldPollCards); 1078 setShouldPollCards(shouldPollCards);
1082 } 1079 }
1083 1080
1084 /** 1081 /**
1085 * Coordinates the behavior of Google Now for Chrome depending on 1082 * Coordinates the behavior of Google Now for Chrome depending on
1086 * Chrome and extension state. 1083 * Chrome and extension state.
1087 */ 1084 */
1088 function onStateChange() { 1085 function onStateChange() {
1089 tasks.add(STATE_CHANGED_TASK_NAME, function() { 1086 tasks.add(STATE_CHANGED_TASK_NAME, function() {
1090 Promise.all([ 1087 Promise.all([
1091 isSignedIn(), 1088 authenticationManager.isSignedIn(),
1092 isGeolocationEnabled(), 1089 isGeolocationEnabled(),
1093 canEnableBackground(), 1090 canEnableBackground(),
1094 isNotificationsEnabled(), 1091 isNotificationsEnabled(),
1095 isGoogleNowEnabled()]) 1092 isGoogleNowEnabled()])
1096 .then(function(results) { 1093 .then(function(results) {
1097 updateRunningState.apply(null, results); 1094 updateRunningState.apply(null, results);
1098 }); 1095 });
1099 }); 1096 });
1100 } 1097 }
1101 1098
1102 /** 1099 /**
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 /**
1115 * Gets the geolocation enabled preference. 1100 * Gets the geolocation enabled preference.
1116 * @return {Promise} A promise to get the geolocation enabled preference. 1101 * @return {Promise} A promise to get the geolocation enabled preference.
1117 */ 1102 */
1118 function isGeolocationEnabled() { 1103 function isGeolocationEnabled() {
1119 return new Promise(function(resolve) { 1104 return new Promise(function(resolve) {
1120 instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get( 1105 instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get(
1121 {}, 1106 {},
1122 function(prefValue) { 1107 function(prefValue) {
1123 resolve(!!prefValue.value); 1108 resolve(!!prefValue.value);
1124 }); 1109 });
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
1278 lastPollNowPayloads: items.lastPollNowPayloads, 1263 lastPollNowPayloads: items.lastPollNowPayloads,
1279 notificationGroups: items.notificationGroups 1264 notificationGroups: items.notificationGroups
1280 }); 1265 });
1281 1266
1282 updateNotificationsCards(); 1267 updateNotificationsCards();
1283 } 1268 }
1284 }); 1269 });
1285 }); 1270 });
1286 } 1271 }
1287 }); 1272 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698