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

Refactor policy registration and isUserManaged

The main purpose here is the refactoring of the almost identical
RegisterAndFetchPolicyBeforeSignIn and isUserManager. This brought some
simplification on the java code, with the removal of the native call to
ShouldLoadPolicyForUser.

Bug: 963400
Change-Id: I828f9a328bc153ebc6325f194fb34206bbe40e9a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1628721
Commit-Queue: Pâris Meuleman <pmeuleman@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#666337}
parent cf6d6b40
...@@ -443,13 +443,6 @@ public class SigninManager implements AccountTrackerService.OnSystemAccountsSeed ...@@ -443,13 +443,6 @@ public class SigninManager implements AccountTrackerService.OnSystemAccountsSeed
return; return;
} }
if (!SigninManagerJni.get().shouldLoadPolicyForUser(mSignInState.mAccount.name)) {
// Proceed with the sign-in flow without checking for policy if it can be determined
// that this account can't have management enabled based on the username.
finishSignIn();
return;
}
Log.d(TAG, "Checking if account has policy management enabled"); Log.d(TAG, "Checking if account has policy management enabled");
// This will call back to onPolicyFetchedBeforeSignIn. // This will call back to onPolicyFetchedBeforeSignIn.
SigninManagerJni.get().registerAndFetchPolicyBeforeSignIn( SigninManagerJni.get().registerAndFetchPolicyBeforeSignIn(
...@@ -458,9 +451,11 @@ public class SigninManager implements AccountTrackerService.OnSystemAccountsSeed ...@@ -458,9 +451,11 @@ public class SigninManager implements AccountTrackerService.OnSystemAccountsSeed
@CalledByNative @CalledByNative
@VisibleForTesting @VisibleForTesting
/**
* If the user is managed, its policy has been fetched and is being enforced; features like sync
* may now be disabled by policy, and the rest of the sign-in flow can be resumed.
*/
void onPolicyFetchedBeforeSignIn() { void onPolicyFetchedBeforeSignIn() {
// Policy has been fetched for the user and is being enforced; features like sync may now
// be disabled by policy, and the rest of the sign-in flow can be resumed.
finishSignIn(); finishSignIn();
} }
...@@ -727,7 +722,7 @@ public class SigninManager implements AccountTrackerService.OnSystemAccountsSeed ...@@ -727,7 +722,7 @@ public class SigninManager implements AccountTrackerService.OnSystemAccountsSeed
* otherwise. May be called synchronously from this function. * otherwise. May be called synchronously from this function.
*/ */
public void isUserManaged(String email, final Callback<Boolean> callback) { public void isUserManaged(String email, final Callback<Boolean> callback) {
SigninManagerJni.get().isUserManaged(email, callback); SigninManagerJni.get().isUserManaged(this, mNativeSigninManagerAndroid, email, callback);
} }
public static String extractDomainName(String email) { public static String extractDomainName(String email) {
...@@ -773,9 +768,8 @@ public class SigninManager implements AccountTrackerService.OnSystemAccountsSeed ...@@ -773,9 +768,8 @@ public class SigninManager implements AccountTrackerService.OnSystemAccountsSeed
boolean isSignedInOnNative(@JCaller SigninManager self, long nativeSigninManagerAndroid); boolean isSignedInOnNative(@JCaller SigninManager self, long nativeSigninManagerAndroid);
boolean shouldLoadPolicyForUser(String username); void isUserManaged(@JCaller SigninManager self, long nativeSigninManagerAndroid,
String username, Callback<Boolean> callback);
void isUserManaged(String username, Callback<Boolean> callback);
String extractDomainName(String email); String extractDomainName(String email);
} }
......
...@@ -212,7 +212,6 @@ public class SigninManagerTest { ...@@ -212,7 +212,6 @@ public class SigninManagerTest {
doReturn(true).when(mAccountTrackerService).checkAndSeedSystemAccounts(); doReturn(true).when(mAccountTrackerService).checkAndSeedSystemAccounts();
// Request that policy is loaded. It will pause sign-in until onPolicyCheckedBeforeSignIn is // Request that policy is loaded. It will pause sign-in until onPolicyCheckedBeforeSignIn is
// invoked. // invoked.
doReturn(true).when(mNativeMock).shouldLoadPolicyForUser(any());
doNothing().when(mNativeMock).registerAndFetchPolicyBeforeSignIn(any(), anyLong(), any()); doNothing().when(mNativeMock).registerAndFetchPolicyBeforeSignIn(any(), anyLong(), any());
doReturn(true).when(mSigninManager).isSigninSupported(); doReturn(true).when(mSigninManager).isSigninSupported();
......
...@@ -209,6 +209,31 @@ void SigninManagerAndroid::WipeGoogleServiceWorkerCaches( ...@@ -209,6 +209,31 @@ void SigninManagerAndroid::WipeGoogleServiceWorkerCaches(
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
} }
void SigninManagerAndroid::RegisterPolicyWithAccount(
const CoreAccountInfo& account,
RegisterPolicyWithAccountCallback callback) {
if (!ShouldLoadPolicyForUser(account.email)) {
std::move(callback).Run(base::nullopt);
return;
}
policy::UserPolicySigninService* service =
policy::UserPolicySigninServiceFactory::GetForProfile(profile_);
service->RegisterForPolicyWithAccountId(
account.email, account.account_id,
base::AdaptCallbackForRepeating(base::BindOnce(
[](RegisterPolicyWithAccountCallback callback,
const std::string& dm_token, const std::string& client_id) {
base::Optional<ManagementCredentials> credentials;
if (!dm_token.empty()) {
credentials.emplace(dm_token, client_id);
}
std::move(callback).Run(credentials);
},
std::move(callback))));
}
void SigninManagerAndroid::RegisterAndFetchPolicyBeforeSignIn( void SigninManagerAndroid::RegisterAndFetchPolicyBeforeSignIn(
JNIEnv* env, JNIEnv* env,
const JavaParamRef<jobject>& obj, const JavaParamRef<jobject>& obj,
...@@ -220,43 +245,39 @@ void SigninManagerAndroid::RegisterAndFetchPolicyBeforeSignIn( ...@@ -220,43 +245,39 @@ void SigninManagerAndroid::RegisterAndFetchPolicyBeforeSignIn(
// ExtractDomainName Dchecks that username is a valid email, in practice // ExtractDomainName Dchecks that username is a valid email, in practice
// this checks that @ is present and is not the last character. // this checks that @ is present and is not the last character.
gaia::ExtractDomainName(username); gaia::ExtractDomainName(username);
policy::UserPolicySigninService* service =
policy::UserPolicySigninServiceFactory::GetForProfile(profile_);
CoreAccountInfo account = CoreAccountInfo account =
IdentityManagerFactory::GetForProfile(profile_) IdentityManagerFactory::GetForProfile(profile_)
->FindAccountInfoForAccountWithRefreshTokenByEmailAddress(username) ->FindAccountInfoForAccountWithRefreshTokenByEmailAddress(username)
.value(); .value();
service->RegisterForPolicyWithAccountId(
username, account.account_id, RegisterPolicyWithAccount(
base::Bind(&SigninManagerAndroid::OnPolicyRegisterDone, account, base::BindOnce(&SigninManagerAndroid::OnPolicyRegisterDone,
weak_factory_.GetWeakPtr(), account)); weak_factory_.GetWeakPtr(), account));
} }
void SigninManagerAndroid::OnPolicyRegisterDone(const CoreAccountInfo& account, void SigninManagerAndroid::OnPolicyRegisterDone(
const std::string& dm_token, const CoreAccountInfo& account,
const std::string& client_id) { const base::Optional<ManagementCredentials>& credentials) {
if (dm_token.empty()) { if (credentials) {
FetchPolicyBeforeSignIn(account, credentials.value());
} else {
// User's account does not have a policy to fetch. // User's account does not have a policy to fetch.
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
Java_SigninManager_onPolicyFetchedBeforeSignIn(env, java_signin_manager_); Java_SigninManager_onPolicyFetchedBeforeSignIn(env, java_signin_manager_);
} else {
FetchPolicyBeforeSignIn(account, dm_token, client_id);
} }
} }
void SigninManagerAndroid::FetchPolicyBeforeSignIn( void SigninManagerAndroid::FetchPolicyBeforeSignIn(
const CoreAccountInfo& account, const CoreAccountInfo& account,
const std::string& dm_token, const ManagementCredentials& credentials) {
const std::string& client_id) {
policy::UserPolicySigninService* service = policy::UserPolicySigninService* service =
policy::UserPolicySigninServiceFactory::GetForProfile(profile_); policy::UserPolicySigninServiceFactory::GetForProfile(profile_);
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory = scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
content::BrowserContext::GetDefaultStoragePartition(profile_) content::BrowserContext::GetDefaultStoragePartition(profile_)
->GetURLLoaderFactoryForBrowserProcess(); ->GetURLLoaderFactoryForBrowserProcess();
service->FetchPolicyForSignedInUser( service->FetchPolicyForSignedInUser(
AccountIdFromAccountInfo(account), dm_token, client_id, AccountIdFromAccountInfo(account), credentials.dm_token,
url_loader_factory, credentials.client_id, url_loader_factory,
base::Bind(&SigninManagerAndroid::OnPolicyFetchDone, base::Bind(&SigninManagerAndroid::OnPolicyFetchDone,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
} }
...@@ -335,43 +356,26 @@ static jlong JNI_SigninManager_Init(JNIEnv* env, ...@@ -335,43 +356,26 @@ static jlong JNI_SigninManager_Init(JNIEnv* env,
return reinterpret_cast<intptr_t>(signin_manager_android); return reinterpret_cast<intptr_t>(signin_manager_android);
} }
static jboolean JNI_SigninManager_ShouldLoadPolicyForUser( void SigninManagerAndroid::IsUserManaged(
JNIEnv* env,
const JavaParamRef<jstring>& j_username) {
std::string username =
base::android::ConvertJavaStringToUTF8(env, j_username);
return ShouldLoadPolicyForUser(username);
}
static void JNI_SigninManager_IsUserManaged(
JNIEnv* env, JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jstring>& j_username, const JavaParamRef<jstring>& j_username,
const JavaParamRef<jobject>& j_callback) { const JavaParamRef<jobject>& j_callback) {
base::android::ScopedJavaGlobalRef<jobject> callback(env, j_callback); base::android::ScopedJavaGlobalRef<jobject> callback(env, j_callback);
std::string username = std::string username =
base::android::ConvertJavaStringToUTF8(env, j_username); base::android::ConvertJavaStringToUTF8(env, j_username);
if (!ShouldLoadPolicyForUser(username)) {
base::android::RunBooleanCallbackAndroid(callback, false);
return;
}
Profile* profile = ProfileManager::GetActiveUserProfile(); base::Optional<CoreAccountInfo> account =
policy::UserPolicySigninService* service = IdentityManagerFactory::GetForProfile(profile_)
policy::UserPolicySigninServiceFactory::GetForProfile(profile); ->FindAccountInfoForAccountWithRefreshTokenByEmailAddress(username);
service->RegisterForPolicyWithAccountId(
username, RegisterPolicyWithAccount(
IdentityManagerFactory::GetForProfile(profile) account.value_or(CoreAccountInfo{}),
->FindAccountInfoForAccountWithRefreshTokenByEmailAddress(username) base::BindOnce(
.value_or(AccountInfo{})
.account_id,
// TODO: if UserPolicySigninService::PolicyRegistrationCallback is changed
// to base::OnceCallback, change this to base::BindOnce; otherwise remove
// this comment. See https://crbug.com/948098 for more details.
base::BindRepeating(
[](base::android::ScopedJavaGlobalRef<jobject> callback, [](base::android::ScopedJavaGlobalRef<jobject> callback,
const std::string& dm_token, const std::string& client_id) { const base::Optional<ManagementCredentials>& credentials) {
base::android::RunBooleanCallbackAndroid(callback, base::android::RunBooleanCallbackAndroid(callback,
!dm_token.empty()); credentials.has_value());
}, },
callback)); callback));
} }
......
...@@ -79,24 +79,44 @@ class SigninManagerAndroid : public identity::IdentityManager::Observer { ...@@ -79,24 +79,44 @@ class SigninManagerAndroid : public identity::IdentityManager::Observer {
jboolean IsSignedInOnNative(JNIEnv* env, jboolean IsSignedInOnNative(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj); const base::android::JavaParamRef<jobject>& obj);
void IsUserManaged(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& j_username,
const base::android::JavaParamRef<jobject>& j_callback);
// identity::IdentityManager::Observer implementation. // identity::IdentityManager::Observer implementation.
void OnPrimaryAccountCleared( void OnPrimaryAccountCleared(
const CoreAccountInfo& previous_primary_account_info) override; const CoreAccountInfo& previous_primary_account_info) override;
private: private:
struct ManagementCredentials {
ManagementCredentials(const std::string& dm_token,
const std::string& client_id)
: dm_token(dm_token), client_id(client_id) {}
const std::string dm_token;
const std::string client_id;
};
using RegisterPolicyWithAccountCallback = base::OnceCallback<void(
const base::Optional<ManagementCredentials>& credentials)>;
friend class SigninManagerAndroidTest; friend class SigninManagerAndroidTest;
FRIEND_TEST_ALL_PREFIXES(SigninManagerAndroidTest, FRIEND_TEST_ALL_PREFIXES(SigninManagerAndroidTest,
DeleteGoogleServiceWorkerCaches); DeleteGoogleServiceWorkerCaches);
~SigninManagerAndroid() override; ~SigninManagerAndroid() override;
void OnPolicyRegisterDone(const CoreAccountInfo& account_id, // If required registers for policy with given account. callback will be
const std::string& dm_token, // called with credentials if the account is managed.
const std::string& client_id); void RegisterPolicyWithAccount(const CoreAccountInfo& account,
RegisterPolicyWithAccountCallback callback);
void OnPolicyRegisterDone(
const CoreAccountInfo& account_id,
const base::Optional<ManagementCredentials>& credentials);
void FetchPolicyBeforeSignIn(const CoreAccountInfo& account_id, void FetchPolicyBeforeSignIn(const CoreAccountInfo& account_id,
const std::string& dm_token, const ManagementCredentials& credentials);
const std::string& client_id);
void OnPolicyFetchDone(bool success) const; void OnPolicyFetchDone(bool success) const;
......
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