Commit e1fef60d authored by Mihai Sardarescu's avatar Mihai Sardarescu Committed by Commit Bot

Convert to use CoreAccountId on iOS.

This CL converts all ChromeOS code to use CoreAccountId instead of
std::string for the account id.

Bug: 959157
Change-Id: Ia27b9e566100ecd7c1ead3a5ca8e46da39e569ea
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1906149Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Mihai Sardarescu <msarda@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713856}
parent 6b045dc3
......@@ -207,7 +207,7 @@ void ProfileOAuth2TokenServiceIOSDelegate::ReloadCredentials() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// Get the list of new account ids.
std::set<std::string> new_account_ids;
std::set<CoreAccountId> new_account_ids;
for (const auto& new_account : provider_->GetAllAccounts()) {
DCHECK(!new_account.gaia.empty());
DCHECK(!new_account.email.empty());
......@@ -216,7 +216,7 @@ void ProfileOAuth2TokenServiceIOSDelegate::ReloadCredentials() {
// the GAIA ID is available if any client of this token service starts
// a fetch access token operation when it receives a
// |OnRefreshTokenAvailable| notification.
std::string account_id = account_tracker_service_->SeedAccountInfo(
CoreAccountId account_id = account_tracker_service_->SeedAccountInfo(
AccountInfoFromDeviceAccount(new_account));
new_account_ids.insert(account_id);
}
......
......@@ -99,7 +99,7 @@ class ProfileOAuth2TokenServiceIOSDelegateTest
auth_error_changed_count_ = 0;
}
std::string GetAccountId(const ProviderAccount& provider_account) {
CoreAccountId GetAccountId(const ProviderAccount& provider_account) {
return account_tracker_.PickAccountIdForAccount(provider_account.gaia,
provider_account.email);
}
......@@ -139,7 +139,8 @@ TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest,
EXPECT_EQ(0, tokens_loaded_count_);
EXPECT_EQ(1, token_revoked_count_);
EXPECT_EQ(0U, oauth2_delegate_->GetAccounts().size());
EXPECT_FALSE(oauth2_delegate_->RefreshTokenIsAvailable("another_account"));
EXPECT_FALSE(oauth2_delegate_->RefreshTokenIsAvailable(
CoreAccountId("another_account")));
}
TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest,
......@@ -277,7 +278,7 @@ TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest,
ResetObserverCounts();
GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_ERROR);
oauth2_delegate_->UpdateAuthError(GetAccountId(account1), error);
EXPECT_EQ(error, oauth2_delegate_->GetAuthError("gaia_1"));
EXPECT_EQ(error, oauth2_delegate_->GetAuthError(GetAccountId(account1)));
EXPECT_EQ(1, auth_error_changed_count_);
oauth2_delegate_->RevokeAllCredentials();
......@@ -292,15 +293,15 @@ TEST_F(ProfileOAuth2TokenServiceIOSDelegateTest, GetAuthError) {
oauth2_delegate_->ReloadCredentials();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(),
oauth2_delegate_->GetAuthError("gaia_1"));
oauth2_delegate_->GetAuthError(GetAccountId(account1)));
// Update the error.
GoogleServiceAuthError error =
GoogleServiceAuthError::FromInvalidGaiaCredentialsReason(
GoogleServiceAuthError::InvalidGaiaCredentialsReason::
CREDENTIALS_REJECTED_BY_SERVER);
oauth2_delegate_->UpdateAuthError("gaia_1", error);
EXPECT_EQ(error, oauth2_delegate_->GetAuthError("gaia_1"));
oauth2_delegate_->UpdateAuthError(GetAccountId(account1), error);
EXPECT_EQ(error, oauth2_delegate_->GetAuthError(GetAccountId(account1)));
// Unknown account has no error.
EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(),
oauth2_delegate_->GetAuthError("gaia_2"));
oauth2_delegate_->GetAuthError(CoreAccountId("gaia_2")));
}
......@@ -22,7 +22,7 @@ CoreAccountId::CoreAccountId(std::string&& id) : id(std::move(id)) {}
CoreAccountId::CoreAccountId(const std::string& id) : id(id) {}
#if defined(OS_CHROMEOS) || defined(OS_IOS) || defined(OS_ANDROID)
#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
CoreAccountId::operator std::string() const {
return id;
}
......
......@@ -24,7 +24,7 @@ struct CoreAccountId {
CoreAccountId& operator=(const CoreAccountId&);
CoreAccountId& operator=(CoreAccountId&&) noexcept;
#if defined(OS_CHROMEOS) || defined(OS_IOS) || defined(OS_ANDROID)
#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
// Those implicit constructor and conversion operator allow to
// progressively migrate the code to use this struct. Removing
// them is tracked by https://crbug.com/959161
......
......@@ -139,7 +139,7 @@ class AuthenticationService : public KeyedService,
// is only updated when the app is in foreground and used to detect
// if any change occurred while the app was in background.
// See HaveAccountsChangesWhileInBackground().
std::vector<std::string> GetLastKnownAccountsFromForeground();
std::vector<CoreAccountId> GetLastKnownAccountsFromForeground();
// Returns the cached MDM infos associated with |identity|. If the cache
// is stale for |identity|, the entry might be removed.
......@@ -222,7 +222,7 @@ class AuthenticationService : public KeyedService,
bool is_reloading_credentials_ = false;
// Map between account IDs and their associated MDM error.
mutable std::map<std::string, NSDictionary*> cached_mdm_infos_;
mutable std::map<CoreAccountId, NSDictionary*> cached_mdm_infos_;
ScopedObserver<ios::ChromeIdentityService,
ios::ChromeIdentityService::Observer>
......
......@@ -54,8 +54,9 @@ enum LoginMethodAndSyncState {
constexpr char kFakeAccountIdForRemovedAccount[] = "0000000000000";
// Returns the account id associated with |identity|.
std::string ChromeIdentityToAccountID(signin::IdentityManager* identity_manager,
ChromeIdentity* identity) {
CoreAccountId ChromeIdentityToAccountID(
signin::IdentityManager* identity_manager,
ChromeIdentity* identity) {
std::string gaia_id = base::SysNSStringToUTF8([identity gaiaID]);
auto maybe_account =
identity_manager
......@@ -152,7 +153,7 @@ void AuthenticationService::OnApplicationWillEnterForeground() {
// Clear signin errors on the accounts that had a specific MDM device status.
// This will trigger services to fetch data for these accounts again.
using std::swap;
std::map<std::string, NSDictionary*> cached_mdm_infos;
std::map<CoreAccountId, NSDictionary*> cached_mdm_infos;
swap(cached_mdm_infos_, cached_mdm_infos);
if (!cached_mdm_infos.empty()) {
......@@ -201,7 +202,7 @@ void AuthenticationService::UpdateHaveAccountsChangedWhileInBackground() {
// Load accounts from preference before synchronizing the accounts with
// the system, otherwiser we would never detect any changes to the list
// of accounts.
std::vector<std::string> last_foreground_accounts =
std::vector<CoreAccountId> last_foreground_accounts =
GetLastKnownAccountsFromForeground();
std::sort(last_foreground_accounts.begin(), last_foreground_accounts.end());
......@@ -214,7 +215,7 @@ void AuthenticationService::UpdateHaveAccountsChangedWhileInBackground() {
std::vector<CoreAccountInfo> current_accounts_info =
identity_manager_->GetAccountsWithRefreshTokens();
std::vector<std::string> current_accounts;
std::vector<CoreAccountId> current_accounts;
for (const CoreAccountInfo& account_info : current_accounts_info)
current_accounts.push_back(account_info.account_id);
std::sort(current_accounts.begin(), current_accounts.end());
......@@ -239,11 +240,11 @@ void AuthenticationService::MigrateAccountsStoredInPrefsIfNeeded() {
return;
}
std::vector<std::string> account_ids = GetLastKnownAccountsFromForeground();
std::vector<CoreAccountId> account_ids = GetLastKnownAccountsFromForeground();
std::vector<base::Value> accounts_pref_value;
for (const std::string& account_id : account_ids) {
for (const auto& account_id : account_ids) {
if (identity_manager_->HasAccountWithRefreshToken(account_id)) {
accounts_pref_value.emplace_back(account_id);
accounts_pref_value.emplace_back(account_id.id);
} else {
// The account for |email| was removed since the last application cold
// start. Insert |kFakeAccountIdForRemovedAccount| to ensure
......@@ -268,16 +269,16 @@ void AuthenticationService::StoreKnownAccountsWhileInForeground() {
base::Value(std::move(accounts_pref_value)));
}
std::vector<std::string>
std::vector<CoreAccountId>
AuthenticationService::GetLastKnownAccountsFromForeground() {
const base::Value* accounts_pref =
pref_service_->GetList(prefs::kSigninLastAccounts);
std::vector<std::string> accounts;
std::vector<CoreAccountId> accounts;
for (const auto& value : accounts_pref->GetList()) {
DCHECK(value.is_string());
DCHECK(!value.GetString().empty());
accounts.push_back(value.GetString());
accounts.push_back(CoreAccountId(value.GetString()));
}
return accounts;
}
......
......@@ -65,6 +65,10 @@ std::unique_ptr<KeyedService> BuildMockSyncSetupService(
ProfileSyncServiceFactory::GetForBrowserState(browser_state));
}
CoreAccountId GetAccountId(ChromeIdentity* identity) {
return CoreAccountId(base::SysNSStringToUTF8([identity gaiaID]));
}
} // namespace
class AuthenticationServiceTest : public PlatformTest {
......@@ -112,7 +116,7 @@ class AuthenticationServiceTest : public PlatformTest {
authentication_service()->StoreKnownAccountsWhileInForeground();
}
std::vector<std::string> GetLastKnownAccountsFromForeground() {
std::vector<CoreAccountId> GetLastKnownAccountsFromForeground() {
return authentication_service()->GetLastKnownAccountsFromForeground();
}
......@@ -134,14 +138,13 @@ class AuthenticationServiceTest : public PlatformTest {
}
void SetCachedMDMInfo(ChromeIdentity* identity, NSDictionary* user_info) {
authentication_service()
->cached_mdm_infos_[base::SysNSStringToUTF8([identity gaiaID])] =
authentication_service()->cached_mdm_infos_[GetAccountId(identity)] =
user_info;
}
bool HasCachedMDMInfo(ChromeIdentity* identity) {
return authentication_service()->cached_mdm_infos_.count(
base::SysNSStringToUTF8([identity gaiaID])) > 0;
GetAccountId(identity)) > 0;
}
AuthenticationService* authentication_service() {
......@@ -249,7 +252,7 @@ TEST_F(AuthenticationServiceTest, TestHandleForgottenIdentityPromptSignIn) {
TEST_F(AuthenticationServiceTest, StoreAndGetAccountsInPrefs) {
// Profile starts empty.
std::vector<std::string> accounts = GetLastKnownAccountsFromForeground();
std::vector<CoreAccountId> accounts = GetLastKnownAccountsFromForeground();
EXPECT_TRUE(accounts.empty());
// Sign in.
......@@ -261,8 +264,8 @@ TEST_F(AuthenticationServiceTest, StoreAndGetAccountsInPrefs) {
StoreKnownAccountsWhileInForeground();
accounts = GetLastKnownAccountsFromForeground();
ASSERT_EQ(2u, accounts.size());
EXPECT_EQ("foo2ID", accounts[0]);
EXPECT_EQ("fooID", accounts[1]);
EXPECT_EQ(CoreAccountId("foo2ID"), accounts[0]);
EXPECT_EQ(CoreAccountId("fooID"), accounts[1]);
}
TEST_F(AuthenticationServiceTest,
......@@ -281,8 +284,8 @@ TEST_F(AuthenticationServiceTest,
identity_manager()->GetAccountsWithRefreshTokens();
std::sort(accounts.begin(), accounts.end(), account_compare_func);
ASSERT_EQ(2u, accounts.size());
EXPECT_EQ("foo2ID", accounts[0].account_id);
EXPECT_EQ("fooID", accounts[1].account_id);
EXPECT_EQ(CoreAccountId("foo2ID"), accounts[0].account_id);
EXPECT_EQ(CoreAccountId("fooID"), accounts[1].account_id);
// Simulate a switching to background and back to foreground, triggering a
// credentials reload.
......@@ -294,9 +297,9 @@ TEST_F(AuthenticationServiceTest,
accounts = identity_manager()->GetAccountsWithRefreshTokens();
std::sort(accounts.begin(), accounts.end(), account_compare_func);
ASSERT_EQ(3u, accounts.size());
EXPECT_EQ("foo2ID", accounts[0].account_id);
EXPECT_EQ("foo3ID", accounts[1].account_id);
EXPECT_EQ("fooID", accounts[2].account_id);
EXPECT_EQ(CoreAccountId("foo2ID"), accounts[0].account_id);
EXPECT_EQ(CoreAccountId("foo3ID"), accounts[1].account_id);
EXPECT_EQ(CoreAccountId("fooID"), accounts[2].account_id);
}
TEST_F(AuthenticationServiceTest, HaveAccountsChanged_Default) {
......@@ -429,7 +432,7 @@ TEST_F(AuthenticationServiceTest, MDMErrorsClearedOnForeground) {
GoogleServiceAuthError error(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
signin::UpdatePersistentErrorOfRefreshTokenForAccount(
identity_manager(), base::SysNSStringToUTF8([identity(0) gaiaID]), error);
identity_manager(), GetAccountId(identity(0)), error);
// MDM error for |identity_| is being cleared and the error state of refresh
// token will be updated.
......@@ -482,7 +485,7 @@ TEST_F(AuthenticationServiceTest, HandleMDMNotification) {
GoogleServiceAuthError error(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
signin::UpdatePersistentErrorOfRefreshTokenForAccount(
identity_manager(), base::SysNSStringToUTF8([identity(0) gaiaID]), error);
identity_manager(), GetAccountId(identity(0)), error);
NSDictionary* user_info1 = @{ @"foo" : @1 };
ON_CALL(*identity_service(), GetMDMDeviceStatus(user_info1))
......@@ -518,7 +521,7 @@ TEST_F(AuthenticationServiceTest, HandleMDMBlockedNotification) {
GoogleServiceAuthError error(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
signin::UpdatePersistentErrorOfRefreshTokenForAccount(
identity_manager(), base::SysNSStringToUTF8([identity(0) gaiaID]), error);
identity_manager(), GetAccountId(identity(0)), error);
NSDictionary* user_info1 = @{ @"foo" : @1 };
ON_CALL(*identity_service(), GetMDMDeviceStatus(user_info1))
......@@ -576,7 +579,7 @@ TEST_F(AuthenticationServiceTest, ShowMDMErrorDialog) {
GoogleServiceAuthError error(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
signin::UpdatePersistentErrorOfRefreshTokenForAccount(
identity_manager(), base::SysNSStringToUTF8([identity(0) gaiaID]), error);
identity_manager(), GetAccountId(identity(0)), error);
NSDictionary* user_info = [NSDictionary dictionary];
SetCachedMDMInfo(identity(0), user_info);
......
......@@ -336,9 +336,9 @@ const int64_t kAuthenticationFlowTimeoutSeconds = 10;
- (BOOL)shouldHandleMergeCaseForIdentity:(ChromeIdentity*)identity
browserState:
(ios::ChromeBrowserState*)browserState {
std::string lastSignedInAccountId =
browserState->GetPrefs()->GetString(prefs::kGoogleServicesLastAccountId);
std::string currentSignedInAccountId =
CoreAccountId lastSignedInAccountId(
browserState->GetPrefs()->GetString(prefs::kGoogleServicesLastAccountId));
CoreAccountId currentSignedInAccountId =
IdentityManagerFactory::GetForBrowserState(browserState)
->PickAccountIdForAccount(
base::SysNSStringToUTF8([identity gaiaID]),
......
......@@ -259,7 +259,7 @@ enum AuthenticationState {
int consent_confirmation_id = showAccountsSettings
? openSettingsStringId
: [self acceptSigninButtonStringId];
std::string account_id =
CoreAccountId account_id =
IdentityManagerFactory::GetForBrowserState(self.browserState)
->PickAccountIdForAccount(
base::SysNSStringToUTF8([_selectedIdentity gaiaID]),
......
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