Commit f0ba31f3 authored by Pâris MEULEMAN's avatar Pâris MEULEMAN Committed by Commit Bot

Wrap OAuth2TokenService token methods in IdentityManager

Wrap OAuth2TokenService GetAccessToken, InvalidateToken,
GetNewAccessToken in IdentityManager and use it in dependencies. This is
a step of the layering of Signin and Identity in Android and a precursor
to the internalization of Oauth2tokenservice.

details here:
https://docs.google.com/document/d/1_ks5kg3qr6TVq7NT6MFg6lh5Yiv-1lic6Rsd9nu5ubI


Bug: 934688
Change-Id: Idf7cde0546a32fe815f87ae3cea95cd092edfa79
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1764128
Auto-Submit: Pâris Meuleman <pmeuleman@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarTatiana Gornak <melandory@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Tatiana Gornak <melandory@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712973}
parent 23333545
...@@ -39,6 +39,7 @@ android_library("java") { ...@@ -39,6 +39,7 @@ android_library("java") {
"//chrome/browser/util:java", "//chrome/browser/util:java",
"//components/policy/android:policy_java", "//components/policy/android:policy_java",
"//components/signin/core/browser/android:java", "//components/signin/core/browser/android:java",
"//components/signin/public/identity_manager/android:java",
"//components/url_formatter/android:url_formatter_java", "//components/url_formatter/android:url_formatter_java",
"//content/public/android:content_java", "//content/public/android:content_java",
"//mojo/public/java:bindings_java", "//mojo/public/java:bindings_java",
......
...@@ -19,7 +19,7 @@ import org.chromium.base.annotations.JNINamespace; ...@@ -19,7 +19,7 @@ import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods; import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.signin.IdentityServicesProvider; import org.chromium.chrome.browser.signin.IdentityServicesProvider;
import org.chromium.components.signin.AccountManagerFacade; import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.OAuth2TokenService; import org.chromium.components.signin.identitymanager.IdentityManager;
import org.chromium.content_public.browser.WebContents; import org.chromium.content_public.browser.WebContents;
import java.util.Arrays; import java.util.Arrays;
...@@ -292,8 +292,8 @@ class AutofillAssistantClient { ...@@ -292,8 +292,8 @@ class AutofillAssistantClient {
return; return;
} }
IdentityServicesProvider.getOAuth2TokenService().getAccessToken( IdentityServicesProvider.getIdentityManager().getAccessToken(
mAccount, AUTH_TOKEN_TYPE, new OAuth2TokenService.GetAccessTokenCallback() { mAccount, AUTH_TOKEN_TYPE, new IdentityManager.GetAccessTokenCallback() {
@Override @Override
public void onGetTokenSuccess(String token) { public void onGetTokenSuccess(String token) {
if (mNativeClientAndroid != 0) { if (mNativeClientAndroid != 0) {
...@@ -318,7 +318,7 @@ class AutofillAssistantClient { ...@@ -318,7 +318,7 @@ class AutofillAssistantClient {
return; return;
} }
IdentityServicesProvider.getOAuth2TokenService().invalidateAccessToken(accessToken); IdentityServicesProvider.getIdentityManager().invalidateAccessToken(accessToken);
} }
/** Returns the e-mail address that corresponds to the access token or an empty string. */ /** Returns the e-mail address that corresponds to the access token or an empty string. */
......
...@@ -24,7 +24,7 @@ import org.chromium.base.task.TaskTraits; ...@@ -24,7 +24,7 @@ import org.chromium.base.task.TaskTraits;
import org.chromium.chrome.browser.init.ProcessInitializationHandler; import org.chromium.chrome.browser.init.ProcessInitializationHandler;
import org.chromium.components.signin.AccountManagerFacade; import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.ChromeSigninController; import org.chromium.components.signin.ChromeSigninController;
import org.chromium.components.signin.OAuth2TokenService; import org.chromium.components.signin.identitymanager.IdentityManager;
import org.chromium.components.sync.SyncConstants; import org.chromium.components.sync.SyncConstants;
import org.chromium.content_public.browser.UiThreadTaskTraits; import org.chromium.content_public.browser.UiThreadTaskTraits;
...@@ -70,9 +70,9 @@ public class InvalidationGcmUpstreamSender extends GcmUpstreamSenderService { ...@@ -70,9 +70,9 @@ public class InvalidationGcmUpstreamSender extends GcmUpstreamSenderService {
// Attempt to retrieve a token for the user. // Attempt to retrieve a token for the user.
// crbug.com/1014098: Do not use IdentityServicesProvider because the profile may not be // crbug.com/1014098: Do not use IdentityServicesProvider because the profile may not be
// initialized yet. // initialized yet.
OAuth2TokenService.getAccessTokenWithFacade(AccountManagerFacade.get(), account, IdentityManager.getAccessTokenWithFacade(AccountManagerFacade.get(), account,
SyncConstants.CHROME_SYNC_OAUTH2_SCOPE, SyncConstants.CHROME_SYNC_OAUTH2_SCOPE,
new OAuth2TokenService.GetAccessTokenCallback() { new IdentityManager.GetAccessTokenCallback() {
@Override @Override
public void onGetTokenSuccess(final String token) { public void onGetTokenSuccess(final String token) {
PostTask.postTask(TaskTraits.BEST_EFFORT_MAY_BLOCK, () -> { PostTask.postTask(TaskTraits.BEST_EFFORT_MAY_BLOCK, () -> {
......
...@@ -74,7 +74,8 @@ public class SigninManagerTest { ...@@ -74,7 +74,8 @@ public class SigninManagerTest {
mIdentityMutator = mock(IdentityMutator.class); mIdentityMutator = mock(IdentityMutator.class);
mIdentityManager = spy(new IdentityManager(0 /* nativeIdentityManager */)); mIdentityManager = spy(
new IdentityManager(0 /* nativeIdentityManager */, null /* OAuth2TokenService */));
AndroidSyncSettings androidSyncSettings = mock(AndroidSyncSettings.class); AndroidSyncSettings androidSyncSettings = mock(AndroidSyncSettings.class);
......
...@@ -239,6 +239,7 @@ if (is_android) { ...@@ -239,6 +239,7 @@ if (is_android) {
"//base:base_java", "//base:base_java",
"//base:jni_java", "//base:jni_java",
"//components/signin/core/browser/android:java", "//components/signin/core/browser/android:java",
"//components/signin/public/identity_manager/android:java",
"//components/sync/android:sync_java", "//components/sync/android:sync_java",
"//third_party/android_deps:com_google_protobuf_protobuf_lite_java", "//third_party/android_deps:com_google_protobuf_protobuf_lite_java",
"//third_party/cacheinvalidation:cacheinvalidation_javalib", "//third_party/cacheinvalidation:cacheinvalidation_javalib",
......
...@@ -30,8 +30,9 @@ import org.chromium.base.ContextUtils; ...@@ -30,8 +30,9 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.ChromeSigninController; import org.chromium.components.signin.ChromeSigninController;
import org.chromium.components.signin.OAuth2TokenService; import org.chromium.components.signin.identitymanager.IdentityManager;
import org.chromium.components.sync.AndroidSyncSettings; import org.chromium.components.sync.AndroidSyncSettings;
import org.chromium.components.sync.ModelTypeHelper; import org.chromium.components.sync.ModelTypeHelper;
import org.chromium.components.sync.SyncConstants; import org.chromium.components.sync.SyncConstants;
...@@ -258,9 +259,9 @@ public class InvalidationClientService extends AndroidListener { ...@@ -258,9 +259,9 @@ public class InvalidationClientService extends AndroidListener {
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
// Attempt to retrieve a token for the user. This method will also invalidate // Attempt to retrieve a token for the user. This method will also invalidate
// invalidAuthToken if it is non-null. // invalidAuthToken if it is non-null.
OAuth2TokenService.getNewAccessToken(account, invalidAuthToken, IdentityManager.getNewAccessTokenWithFacade(AccountManagerFacade.get(), account,
SyncConstants.CHROME_SYNC_OAUTH2_SCOPE, invalidAuthToken, SyncConstants.CHROME_SYNC_OAUTH2_SCOPE,
new OAuth2TokenService.GetAccessTokenCallback() { new IdentityManager.GetAccessTokenCallback() {
@Override @Override
public void onGetTokenSuccess(String token) { public void onGetTokenSuccess(String token) {
setAuthToken(InvalidationClientService.this.getApplicationContext(), setAuthToken(InvalidationClientService.this.getApplicationContext(),
......
...@@ -238,20 +238,26 @@ public final class OAuth2TokenService ...@@ -238,20 +238,26 @@ public final class OAuth2TokenService
/** /**
* Invalidates the old token (if non-null/non-empty) and asynchronously generates a new one. * Invalidates the old token (if non-null/non-empty) and asynchronously generates a new one.
*
* @deprecated Use invalidateAccessToken and getAccessToken instead. crbug.com/1002894: This
* method is needed by InvalidationClientService which is not necessary anymore.
*
* @param account the account to get the access token for. * @param account the account to get the access token for.
* @param oldToken The old token to be invalidated or null. * @param oldToken The old token to be invalidated or null.
* @param scope The scope to get an auth token for (with Android-style 'oauth2:' prefix). * @param scope The scope to get an auth token for (with Android-style 'oauth2:' prefix).
* @param callback called on successful and unsuccessful fetching of auth token. * @param callback called on successful and unsuccessful fetching of auth token.
*/ */
public static void getNewAccessToken(Account account, @Nullable String oldToken, String scope, @Deprecated
public static void getNewAccessTokenWithFacade(AccountManagerFacade accountManagerFacade,
Account account, @Nullable String oldToken, String scope,
GetAccessTokenCallback callback) { GetAccessTokenCallback callback) {
ConnectionRetry.runAuthTask(new AuthTask<String>() { ConnectionRetry.runAuthTask(new AuthTask<String>() {
@Override @Override
public String run() throws AuthException { public String run() throws AuthException {
if (!TextUtils.isEmpty(oldToken)) { if (!TextUtils.isEmpty(oldToken)) {
AccountManagerFacade.get().invalidateAccessToken(oldToken); accountManagerFacade.invalidateAccessToken(oldToken);
} }
return AccountManagerFacade.get().getAccessToken(account, scope); return accountManagerFacade.getAccessToken(account, scope);
} }
@Override @Override
public void onSuccess(String token) { public void onSuccess(String token) {
......
...@@ -172,7 +172,6 @@ void FakeProfileOAuth2TokenServiceDelegate::UpdateAuthError( ...@@ -172,7 +172,6 @@ void FakeProfileOAuth2TokenServiceDelegate::UpdateAuthError(
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
base::android::ScopedJavaLocalRef<jobject> base::android::ScopedJavaLocalRef<jobject>
FakeProfileOAuth2TokenServiceDelegate::GetJavaObject() { FakeProfileOAuth2TokenServiceDelegate::GetJavaObject() {
NOTREACHED();
return base::android::ScopedJavaLocalRef<jobject>(); return base::android::ScopedJavaLocalRef<jobject>();
} }
#endif #endif
...@@ -2,4 +2,8 @@ specific_include_rules = { ...@@ -2,4 +2,8 @@ specific_include_rules = {
"CoreAccountInfo.java": [ "CoreAccountInfo.java": [
"+components/signin/core/browser/android/java/src/org/chromium/components/signin/AccountManagerFacade.java" "+components/signin/core/browser/android/java/src/org/chromium/components/signin/AccountManagerFacade.java"
], ],
"IdentityManager.java": [
"+components/signin/core/browser/android/java/src/org/chromium/components/signin/AccountManagerFacade.java",
"+components/signin/core/browser/android/java/src/org/chromium/components/signin/OAuth2TokenService.java"
],
} }
...@@ -4,12 +4,16 @@ ...@@ -4,12 +4,16 @@
package org.chromium.components.signin.identitymanager; package org.chromium.components.signin.identitymanager;
import android.accounts.Account;
import android.support.annotation.MainThread;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import org.chromium.base.ObserverList; import org.chromium.base.ObserverList;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods; import org.chromium.base.annotations.NativeMethods;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.OAuth2TokenService;
/** /**
* IdentityManager provides access to native IdentityManager's public API to java components. * IdentityManager provides access to native IdentityManager's public API to java components.
...@@ -35,8 +39,13 @@ public class IdentityManager { ...@@ -35,8 +39,13 @@ public class IdentityManager {
*/ */
void onPrimaryAccountCleared(CoreAccountInfo account); void onPrimaryAccountCleared(CoreAccountInfo account);
} }
/**
* A simple callback for getAccessToken.
*/
public interface GetAccessTokenCallback extends OAuth2TokenService.GetAccessTokenCallback {}
private long mNativeIdentityManager; private long mNativeIdentityManager;
private OAuth2TokenService mOAuth2TokenService;
private final ObserverList<Observer> mObservers = new ObserverList<>(); private final ObserverList<Observer> mObservers = new ObserverList<>();
...@@ -44,14 +53,16 @@ public class IdentityManager { ...@@ -44,14 +53,16 @@ public class IdentityManager {
* Called by native to create an instance of IdentityManager. * Called by native to create an instance of IdentityManager.
*/ */
@CalledByNative @CalledByNative
private static IdentityManager create(long nativeIdentityManager) { private static IdentityManager create(
long nativeIdentityManager, OAuth2TokenService oAuth2TokenService) {
assert nativeIdentityManager != 0; assert nativeIdentityManager != 0;
return new IdentityManager(nativeIdentityManager); return new IdentityManager(nativeIdentityManager, oAuth2TokenService);
} }
@VisibleForTesting @VisibleForTesting
public IdentityManager(long nativeIdentityManager) { public IdentityManager(long nativeIdentityManager, OAuth2TokenService oAuth2TokenService) {
mNativeIdentityManager = nativeIdentityManager; mNativeIdentityManager = nativeIdentityManager;
mOAuth2TokenService = oAuth2TokenService;
} }
/** /**
...@@ -132,6 +143,72 @@ public class IdentityManager { ...@@ -132,6 +143,72 @@ public class IdentityManager {
mNativeIdentityManager, email); mNativeIdentityManager, email);
} }
/**
* Call this method to retrieve an OAuth2 access token for the given account and scope. Please
* note that this method expects a scope with 'oauth2:' prefix.
* @param account the account to get the access token for.
* @param scope The scope to get an auth token for (with Android-style 'oauth2:' prefix).
* @param callback called on successful and unsuccessful fetching of auth token.
*/
@MainThread
public void getAccessToken(Account account, String scope, GetAccessTokenCallback callback) {
assert mOAuth2TokenService != null;
// TODO(crbug.com/934688) The following should call a JNI method instead.
mOAuth2TokenService.getAccessToken(account, scope, callback);
}
/**
* Call this method to retrieve an OAuth2 access token for the given account and scope. Please
* note that this method expects a scope with 'oauth2:' prefix.
*
* @deprecated Use getAccessToken instead. crbug.com/1014098: This method is available as a
* workaround for a callsite where native is not initialized yet.
*
* @param accountManagerFacade AccountManagerFacade to request the access token from.
* @param account the account to get the access token for.
* @param scope The scope to get an auth token for (with Android-style 'oauth2:' prefix).
* @param callback called on successful and unsuccessful fetching of auth token.
*/
@MainThread
@Deprecated
public static void getAccessTokenWithFacade(AccountManagerFacade accountManagerFacade,
Account account, String scope, GetAccessTokenCallback callback) {
// TODO(crbug.com/934688) The following should call a JNI method instead.
OAuth2TokenService.getAccessTokenWithFacade(accountManagerFacade, account, scope, callback);
}
/**
* Called by native to invalidate an OAuth2 token. Please note that the token is invalidated
* asynchronously.
*/
@MainThread
public void invalidateAccessToken(String accessToken) {
assert mOAuth2TokenService != null;
// TODO(crbug.com/934688) The following should call a JNI method instead.
mOAuth2TokenService.invalidateAccessToken(accessToken);
}
/**
* Invalidates the old token (if non-null/non-empty) and asynchronously generates a new one.
*
* @deprecated Use invalidateAccessToken and getAccessToken instead. TODO(crbug.com/1002894):
* This method is needed by InvalidationClientService which is not necessary anymore.
*
* @param accountManagerFacade AccountManagerFacade to request the access token from.
* @param account the account to get the access token for.
* @param oldToken The old token to be invalidated or null.
* @param scope The scope to get an auth token for (with Android-style 'oauth2:' prefix).
* @param callback called on successful and unsuccessful fetching of auth token.
*/
@Deprecated
public static void getNewAccessTokenWithFacade(AccountManagerFacade accountManagerFacade,
Account account, @Nullable String oldToken, String scope,
GetAccessTokenCallback callback) {
OAuth2TokenService.getNewAccessTokenWithFacade(
accountManagerFacade, account, oldToken, scope, callback);
}
@NativeMethods @NativeMethods
interface Natives { interface Natives {
public @Nullable CoreAccountInfo getPrimaryAccountInfo(long nativeIdentityManager); public @Nullable CoreAccountInfo getPrimaryAccountInfo(long nativeIdentityManager);
......
...@@ -79,7 +79,8 @@ IdentityManager::IdentityManager( ...@@ -79,7 +79,8 @@ IdentityManager::IdentityManager(
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
java_identity_manager_ = Java_IdentityManager_create( java_identity_manager_ = Java_IdentityManager_create(
base::android::AttachCurrentThread(), reinterpret_cast<intptr_t>(this)); base::android::AttachCurrentThread(), reinterpret_cast<intptr_t>(this),
token_service_->GetDelegate()->GetJavaObject());
#endif #endif
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment