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

Unified Diff: remoting/webapp/crd/js/oauth2_api_impl.js

Issue 1003433002: Updated remoting.xhr API to use promises. Removed access to the native (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@spy-promise
Patch Set: Created 5 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
Index: remoting/webapp/crd/js/oauth2_api_impl.js
diff --git a/remoting/webapp/crd/js/oauth2_api_impl.js b/remoting/webapp/crd/js/oauth2_api_impl.js
index 26fd3da42dbbbc01c74316bd2041c69267e2ca51..de5d9df49e6c25165888c67f2c12705337d22686 100644
--- a/remoting/webapp/crd/js/oauth2_api_impl.js
+++ b/remoting/webapp/crd/js/oauth2_api_impl.js
@@ -48,13 +48,13 @@ remoting.OAuth2ApiImpl.prototype.interpretXhrStatus_ =
// Return AUTHENTICATION_FAILED by default, so that the user can try to
// recover from an unexpected failure by signing in again.
/** @type {!remoting.Error} */
- var error = remoting.Error.AUTHENTICATION_FAILED;
+ var error = new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED);
if (xhrStatus == 400 || xhrStatus == 401 || xhrStatus == 403) {
- error = remoting.Error.AUTHENTICATION_FAILED;
+ error = new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED);
} else if (xhrStatus == 502 || xhrStatus == 503) {
- error = remoting.Error.SERVICE_UNAVAILABLE;
+ error = new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE);
} else if (xhrStatus == 0) {
- error = remoting.Error.NETWORK_FAILURE;
+ error = new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE);
} else {
console.warn('Unexpected authentication response code: ' + xhrStatus);
}
@@ -75,37 +75,36 @@ remoting.OAuth2ApiImpl.prototype.interpretXhrStatus_ =
*/
remoting.OAuth2ApiImpl.prototype.refreshAccessToken = function(
onDone, onError, clientId, clientSecret, refreshToken) {
- /** @param {XMLHttpRequest} xhr */
- var onResponse = function(xhr) {
- if (xhr.status == 200) {
+ /** @param {!remoting.Xhr.Response} response */
+ var onResponse = function(response) {
+ if (response.status == 200) {
try {
// Don't use base.jsonParseSafe here unless you also include base.js,
// otherwise this won't work from the OAuth trampoline.
// TODO(jamiewalch): Fix this once we're no longer using the trampoline.
- var tokens = JSON.parse(xhr.responseText);
+ var tokens = JSON.parse(response.getText());
onDone(tokens['access_token'], tokens['expires_in']);
} catch (/** @type {Error} */ err) {
console.error('Invalid "token" response from server:', err);
- onError(remoting.Error.UNEXPECTED);
+ onError(remoting.Error.unexpected());
}
} else {
- console.error('Failed to refresh token. Status: ' + xhr.status +
- ' response: ' + xhr.responseText);
- onError(fromHttpStatus(xhr.status));
+ console.error('Failed to refresh token. Status: ' + response.status +
+ ' response: ' + response.getText());
+ onError(remoting.Error.fromHttpStatus(response.status));
}
};
- remoting.xhr.start({
+ new remoting.Xhr({
method: 'POST',
url: this.getOAuth2TokenEndpoint_(),
- onDone: onResponse,
formContent: {
'client_id': clientId,
'client_secret': clientSecret,
'refresh_token': refreshToken,
'grant_type': 'refresh_token'
}
- });
+ }).start().then(onResponse);
};
/**
@@ -124,31 +123,30 @@ remoting.OAuth2ApiImpl.prototype.refreshAccessToken = function(
*/
remoting.OAuth2ApiImpl.prototype.exchangeCodeForTokens = function(
onDone, onError, clientId, clientSecret, code, redirectUri) {
- /** @param {XMLHttpRequest} xhr */
- var onResponse = function(xhr) {
- if (xhr.status == 200) {
+ /** @param {!remoting.Xhr.Response} response */
+ var onResponse = function(response) {
+ if (response.status == 200) {
try {
// Don't use base.jsonParseSafe here unless you also include base.js,
// otherwise this won't work from the OAuth trampoline.
// TODO(jamiewalch): Fix this once we're no longer using the trampoline.
- var tokens = JSON.parse(xhr.responseText);
+ var tokens = JSON.parse(response.getText());
onDone(tokens['refresh_token'],
tokens['access_token'], tokens['expires_in']);
} catch (/** @type {Error} */ err) {
console.error('Invalid "token" response from server:', err);
- onError(remoting.Error.UNEXPECTED);
+ onError(remoting.Error.unexpected());
}
} else {
- console.error('Failed to exchange code for token. Status: ' + xhr.status +
- ' response: ' + xhr.responseText);
- onError(fromHttpStatus(xhr.status));
+ console.error('Failed to exchange code for token. Status: ' +
+ response.status + ' response: ' + response.getText());
+ onError(remoting.Error.fromHttpStatus(response.status));
}
};
- remoting.xhr.start({
+ new remoting.Xhr({
method: 'POST',
url: this.getOAuth2TokenEndpoint_(),
- onDone: onResponse,
formContent: {
'client_id': clientId,
'client_secret': clientSecret,
@@ -156,7 +154,7 @@ remoting.OAuth2ApiImpl.prototype.exchangeCodeForTokens = function(
'code': code,
'grant_type': 'authorization_code'
}
- });
+ }).start().then(onResponse);
};
/**
@@ -170,28 +168,27 @@ remoting.OAuth2ApiImpl.prototype.exchangeCodeForTokens = function(
* @return {void} Nothing.
*/
remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) {
- /** @param {XMLHttpRequest} xhr */
- var onResponse = function(xhr) {
- if (xhr.status == 200) {
+ /** @param {!remoting.Xhr.Response} response */
+ var onResponse = function(response) {
+ if (response.status == 200) {
try {
- var result = JSON.parse(xhr.responseText);
+ var result = JSON.parse(response.getText());
onDone(result['email']);
} catch (/** @type {Error} */ err) {
console.error('Invalid "userinfo" response from server:', err);
- onError(remoting.Error.UNEXPECTED);
+ onError(remoting.Error.unexpected());
}
} else {
- console.error('Failed to get email. Status: ' + xhr.status +
- ' response: ' + xhr.responseText);
- onError(fromHttpStatus(xhr.status));
+ console.error('Failed to get email. Status: ' + response.status +
+ ' response: ' + response.getText());
+ onError(remoting.Error.fromHttpStatus(response.status));
}
};
- remoting.xhr.start({
+ new remoting.Xhr({
method: 'GET',
url: this.getOAuth2ApiUserInfoEndpoint_(),
- onDone: onResponse,
oauthToken: token
- });
+ }).start().then(onResponse);
};
/**
@@ -206,37 +203,36 @@ remoting.OAuth2ApiImpl.prototype.getEmail = function(onDone, onError, token) {
*/
remoting.OAuth2ApiImpl.prototype.getUserInfo =
function(onDone, onError, token) {
- /** @param {XMLHttpRequest} xhr */
- var onResponse = function(xhr) {
- if (xhr.status == 200) {
+ /** @param {!remoting.Xhr.Response} response */
+ var onResponse = function(response) {
+ if (response.status == 200) {
try {
- var result = JSON.parse(xhr.responseText);
+ var result = JSON.parse(response.getText());
onDone(result['email'], result['name']);
} catch (/** @type {Error} */ err) {
console.error('Invalid "userinfo" response from server:', err);
- onError(remoting.Error.UNEXPECTED);
+ onError(remoting.Error.unexpected());
}
} else {
- console.error('Failed to get user info. Status: ' + xhr.status +
- ' response: ' + xhr.responseText);
- onError(fromHttpStatus(xhr.status));
+ console.error('Failed to get user info. Status: ' + response.status +
+ ' response: ' + response.getText());
+ onError(remoting.Error.fromHttpStatus(response.status));
}
};
- remoting.xhr.start({
+ new remoting.Xhr({
method: 'GET',
url: this.getOAuth2ApiUserInfoEndpoint_(),
- onDone: onResponse,
oauthToken: token
- });
+ }).start().then(onResponse);
};
/** @returns {!remoting.Error} */
function fromHttpStatus(/** number */ status) {
var error = remoting.Error.fromHttpStatus(status);
- if (error === remoting.Error.UNEXPECTED) {
+ if (error === remoting.Error.unexpected()) {
// Return AUTHENTICATION_FAILED by default, so that the user can try to
// recover from an unexpected failure by signing in again.
- return remoting.Error.AUTHENTICATION_FAILED;
+ return new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED);
}
return error;
}

Powered by Google App Engine
This is Rietveld 408576698