| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.AsyncTask; | 8 import android.os.AsyncTask; |
| 9 import android.os.Handler; |
| 10 import android.os.Looper; |
| 9 | 11 |
| 10 import com.google.android.gms.auth.GoogleAuthException; | 12 import com.google.android.gms.auth.GoogleAuthException; |
| 11 import com.google.android.gms.auth.GoogleAuthUtil; | 13 import com.google.android.gms.auth.GoogleAuthUtil; |
| 12 | 14 |
| 13 import org.chromium.chromoting.base.OAuthTokenFetcher; | 15 import org.chromium.chromoting.base.OAuthTokenFetcher; |
| 14 | 16 |
| 15 import java.io.IOException; | 17 import java.io.IOException; |
| 16 | 18 |
| 17 /** | 19 /** |
| 18 * This helper guards same auth token requesting task shouldn't be run more than
once at the same | 20 * This helper guards same auth token requesting task shouldn't be run more than
once at the same |
| (...skipping 11 matching lines...) Expand all Loading... |
| 30 * @param tokenScope Scope to use when fetching the OAuth token. | 32 * @param tokenScope Scope to use when fetching the OAuth token. |
| 31 */ | 33 */ |
| 32 public OAuthTokenConsumer(Context context, String tokenScope) { | 34 public OAuthTokenConsumer(Context context, String tokenScope) { |
| 33 mContext = context; | 35 mContext = context; |
| 34 mTokenScope = tokenScope; | 36 mTokenScope = tokenScope; |
| 35 mWaitingForAuthToken = false; | 37 mWaitingForAuthToken = false; |
| 36 } | 38 } |
| 37 | 39 |
| 38 /** | 40 /** |
| 39 * Retrieves the auth token and call the callback when it is done. callback.
onTokenFetched() | 41 * Retrieves the auth token and call the callback when it is done. callback.
onTokenFetched() |
| 40 * will be called if the retrieval succeeds, otherwise callback.onError() wi
ll be called. | 42 * will be called on the main thread if the retrieval succeeds, otherwise ca
llback.onError() |
| 43 * will be called. |
| 41 * The callback will not be run if the task is already running and false wil
l be returned in | 44 * The callback will not be run if the task is already running and false wil
l be returned in |
| 42 * that case. | 45 * that case. |
| 43 * Each OAuthTokenConsumer is supposed to work for one specific task. It is
the caller's | 46 * Each OAuthTokenConsumer is supposed to work for one specific task. It is
the caller's |
| 44 * responsibility to supply equivalent callbacks (variables being captured c
an vary) for the | 47 * responsibility to supply equivalent callbacks (variables being captured c
an vary) for the |
| 45 * same consumer. | 48 * same consumer. |
| 46 * If user recoverable exception occurs and |context| is an activity, |callb
ack| will not be | 49 * If user recoverable exception occurs and |context| is an activity, |callb
ack| will not be |
| 47 * run and onActivityResult() of the activity will be called instead. | 50 * run and onActivityResult() of the activity will be called instead. |
| 48 * @param account User's account name (email). | 51 * @param account User's account name (email). |
| 49 * @param callback the callback to be called | 52 * @param callback the callback to be called |
| 50 * @return true if the consumer will run |callback| when the token is fetche
d and false | 53 * @return true if the consumer will run |callback| when the token is fetche
d and false |
| (...skipping 28 matching lines...) Expand all Loading... |
| 79 * @return The latest auth token fetched by calling consume(). This should b
e used right after | 82 * @return The latest auth token fetched by calling consume(). This should b
e used right after |
| 80 * the callback passed to consume() is run. The token may become in
valid after some | 83 * the callback passed to consume() is run. The token may become in
valid after some |
| 81 * amount of time. | 84 * amount of time. |
| 82 */ | 85 */ |
| 83 public String getLatestToken() { | 86 public String getLatestToken() { |
| 84 return mLatestToken; | 87 return mLatestToken; |
| 85 } | 88 } |
| 86 | 89 |
| 87 /** | 90 /** |
| 88 * Revokes the latest token fetched by the consumer. | 91 * Revokes the latest token fetched by the consumer. |
| 89 * @param callback onTokenFetch(null) will be called if the token is cleared
successfully. | 92 * @param callback onTokenFetch(null) will be called on the main thread if t
he token is cleared |
| 90 * onError(error) will be called if any error occurs. |callb
ack| can be null, | 93 * successfully. onError(error) will be called if any error
occurs. |callback| |
| 91 * in which case token will be cleared without running the c
allback. | 94 * can be null, in which case token will be cleared without
running the |
| 95 * callback. |
| 92 */ | 96 */ |
| 93 public void revokeLatestToken(final OAuthTokenFetcher.Callback callback) { | 97 public void revokeLatestToken(final OAuthTokenFetcher.Callback callback) { |
| 94 new AsyncTask<Void, Void, Void>() { | 98 new AsyncTask<Void, Void, Void>() { |
| 95 @Override | 99 @Override |
| 96 protected Void doInBackground(Void... params) { | 100 protected Void doInBackground(Void... params) { |
| 97 try { | 101 try { |
| 98 GoogleAuthUtil.clearToken(mContext, mLatestToken); | 102 GoogleAuthUtil.clearToken(mContext, mLatestToken); |
| 99 mLatestToken = null; | 103 mLatestToken = null; |
| 100 if (callback != null) { | 104 if (callback != null) { |
| 101 callback.onTokenFetched(null); | 105 new Handler(Looper.getMainLooper()).post(new Runnable()
{ |
| 106 @Override |
| 107 public void run() { |
| 108 callback.onTokenFetched(null); |
| 109 } |
| 110 }); |
| 102 } | 111 } |
| 103 } catch (GoogleAuthException e) { | 112 } catch (GoogleAuthException e) { |
| 104 if (callback != null) { | 113 if (callback != null) { |
| 114 handleErrorOnMainThread(callback, OAuthTokenFetcher.Erro
r.UNEXPECTED); |
| 105 callback.onError(OAuthTokenFetcher.Error.UNEXPECTED); | 115 callback.onError(OAuthTokenFetcher.Error.UNEXPECTED); |
| 106 } | 116 } |
| 107 } catch (IOException e) { | 117 } catch (IOException e) { |
| 108 if (callback != null) { | 118 if (callback != null) { |
| 109 callback.onError(OAuthTokenFetcher.Error.NETWORK); | 119 handleErrorOnMainThread(callback, OAuthTokenFetcher.Erro
r.NETWORK); |
| 110 } | 120 } |
| 111 } | 121 } |
| 112 return null; | 122 return null; |
| 113 } | 123 } |
| 114 }.execute(); | 124 }.execute(); |
| 115 } | 125 } |
| 126 |
| 127 private void handleErrorOnMainThread(final OAuthTokenFetcher.Callback callba
ck, |
| 128 final OAuthTokenFetcher.Error error) { |
| 129 new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 130 @Override |
| 131 public void run() { |
| 132 callback.onError(error); |
| 133 } |
| 134 }); |
| 135 } |
| 116 } | 136 } |
| OLD | NEW |