Commit 77f113ce authored by Mario Sanchez Prada's avatar Mario Sanchez Prada Committed by Commit Bot

Migrate SupervisedUserService away from ProfileOAuth2TokenService

Use IdentityManager and AccountsMutator instead to remove the dependency.

Bug: 912150
Change-Id: Ia0b678feceeaf9d39898a5a82a2c997bfb63b9cf
Reviewed-on: https://chromium-review.googlesource.com/c/1415410Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Commit-Queue: Mario Sanchez Prada <mario@igalia.com>
Cr-Commit-Position: refs/heads/master@{#628284}
parent 6586e42a
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/supervised_user_whitelist_installer.h" #include "chrome/browser/component_updater/supervised_user_whitelist_installer.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/supervised_user/experimental/supervised_user_filtering_switches.h" #include "chrome/browser/supervised_user/experimental/supervised_user_filtering_switches.h"
#include "chrome/browser/supervised_user/permission_request_creator.h" #include "chrome/browser/supervised_user/permission_request_creator.h"
#include "chrome/browser/supervised_user/supervised_user_constants.h" #include "chrome/browser/supervised_user/supervised_user_constants.h"
...@@ -42,12 +42,13 @@ ...@@ -42,12 +42,13 @@
#include "components/policy/core/browser/url_util.h" #include "components/policy/core/browser/url_util.h"
#include "components/pref_registry/pref_registry_syncable.h" #include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/sync/driver/sync_user_settings.h" #include "components/sync/driver/sync_user_settings.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "extensions/buildflags/buildflags.h" #include "extensions/buildflags/buildflags.h"
#include "net/traffic_annotation/network_traffic_annotation.h" #include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/identity/public/cpp/accounts_mutator.h"
#include "services/identity/public/cpp/identity_manager.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
...@@ -324,11 +325,9 @@ base::string16 SupervisedUserService::GetExtensionsLockedMessage() const { ...@@ -324,11 +325,9 @@ base::string16 SupervisedUserService::GetExtensionsLockedMessage() const {
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
void SupervisedUserService::InitSync(const std::string& refresh_token) { void SupervisedUserService::InitSync(const std::string& refresh_token) {
ProfileOAuth2TokenService* token_service = IdentityManagerFactory::GetForProfile(profile_)
ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); ->GetAccountsMutator()
token_service->UpdateCredentials( ->LegacySetRefreshTokenForSupervisedUser(refresh_token);
supervised_users::kSupervisedUserPseudoEmail, refresh_token,
signin_metrics::SourceForRefreshTokenOperation::kSupervisedUser_InitSync);
} }
#endif // !defined(OS_ANDROID) #endif // !defined(OS_ANDROID)
...@@ -378,9 +377,7 @@ void SupervisedUserService::SetActive(bool active) { ...@@ -378,9 +377,7 @@ void SupervisedUserService::SetActive(bool active) {
if (!delegate_ || !delegate_->SetActive(active_)) { if (!delegate_ || !delegate_->SetActive(active_)) {
if (active_) { if (active_) {
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
ProfileOAuth2TokenService* token_service = IdentityManagerFactory::GetForProfile(profile_)->LegacyLoadCredentials(
ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
token_service->LoadCredentials(
supervised_users::kSupervisedUserPseudoEmail); supervised_users::kSupervisedUserPseudoEmail);
#else #else
NOTREACHED(); NOTREACHED();
......
...@@ -61,6 +61,11 @@ class AccountsMutator { ...@@ -61,6 +61,11 @@ class AccountsMutator {
const std::string& account_id) = 0; const std::string& account_id) = 0;
#endif #endif
// Updates the refresh token for the supervised user.
// TODO(860492): Remove this once supervised user support is removed.
virtual void LegacySetRefreshTokenForSupervisedUser(
const std::string& refresh_token) = 0;
private: private:
DISALLOW_COPY_AND_ASSIGN(AccountsMutator); DISALLOW_COPY_AND_ASSIGN(AccountsMutator);
}; };
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "services/identity/public/cpp/accounts_mutator_impl.h" #include "services/identity/public/cpp/accounts_mutator_impl.h"
#include <string>
#include "base/logging.h" #include "base/logging.h"
#include "components/signin/core/browser/account_info.h" #include "components/signin/core/browser/account_info.h"
#include "components/signin/core/browser/account_tracker_service.h" #include "components/signin/core/browser/account_tracker_service.h"
...@@ -90,4 +92,11 @@ void AccountsMutatorImpl::MoveAccount(AccountsMutator* target, ...@@ -90,4 +92,11 @@ void AccountsMutatorImpl::MoveAccount(AccountsMutator* target,
} }
#endif #endif
void AccountsMutatorImpl::LegacySetRefreshTokenForSupervisedUser(
const std::string& refresh_token) {
token_service_->UpdateCredentials(
"managed_user@localhost", refresh_token,
signin_metrics::SourceForRefreshTokenOperation::kSupervisedUser_InitSync);
}
} // namespace identity } // namespace identity
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef SERVICES_IDENTITY_PUBLIC_CPP_ACCOUNTS_MUTATOR_IMPL_H_ #ifndef SERVICES_IDENTITY_PUBLIC_CPP_ACCOUNTS_MUTATOR_IMPL_H_
#define SERVICES_IDENTITY_PUBLIC_CPP_ACCOUNTS_MUTATOR_IMPL_H_ #define SERVICES_IDENTITY_PUBLIC_CPP_ACCOUNTS_MUTATOR_IMPL_H_
#include <string>
#include "base/macros.h" #include "base/macros.h"
#include "components/signin/core/browser/signin_metrics.h" #include "components/signin/core/browser/signin_metrics.h"
#include "services/identity/public/cpp/accounts_mutator.h" #include "services/identity/public/cpp/accounts_mutator.h"
...@@ -63,6 +65,11 @@ class AccountsMutatorImpl : public AccountsMutator { ...@@ -63,6 +65,11 @@ class AccountsMutatorImpl : public AccountsMutator {
const std::string& account_id) override; const std::string& account_id) override;
#endif #endif
// Updates the refresh token for the supervised user.
// TODO(860492): Remove this once supervised user support is removed.
void LegacySetRefreshTokenForSupervisedUser(
const std::string& refresh_token) override;
private: private:
ProfileOAuth2TokenService* token_service_; ProfileOAuth2TokenService* token_service_;
AccountTrackerService* account_tracker_service_; AccountTrackerService* account_tracker_service_;
......
...@@ -23,6 +23,7 @@ const char kTestEmail[] = "test_user@test.com"; ...@@ -23,6 +23,7 @@ const char kTestEmail[] = "test_user@test.com";
const char kTestEmail2[] = "test_user@test-2.com"; const char kTestEmail2[] = "test_user@test-2.com";
const char kRefreshToken[] = "refresh_token"; const char kRefreshToken[] = "refresh_token";
const char kRefreshToken2[] = "refresh_token_2"; const char kRefreshToken2[] = "refresh_token_2";
const char kSupervisedUserPseudoEmail[] = "managed_user@localhost";
// Class that observes updates from identity::IdentityManager. // Class that observes updates from identity::IdentityManager.
class TestIdentityManagerObserver : public identity::IdentityManager::Observer { class TestIdentityManagerObserver : public identity::IdentityManager::Observer {
...@@ -607,4 +608,39 @@ TEST_F(AccountsMutatorTest, MoveAccount) { ...@@ -607,4 +608,39 @@ TEST_F(AccountsMutatorTest, MoveAccount) {
} }
#endif // BUILDFLAG(ENABLE_DICE_SUPPORT) #endif // BUILDFLAG(ENABLE_DICE_SUPPORT)
TEST_F(AccountsMutatorTest, LegacySetRefreshTokenForSupervisedUser) {
// Abort the test if the current platform does not support accounts mutation.
if (!accounts_mutator())
return;
EXPECT_EQ(identity_manager()->GetAccountsWithRefreshTokens().size(), 0U);
base::RunLoop run_loop;
identity_manager_observer()->set_on_refresh_token_updated_callback(
base::BindOnce(
[](base::OnceClosure quit_closure, const std::string& account_id) {
std::move(quit_closure).Run();
},
run_loop.QuitClosure()));
accounts_mutator()->LegacySetRefreshTokenForSupervisedUser(kRefreshToken);
run_loop.Run();
// In the context of supervised users, the ProfileOAuth2TokenService is used
// without the AccountTrackerService being used, so we can't use any of the
// IdentityManager::FindAccountInfoForAccountWithRefreshTokenBy*() methods
// since they won't find any account. Use GetAccountsWithRefreshTokens() and
// HasAccountWithRefreshToken*() instead, that only relies in the PO2TS.
std::vector<AccountInfo> accounts =
identity_manager()->GetAccountsWithRefreshTokens();
EXPECT_EQ(accounts.size(), 1U);
EXPECT_EQ(accounts[0].account_id, kSupervisedUserPseudoEmail);
EXPECT_EQ(accounts[0].email, kSupervisedUserPseudoEmail);
EXPECT_TRUE(
identity_manager()->HasAccountWithRefreshToken(accounts[0].account_id));
EXPECT_FALSE(
identity_manager()->HasAccountWithRefreshTokenInPersistentErrorState(
accounts[0].account_id));
}
} // namespace identity } // namespace identity
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