Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chromoting; | |
| 6 | |
| 7 import android.app.Activity; | |
| 8 | |
| 9 import org.chromium.chromoting.base.OAuthTokenFetcher; | |
| 10 | |
| 11 /** | |
| 12 * This helper guards same auth token requesting task shouldn't be run more than once at the same | |
| 13 * time. | |
| 14 */ | |
| 15 public class OAuthTokenConsumer { | |
| 16 private Activity mActivity; | |
| 17 private String mTokenScope; | |
| 18 private boolean mWaitingForAuthToken; | |
| 19 private String mLastToken; | |
|
Lambros
2016/05/13 01:11:31
mLatestToken or mNewestToken or mCurrentToken or j
Yuwei
2016/05/13 17:20:29
Done.
| |
| 20 | |
| 21 /** | |
| 22 * @param activity The Chromoting activity. | |
| 23 * @param tokenScope Scope to use when fetching the OAuth token. | |
| 24 */ | |
| 25 public OAuthTokenConsumer(Activity activity, String tokenScope) { | |
| 26 mActivity = activity; | |
| 27 mTokenScope = tokenScope; | |
| 28 mWaitingForAuthToken = false; | |
| 29 } | |
| 30 | |
| 31 /** | |
| 32 * Retrieves the auth token and call the callback when it is done. callback. onTokenFetched() | |
| 33 * will be called if the retrieval succeeds, otherwise callback.onError() wi ll be called. | |
| 34 * @param account User's account name (email). | |
| 35 * @param callback the callback to be called | |
| 36 * @return true if the same task has not been run and false if the task has already been | |
| 37 * running. | |
| 38 */ | |
| 39 public boolean consume(String account, final OAuthTokenFetcher.Callback call back) { | |
|
Lambros
2016/05/13 01:11:31
If consume() is called several times with differen
Yuwei
2016/05/13 03:53:58
Right. Only the first callback will get run.
Yuwei
2016/05/13 17:20:29
Added more details.
| |
| 40 if (mWaitingForAuthToken) { | |
| 41 return false; | |
| 42 } | |
| 43 mWaitingForAuthToken = true; | |
| 44 | |
| 45 OAuthTokenFetcher fetcher = new OAuthTokenFetcher(mActivity, account, mT okenScope, | |
|
Lambros
2016/05/13 01:11:31
Maybe just do:
new OAuthTokenFetcher(...).fetch();
Yuwei
2016/05/13 17:20:30
Done.
| |
| 46 new OAuthTokenFetcher.Callback() { | |
| 47 @Override | |
| 48 public void onTokenFetched(String token) { | |
| 49 mWaitingForAuthToken = false; | |
| 50 mLastToken = token; | |
| 51 callback.onTokenFetched(token); | |
| 52 } | |
| 53 | |
| 54 @Override | |
| 55 public void onError(OAuthTokenFetcher.Error error) { | |
| 56 mWaitingForAuthToken = false; | |
| 57 callback.onError(error); | |
| 58 } | |
| 59 }); | |
| 60 fetcher.fetch(); | |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 /** | |
| 65 * @return Last fetched auth token. This should be used right after calling consume() or | |
| 66 * retry(). The token may become invalid after some amount of time. | |
|
Yuwei
2016/05/13 00:30:17
Oops... forgot to remove retry()
Yuwei
2016/05/13 17:20:30
Done.
| |
| 67 */ | |
| 68 public String getLastToken() { | |
| 69 return mLastToken; | |
| 70 } | |
| 71 } | |
| OLD | NEW |