This source file includes following definitions.
- onRefreshTokenAvailable
- onRefreshTokenRevoked
- onRefreshTokensLoaded
- getForProfile
- create
- addObserver
- removeObserver
- getAccountOrNullFromUsername
- getAccounts
- getOAuth2AuthToken
- getOAuth2AccessToken
- getOAuth2AccessTokenWithTimeout
- hasOAuth2RefreshToken
- invalidateOAuth2AuthToken
- validateAccounts
- fireRefreshTokenAvailable
- notifyRefreshTokenAvailable
- fireRefreshTokenRevoked
- notifyRefreshTokenRevoked
- fireRefreshTokensLoaded
- notifyRefreshTokensLoaded
- nativeGetForProfile
- nativeOAuth2TokenFetched
- nativeValidateAccounts
- nativeFireRefreshTokenAvailableFromJava
- nativeFireRefreshTokenRevokedFromJava
- nativeFireRefreshTokensLoadedFromJava
package org.chromium.chrome.browser.signin;
import android.accounts.Account;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import org.chromium.base.CalledByNative;
import org.chromium.base.ObserverList;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.signin.ChromeSigninController;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
public final class OAuth2TokenService {
private static final String TAG = "OAuth2TokenService";
public interface OAuth2TokenServiceObserver {
void onRefreshTokenAvailable(Account account);
void onRefreshTokenRevoked(Account account);
void onRefreshTokensLoaded();
}
private static final String OAUTH2_SCOPE_PREFIX = "oauth2:";
private final long mNativeProfileOAuth2TokenService;
private final ObserverList<OAuth2TokenServiceObserver> mObservers;
private OAuth2TokenService(long nativeOAuth2Service) {
mNativeProfileOAuth2TokenService = nativeOAuth2Service;
mObservers = new ObserverList<OAuth2TokenServiceObserver>();
}
public static OAuth2TokenService getForProfile(Profile profile) {
ThreadUtils.assertOnUiThread();
return (OAuth2TokenService) nativeGetForProfile(profile);
}
@CalledByNative
private static OAuth2TokenService create(long nativeOAuth2Service) {
ThreadUtils.assertOnUiThread();
return new OAuth2TokenService(nativeOAuth2Service);
}
public void addObserver(OAuth2TokenServiceObserver observer) {
ThreadUtils.assertOnUiThread();
mObservers.addObserver(observer);
}
public void removeObserver(OAuth2TokenServiceObserver observer) {
ThreadUtils.assertOnUiThread();
mObservers.removeObserver(observer);
}
private static Account getAccountOrNullFromUsername(Context context, String username) {
if (username == null) {
Log.e(TAG, "Username is null");
return null;
}
AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
Account account = accountManagerHelper.getAccountFromName(username);
if (account == null) {
Log.e(TAG, "Account not found for provided username.");
return null;
}
return account;
}
@CalledByNative
public static String[] getAccounts(Context context) {
AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
java.util.List<String> accountNames = accountManagerHelper.getGoogleAccountNames();
return accountNames.toArray(new String[accountNames.size()]);
}
@CalledByNative
public static void getOAuth2AuthToken(
Context context, String username, String scope, final long nativeCallback) {
Account account = getAccountOrNullFromUsername(context, username);
if (account == null) {
nativeOAuth2TokenFetched(null, false, nativeCallback);
return;
}
String oauth2Scope = OAUTH2_SCOPE_PREFIX + scope;
AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(context);
accountManagerHelper.getAuthTokenFromForeground(
null, account, oauth2Scope, new AccountManagerHelper.GetAuthTokenCallback() {
@Override
public void tokenAvailable(String token) {
nativeOAuth2TokenFetched(
token, token != null, nativeCallback);
}
});
}
public static void getOAuth2AccessToken(Context context, @Nullable Activity activity,
Account account, String scope,
AccountManagerHelper.GetAuthTokenCallback callback) {
String oauth2Scope = OAUTH2_SCOPE_PREFIX + scope;
AccountManagerHelper.get(context).getAuthTokenFromForeground(
activity, account, oauth2Scope, callback);
}
public static String getOAuth2AccessTokenWithTimeout(
Context context, @Nullable Activity activity, Account account, String scope,
long timeout, TimeUnit unit) {
assert !ThreadUtils.runningOnUiThread();
final AtomicReference<String> result = new AtomicReference<String>();
final Semaphore semaphore = new Semaphore(0);
getOAuth2AccessToken(
context, activity, account, scope,
new AccountManagerHelper.GetAuthTokenCallback() {
@Override
public void tokenAvailable(String token) {
result.set(token);
semaphore.release();
}
});
try {
if (semaphore.tryAcquire(timeout, unit)) {
return result.get();
} else {
Log.d(TAG, "Failed to retrieve auth token within timeout (" +
timeout + " + " + unit.name() + ")");
return null;
}
} catch (InterruptedException e) {
Log.w(TAG, "Got interrupted while waiting for auth token");
return null;
}
}
@CalledByNative
public static boolean hasOAuth2RefreshToken(Context context, String accountName) {
return AccountManagerHelper.get(context).hasAccountForName(accountName);
}
@CalledByNative
public static void invalidateOAuth2AuthToken(Context context, String accessToken) {
if (accessToken != null) {
AccountManagerHelper.get(context).invalidateAuthToken(accessToken);
}
}
public void validateAccounts(Context context) {
ThreadUtils.assertOnUiThread();
String currentlySignedInAccount =
ChromeSigninController.get(context).getSignedInAccountName();
String[] accounts = getAccounts(context);
nativeValidateAccounts(
mNativeProfileOAuth2TokenService, accounts, currentlySignedInAccount);
}
public void fireRefreshTokenAvailable(Account account) {
ThreadUtils.assertOnUiThread();
assert account != null;
nativeFireRefreshTokenAvailableFromJava(mNativeProfileOAuth2TokenService, account.name);
}
@CalledByNative
public void notifyRefreshTokenAvailable(String accountName) {
assert accountName != null;
Account account = AccountManagerHelper.createAccountFromName(accountName);
for (OAuth2TokenServiceObserver observer : mObservers) {
observer.onRefreshTokenAvailable(account);
}
}
public void fireRefreshTokenRevoked(Account account) {
ThreadUtils.assertOnUiThread();
assert account != null;
nativeFireRefreshTokenRevokedFromJava(mNativeProfileOAuth2TokenService, account.name);
}
@CalledByNative
public void notifyRefreshTokenRevoked(String accountName) {
assert accountName != null;
Account account = AccountManagerHelper.createAccountFromName(accountName);
for (OAuth2TokenServiceObserver observer : mObservers) {
observer.onRefreshTokenRevoked(account);
}
}
public void fireRefreshTokensLoaded() {
ThreadUtils.assertOnUiThread();
nativeFireRefreshTokensLoadedFromJava(mNativeProfileOAuth2TokenService);
}
@CalledByNative
public void notifyRefreshTokensLoaded() {
for (OAuth2TokenServiceObserver observer : mObservers) {
observer.onRefreshTokensLoaded();
}
}
private static native Object nativeGetForProfile(Profile profile);
private static native void nativeOAuth2TokenFetched(
String authToken, boolean result, long nativeCallback);
private native void nativeValidateAccounts(
long nativeAndroidProfileOAuth2TokenService,
String[] accounts, String currentlySignedInAccount);
private native void nativeFireRefreshTokenAvailableFromJava(
long nativeAndroidProfileOAuth2TokenService, String accountName);
private native void nativeFireRefreshTokenRevokedFromJava(
long nativeAndroidProfileOAuth2TokenService, String accountName);
private native void nativeFireRefreshTokensLoadedFromJava(
long nativeAndroidProfileOAuth2TokenService);
}