Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Android Chromoting: Don't fetch auth token if there's a fetch pending.
Browse files Browse the repository at this point in the history
Currently, the host list is fetched from the main activity's onStart()
handler. If onStart() is triggered again during the process of
authentication, this could cause an infinite loop of pending requests.

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

Cr-Commit-Position: refs/heads/master@{#297088}
  • Loading branch information
lambroslambrou authored and Commit bot committed Sep 27, 2014
1 parent 3c37dba commit f1318ec
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public class Chromoting extends Activity implements JniInterface.ConnectionListe
*/
boolean mTriedNewAuthToken;

/**
* Flag to track whether a call to AccountManager.getAuthToken() is currently pending.
* This avoids infinitely-nested calls in case onStart() gets triggered a second time
* while a token is being fetched.
*/
private boolean mWaitingForAuthToken = false;

/** Shows a warning explaining that a Google account is required, then closes the activity. */
private void showNoAccountsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Expand Down Expand Up @@ -292,16 +299,27 @@ public void onCancel(DialogInterface dialog) {
}

private void refreshHostList() {
if (mWaitingForAuthToken) {
return;
}

mTriedNewAuthToken = false;
setHostListProgressVisible(true);

// The refresh button simply makes use of the currently-chosen account.
requestAuthToken();
}

private void requestAuthToken() {
AccountManager.get(this).getAuthToken(mAccount, TOKEN_SCOPE, null, this, this, null);
mWaitingForAuthToken = true;
}

@Override
public void run(AccountManagerFuture<Bundle> future) {
Log.i("auth", "User finished with auth dialogs");
mWaitingForAuthToken = false;

Bundle result = null;
String explanation = null;
try {
Expand All @@ -316,6 +334,7 @@ public void run(AccountManagerFuture<Bundle> future) {
}

if (result == null) {
setHostListProgressVisible(false);
if (explanation != null) {
Toast.makeText(this, explanation, Toast.LENGTH_LONG).show();
}
Expand Down Expand Up @@ -387,7 +406,7 @@ public void onError(HostListLoader.Error error) {
Log.w("auth", "Requesting renewal of rejected auth token");
authenticator.invalidateAuthToken(mAccount.type, mToken);
mToken = null;
authenticator.getAuthToken(mAccount, TOKEN_SCOPE, null, this, this, null);
requestAuthToken();

// We're not in an error state *yet*.
return;
Expand Down

0 comments on commit f1318ec

Please sign in to comment.