| 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 mLatestToken; |
| 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 * The callback will not be run if the task is already running and false wil
l be returned in |
| 35 * that case. |
| 36 * Each OAuthTokenConsumer is supposed to work for one specific task. It is
the caller's |
| 37 * responsibility to supply equivalent callbacks (variables being captured c
an vary) for the |
| 38 * same consumer. |
| 39 * @param account User's account name (email). |
| 40 * @param callback the callback to be called |
| 41 * @return true if the consumer will run |callback| when the token is fetche
d and false |
| 42 * otherwise (meaning a previous callback is waiting to be run). |
| 43 */ |
| 44 public boolean consume(String account, final OAuthTokenFetcher.Callback call
back) { |
| 45 if (mWaitingForAuthToken) { |
| 46 return false; |
| 47 } |
| 48 mWaitingForAuthToken = true; |
| 49 |
| 50 new OAuthTokenFetcher(mActivity, account, mTokenScope, new OAuthTokenFet
cher.Callback() { |
| 51 @Override |
| 52 public void onTokenFetched(String token) { |
| 53 mWaitingForAuthToken = false; |
| 54 mLatestToken = token; |
| 55 callback.onTokenFetched(token); |
| 56 } |
| 57 |
| 58 @Override |
| 59 public void onError(OAuthTokenFetcher.Error error) { |
| 60 mWaitingForAuthToken = false; |
| 61 callback.onError(error); |
| 62 } |
| 63 }).fetch(); |
| 64 return true; |
| 65 } |
| 66 |
| 67 /** |
| 68 * @return The latest auth token fetched by calling consume(). This should b
e used right after |
| 69 * the callback passed to consume() is run. The token may become in
valid after some |
| 70 * amount of time. |
| 71 */ |
| 72 public String getLatestToken() { |
| 73 return mLatestToken; |
| 74 } |
| 75 } |
| OLD | NEW |