Skip to content

Commit

Permalink
[Sync] Add tokenUnavailable to AccountManagerHelper.
Browse files Browse the repository at this point in the history
It's rather misleading that tokenAvailable can be called with a null
token. I think this change makes it a clearer API.

BUG=488563

Review URL: https://codereview.chromium.org/1342313002

Cr-Commit-Position: refs/heads/master@{#349783}
  • Loading branch information
maxbogue authored and Commit bot committed Sep 18, 2015
1 parent 398715a commit c94c53a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ public void deliverMessage(final String to, final Bundle data) {
SyncConstants.CHROME_SYNC_OAUTH2_SCOPE,
new AccountManagerHelper.GetAuthTokenCallback() {
@Override
public void tokenAvailable(String token, boolean isTransientError) {
public void tokenAvailable(String token) {
sendUpstreamMessage(to, data, token);
}

@Override
public void tokenUnavailable(boolean isTransientError) {
GcmUpstreamUma.recordHistogram(
getApplicationContext(), GcmUpstreamUma.UMA_TOKEN_REQUEST_FAILED);
sendUpstreamMessage(to, data, null);
}
});
}

private void sendUpstreamMessage(String to, Bundle data, String token) {
if (token == null) {
GcmUpstreamUma.recordHistogram(
getApplicationContext(), GcmUpstreamUma.UMA_TOKEN_REQUEST_FAILED);
}
// Add the OAuth2 token to the bundle. The token should have the prefix Bearer added to it.
data.putString("Authorization", "Bearer " + token);
if (!isMessageWithinLimit(data)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,13 @@ public void run() {
accountManagerHelper.getAuthToken(
account, oauth2Scope, new AccountManagerHelper.GetAuthTokenCallback() {
@Override
public void tokenAvailable(String token, boolean isTransientError) {
nativeOAuth2TokenFetched(token, isTransientError, nativeCallback);
public void tokenAvailable(String token) {
nativeOAuth2TokenFetched(token, false, nativeCallback);
}

@Override
public void tokenUnavailable(boolean isTransientError) {
nativeOAuth2TokenFetched(null, isTransientError, nativeCallback);
}
});
}
Expand Down Expand Up @@ -194,10 +199,16 @@ public static String getOAuth2AccessTokenWithTimeout(
getOAuth2AccessToken(
context, activity, account, scope, new AccountManagerHelper.GetAuthTokenCallback() {
@Override
public void tokenAvailable(String token, boolean isTransientError) {
public void tokenAvailable(String token) {
result.set(token);
semaphore.release();
}

@Override
public void tokenUnavailable(boolean isTransientError) {
result.set(null);
semaphore.release();
}
});
try {
if (semaphore.tryAcquire(timeout, unit)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,13 @@ public void requestAuthToken(final PendingIntent pendingIntent,
AccountManagerHelper.get(this).getNewAuthToken(account, invalidAuthToken,
getOAuth2ScopeWithType(), new AccountManagerHelper.GetAuthTokenCallback() {
@Override
public void tokenAvailable(String token, boolean isTransientError) {
if (token != null) {
setAuthToken(InvalidationClientService.this.getApplicationContext(),
pendingIntent, token, getOAuth2ScopeWithType());
}
public void tokenAvailable(String token) {
setAuthToken(InvalidationClientService.this.getApplicationContext(),
pendingIntent, token, getOAuth2ScopeWithType());
}

@Override
public void tokenUnavailable(boolean isTransientError) {}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,19 @@ public class AccountManagerHelper {
*/
public interface GetAuthTokenCallback {
/**
* Invoked on the UI thread once a token has been provided by the AccountManager.
* @param token Auth token, or null if no token is available (bad credentials,
* permission denied, etc).
* @param isTransientError If the token is null, then this parameter indicates
* if the error is transient or persistent. If token is non-null, this
* parameter is not used.
* Invoked on the UI thread if a token is provided by the AccountManager.
*
* @param token Auth token, guaranteed not to be null.
*/
void tokenAvailable(String token, boolean isTransientError);
void tokenAvailable(String token);

/**
* Invoked on the UI thread if no token is available.
*
* @param isTransientError Indicates if the error is transient (network timeout or
* unavailable, etc) or persistent (bad credentials, permission denied, etc).
*/
void tokenUnavailable(boolean isTransientError);
}

/**
Expand Down Expand Up @@ -307,7 +312,7 @@ private void getAuthTokenAsynchronously(final Account account, final String auth
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
callback.tokenAvailable(null, false);
callback.tokenUnavailable(false);
}
});
return;
Expand All @@ -332,10 +337,13 @@ public void onPostExecute(String authToken) {
private void onGotAuthTokenResult(Account account, String authTokenType, String authToken,
GetAuthTokenCallback callback, AtomicInteger numTries, AtomicBoolean isTransientError,
ConnectionRetry retry) {
if (authToken != null || !isTransientError.get()
if (authToken != null) {
callback.tokenAvailable(authToken);
return;
} else if (!isTransientError.get()
|| numTries.incrementAndGet() == MAX_TRIES
|| !NetworkChangeNotifier.isInitialized()) {
callback.tokenAvailable(authToken, isTransientError.get());
callback.tokenUnavailable(isTransientError.get());
return;
}
if (retry == null) {
Expand Down

0 comments on commit c94c53a

Please sign in to comment.