| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chromoting; | |
| 6 | |
| 7 import android.app.Activity; | |
| 8 import android.os.AsyncTask; | |
| 9 | |
| 10 import com.google.android.gms.auth.GoogleAuthException; | |
| 11 import com.google.android.gms.auth.GoogleAuthUtil; | |
| 12 import com.google.android.gms.auth.UserRecoverableAuthException; | |
| 13 | |
| 14 import java.io.IOException; | |
| 15 | |
| 16 /** | |
| 17 * This helper class fetches an OAuth token on a separate thread, and properly h
andles the various | |
| 18 * error-conditions that can occur (such as, starting an Activity to prompt user
for input). | |
| 19 */ | |
| 20 public class OAuthTokenFetcher { | |
| 21 /** | |
| 22 * Callback interface to receive the token, or an error notification. These
will be called | |
| 23 * on the application's main thread. Note that if a user-recoverable error o
ccurs, neither of | |
| 24 * these callback will be triggered. Instead, a new Activity will be launche
d, and the calling | |
| 25 * Activity must override | |
| 26 * {@link android.app.Activity#onActivityResult} and handle the result code | |
| 27 * {@link REQUEST_CODE_RECOVER_FROM_OAUTH_ERROR} to re-attempt or cancel fet
ching the token. | |
| 28 */ | |
| 29 public interface Callback { | |
| 30 /** Called when a token is obtained. */ | |
| 31 void onTokenFetched(String token); | |
| 32 | |
| 33 /** | |
| 34 * Called if an unrecoverable error prevents fetching a token. | |
| 35 * @param errorResource String resource of error-message to be displayed
. | |
| 36 */ | |
| 37 void onError(int errorResource); | |
| 38 } | |
| 39 | |
| 40 /** Request code used for starting the OAuth recovery activity. */ | |
| 41 public static final int REQUEST_CODE_RECOVER_FROM_OAUTH_ERROR = 100; | |
| 42 | |
| 43 /** Scopes at which the authentication token we request will be valid. */ | |
| 44 private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com
/auth/chromoting " | |
| 45 + "https://www.googleapis.com/auth/googletalk"; | |
| 46 | |
| 47 /** | |
| 48 * Reference to the main activity. Used for running tasks on the main thread
, and for | |
| 49 * starting other activities to handle user-recoverable errors. | |
| 50 */ | |
| 51 private Activity mActivity; | |
| 52 | |
| 53 /** Account name (e-mail) for which the token will be fetched. */ | |
| 54 private String mAccountName; | |
| 55 | |
| 56 private Callback mCallback; | |
| 57 | |
| 58 public OAuthTokenFetcher(Activity activity, String accountName, Callback cal
lback) { | |
| 59 mActivity = activity; | |
| 60 mAccountName = accountName; | |
| 61 mCallback = callback; | |
| 62 } | |
| 63 | |
| 64 /** Begins fetching a token. Should be called on the main thread. */ | |
| 65 public void fetch() { | |
| 66 fetchImpl(null); | |
| 67 } | |
| 68 | |
| 69 /** | |
| 70 * Begins fetching a token, clearing an existing token from the cache. Shoul
d be called on the | |
| 71 * main thread. | |
| 72 * @param expiredToken A previously-fetched token which has expired. | |
| 73 */ | |
| 74 public void clearAndFetch(String expiredToken) { | |
| 75 fetchImpl(expiredToken); | |
| 76 } | |
| 77 | |
| 78 private void fetchImpl(final String expiredToken) { | |
| 79 new AsyncTask<Void, Void, Void>() { | |
| 80 @Override | |
| 81 protected Void doInBackground(Void... params) { | |
| 82 try { | |
| 83 if (expiredToken != null) { | |
| 84 GoogleAuthUtil.clearToken(mActivity, expiredToken); | |
| 85 } | |
| 86 | |
| 87 // This method is deprecated but its replacement is not yet
available. | |
| 88 // TODO(lambroslambrou): Fix this by replacing |mAccountName
| with an instance | |
| 89 // of android.accounts.Account. | |
| 90 String token = GoogleAuthUtil.getToken(mActivity, mAccountNa
me, TOKEN_SCOPE); | |
| 91 handleTokenReceived(token); | |
| 92 } catch (IOException ioException) { | |
| 93 handleError(R.string.error_network_error); | |
| 94 } catch (UserRecoverableAuthException recoverableException) { | |
| 95 handleRecoverableException(recoverableException); | |
| 96 } catch (GoogleAuthException fatalException) { | |
| 97 handleError(R.string.error_unexpected); | |
| 98 } | |
| 99 return null; | |
| 100 } | |
| 101 }.execute(); | |
| 102 } | |
| 103 | |
| 104 private void handleTokenReceived(final String token) { | |
| 105 mActivity.runOnUiThread(new Runnable() { | |
| 106 @Override | |
| 107 public void run() { | |
| 108 mCallback.onTokenFetched(token); | |
| 109 } | |
| 110 }); | |
| 111 } | |
| 112 | |
| 113 private void handleError(final int error) { | |
| 114 mActivity.runOnUiThread(new Runnable() { | |
| 115 @Override | |
| 116 public void run() { | |
| 117 mCallback.onError(error); | |
| 118 } | |
| 119 }); | |
| 120 } | |
| 121 | |
| 122 private void handleRecoverableException(final UserRecoverableAuthException e
xception) { | |
| 123 mActivity.runOnUiThread(new Runnable() { | |
| 124 @Override | |
| 125 public void run() { | |
| 126 mActivity.startActivityForResult(exception.getIntent(), | |
| 127 REQUEST_CODE_RECOVER_FROM_OAUTH_ERROR); | |
| 128 } | |
| 129 }); | |
| 130 } | |
| 131 } | |
| OLD | NEW |