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

Unified Diff: chrome/browser/resources/google_now/background.js

Issue 187263002: Refactor Authenticated Server Requests to use Promises (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Clarify the scoping Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/utility.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/google_now/background.js
diff --git a/chrome/browser/resources/google_now/background.js b/chrome/browser/resources/google_now/background.js
index 93ea540d0590f2a6e8179a7ecb8bec180ad2b070..182dd7c95447860716dea7e428f8c748f9614f72 100644
--- a/chrome/browser/resources/google_now/background.js
+++ b/chrome/browser/resources/google_now/background.js
@@ -248,31 +248,38 @@ function recordEvent(event) {
}
/**
- * Adds authorization behavior to the request.
- * @param {XMLHttpRequest} request Server request.
- * @param {function(boolean)} callbackBoolean Completion callback with 'success'
- * parameter.
+ * Checks the result of the HTTP Request and updates the authentication
+ * manager on any failure.
+ * @param {XMLHttpRequest} request XMLHTTPRequest that sent the authenticated
rgustafson 2014/03/12 21:26:28 Nitpicky: match capitalization on the second usage
robliao 2014/03/12 22:21:32 Done.
+ * request.
*/
-function setAuthorization(request, callbackBoolean) {
- authenticationManager.getAuthToken().then(function(token) {
- request.setRequestHeader('Authorization', 'Bearer ' + token);
+function checkAuthenticationStatus(request) {
+ if (request.status == HTTP_FORBIDDEN ||
+ request.status == HTTP_UNAUTHORIZED) {
+ authenticationManager.removeToken(token);
+ }
+}
- // Instrument onloadend to remove stale auth tokens.
- var originalOnLoadEnd = request.onloadend;
- request.onloadend = wrapper.wrapCallback(function(event) {
- if (request.status == HTTP_FORBIDDEN ||
- request.status == HTTP_UNAUTHORIZED) {
- authenticationManager.removeToken(token).then(function() {
- originalOnLoadEnd(event);
- });
- } else {
- originalOnLoadEnd(event);
- }
+/**
+ * Builds and sends an authenticated request to the notification server.
+ * @param {string} method Request method.
+ * @param {string} handlerName Server handler to send the request to.
+ * @param {string=} opt_contentType Value for the Content-type header.
+ * @return {Promise} A promise to issue a request to the server.
+ * The promise rejects if there is a client-side authentication issue.
+ */
+function requestFromServer(method, handlerName, opt_contentType) {
+ return authenticationManager.getAuthToken().then(function(token) {
+ var request = buildServerRequest(method, handlerName, opt_contentType);
+ request.setRequestHeader('Authorization', 'Bearer ' + token);
+ var requestPromise = new Promise(function(resolve) {
+ request.addEventListener('loadend', function() {
+ resolve(request);
+ }, false);
+ request.send();
});
-
- callbackBoolean(true);
- }).catch(function() {
- callbackBoolean(false);
+ requestPromise.then(checkAuthenticationStatus);
+ return requestPromise;
});
}
@@ -567,21 +574,15 @@ function requestNotificationGroups(groupNames) {
console.log('requestNotificationGroups: request=' + requestParameters);
- var request = buildServerRequest('GET', 'notifications' + requestParameters);
-
- request.onloadend = function(event) {
- console.log('requestNotificationGroups-onloadend ' + request.status);
- if (request.status == HTTP_OK) {
- recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS);
- processServerResponse(
- JSON.parse(request.responseText), cardShownCallback);
- }
- };
-
- setAuthorization(request, function(success) {
- if (success)
- request.send();
- });
+ requestFromServer('GET', 'notifications' + requestParameters).then(
+ function(request) {
+ console.log('requestNotificationGroups-received ' + request.status);
+ if (request.status == HTTP_OK) {
+ recordEvent(GoogleNowEvent.REQUEST_FOR_CARDS_SUCCESS);
+ processServerResponse(
+ JSON.parse(request.responseText), cardShownCallback);
+ }
+ });
}
/**
@@ -592,11 +593,9 @@ function requestNotificationGroups(groupNames) {
function requestOptedIn(optedInCallback) {
console.log('requestOptedIn from ' + NOTIFICATION_CARDS_URL);
- var request = buildServerRequest('GET', 'settings/optin');
-
- request.onloadend = function(event) {
+ requestFromServer('GET', 'settings/optin').then(function(request) {
console.log(
- 'requestOptedIn-onloadend ' + request.status + ' ' + request.response);
+ 'requestOptedIn-received ' + request.status + ' ' + request.response);
if (request.status == HTTP_OK) {
var parsedResponse = JSON.parse(request.responseText);
if (parsedResponse.value) {
@@ -608,11 +607,6 @@ function requestOptedIn(optedInCallback) {
scheduleNextPoll({}, false);
}
}
- };
-
- setAuthorization(request, function(success) {
- if (success)
- request.send();
});
}
@@ -711,8 +705,7 @@ function requestCardDismissal(
console.log('requestCardDismissal: requestParameters=' + requestParameters);
- var request = buildServerRequest('DELETE', requestParameters);
- request.onloadend = function(event) {
+ requestFromServer('DELETE', requestParameters).then(function(request) {
console.log('requestDismissingCard-onloadend ' + request.status);
if (request.status == HTTP_NOCONTENT)
recordEvent(GoogleNowEvent.DISMISS_REQUEST_SUCCESS);
@@ -723,13 +716,8 @@ function requestCardDismissal(
request.status == HTTP_BAD_REQUEST ||
request.status == HTTP_METHOD_NOT_ALLOWED;
callbackBoolean(done);
- };
-
- setAuthorization(request, function(success) {
- if (success)
- request.send();
- else
- callbackBoolean(false);
+ }).catch(function() {
+ callbackBoolean(false);
});
}
« no previous file with comments | « no previous file | chrome/browser/resources/google_now/utility.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698