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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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).then(function() { |
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 783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1083 setShouldPollCards(shouldPollCards); | 1080 setShouldPollCards(shouldPollCards); |
1084 } | 1081 } |
1085 | 1082 |
1086 /** | 1083 /** |
1087 * Coordinates the behavior of Google Now for Chrome depending on | 1084 * Coordinates the behavior of Google Now for Chrome depending on |
1088 * Chrome and extension state. | 1085 * Chrome and extension state. |
1089 */ | 1086 */ |
1090 function onStateChange() { | 1087 function onStateChange() { |
1091 tasks.add(STATE_CHANGED_TASK_NAME, function() { | 1088 tasks.add(STATE_CHANGED_TASK_NAME, function() { |
1092 Promise.all([ | 1089 Promise.all([ |
1093 isSignedIn(), | 1090 authenticationManager.isSignedIn(), |
1094 isGeolocationEnabled(), | 1091 isGeolocationEnabled(), |
1095 canEnableBackground(), | 1092 canEnableBackground(), |
1096 isNotificationsEnabled(), | 1093 isNotificationsEnabled(), |
1097 isGoogleNowEnabled()]) | 1094 isGoogleNowEnabled()]) |
1098 .then(function(results) { | 1095 .then(function(results) { |
1099 updateRunningState.apply(null, results); | 1096 updateRunningState.apply(null, results); |
1100 }); | 1097 }); |
1101 }); | 1098 }); |
1102 } | 1099 } |
1103 | 1100 |
1104 /** | 1101 /** |
1105 * Determines if the user is signed in. | |
1106 * @return {Promise} A promise to evaluate the signed in state. | |
1107 */ | |
1108 function isSignedIn() { | |
1109 return new Promise(function(resolve) { | |
1110 authenticationManager.isSignedIn(function(signedIn) { | |
1111 resolve(signedIn); | |
1112 }); | |
1113 }); | |
1114 } | |
1115 | |
1116 /** | |
1117 * Gets the geolocation enabled preference. | 1102 * Gets the geolocation enabled preference. |
1118 * @return {Promise} A promise to get the geolocation enabled preference. | 1103 * @return {Promise} A promise to get the geolocation enabled preference. |
1119 */ | 1104 */ |
1120 function isGeolocationEnabled() { | 1105 function isGeolocationEnabled() { |
1121 return new Promise(function(resolve) { | 1106 return new Promise(function(resolve) { |
1122 instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get( | 1107 instrumented.preferencesPrivate.googleGeolocationAccessEnabled.get( |
1123 {}, | 1108 {}, |
1124 function(prefValue) { | 1109 function(prefValue) { |
1125 resolve(!!prefValue.value); | 1110 resolve(!!prefValue.value); |
1126 }); | 1111 }); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1280 lastPollNowPayloads: items.lastPollNowPayloads, | 1265 lastPollNowPayloads: items.lastPollNowPayloads, |
1281 notificationGroups: items.notificationGroups | 1266 notificationGroups: items.notificationGroups |
1282 }); | 1267 }); |
1283 | 1268 |
1284 updateNotificationsCards(); | 1269 updateNotificationsCards(); |
1285 } | 1270 } |
1286 }); | 1271 }); |
1287 }); | 1272 }); |
1288 } | 1273 } |
1289 }); | 1274 }); |
OLD | NEW |