Chromium Code Reviews| Index: components/signin/core/account_id/account_id.cc |
| diff --git a/components/signin/core/account_id/account_id.cc b/components/signin/core/account_id/account_id.cc |
| index 00ceb2e2f52bf9f5225ca76c4af50f99e0f58105..f1502fa12ea4b31e8b52de3676fcde7e84a96e67 100644 |
| --- a/components/signin/core/account_id/account_id.cc |
| +++ b/components/signin/core/account_id/account_id.cc |
| @@ -16,28 +16,21 @@ |
| namespace { |
| -// Known account types. |
| -const char kGoogle[] = "google"; |
| - |
| // Serialization keys |
| const char kGaiaIdKey[] = "gaia_id"; |
| const char kEmailKey[] = "email"; |
| +const char kAccountTypeKey[] = "account_type"; |
| // Prefix for GetAccountIdKey(). |
| const char kKeyGaiaIdPrefix[] = "g-"; |
| - |
| -struct GoogleStringSingleton { |
| - GoogleStringSingleton() : google(kGoogle) {} |
| - |
| - static GoogleStringSingleton* GetInstance() { |
| - return base::Singleton<GoogleStringSingleton>::get(); |
| - } |
| - |
| - const std::string google; |
| -}; |
| +const char kKeyAdIdPrefix[] = "a-"; |
| } // anonymous namespace |
| +const std::string AccountId::kGoogle = "google"; |
| +// TODO(rsorokin): Fix account id after migration. (see crbug.com/668130). |
| +const std::string AccountId::kAd = "ad"; |
| + |
| struct AccountId::EmptyAccountId { |
| EmptyAccountId() : user_id() {} |
| const AccountId user_id; |
| @@ -61,8 +54,15 @@ AccountId::AccountId(const std::string& gaia_id, const std::string& user_email) |
| // TODO(alemate): check gaia_id is not empty once it is required. |
| } |
| +AccountId::AccountId(const std::string& gaia_id, |
| + const std::string& user_email, |
| + const std::string& account_type) |
| + : gaia_id_(gaia_id), user_email_(user_email), account_type_(account_type) {} |
|
Andrew T Wilson (Slow)
2016/11/29 11:05:36
Validate account_type here to make sure it's a val
Roman Sorokin (ftl)
2016/12/02 11:13:10
Done.
|
| + |
| AccountId::AccountId(const AccountId& other) |
| - : gaia_id_(other.gaia_id_), user_email_(other.user_email_) {} |
| + : gaia_id_(other.gaia_id_), |
| + user_email_(other.user_email_), |
| + account_type_(other.account_type_) {} |
| bool AccountId::operator==(const AccountId& other) const { |
| return (this == &other) || |
| @@ -94,7 +94,7 @@ void AccountId::clear() { |
| } |
| const std::string& AccountId::GetAccountType() const { |
| - return GoogleStringSingleton::GetInstance()->google; |
| + return account_type_; |
| } |
| const std::string& AccountId::GetGaiaId() const { |
| @@ -114,7 +114,13 @@ const std::string AccountId::GetAccountIdKey() const { |
| CHECK(!gaia_id_.empty()); |
| #endif |
| - return std::string(kKeyGaiaIdPrefix) + gaia_id_; |
| + if (GetAccountType() == kGoogle) |
| + return std::string(kKeyGaiaIdPrefix) + gaia_id_; |
|
Alexander Alekseev
2016/11/27 08:41:54
nit: split this into several ifs.
Roman Sorokin (ftl)
2016/12/02 11:13:10
Done.
|
| + else if (GetAccountType() == kAd) |
| + return std::string(kKeyAdIdPrefix) + gaia_id_; |
| + else |
|
Alexander Alekseev
2016/11/27 08:41:54
nit: Probably you don't need final "else".
Roman Sorokin (ftl)
2016/12/02 11:13:10
Done.
|
| + LOG(FATAL) << "Unknown account type " << GetAccountType(); |
|
Andrew T Wilson (Slow)
2016/11/29 11:05:36
Agreed, just make this NOTREACHED() << "Unknown ac
Roman Sorokin (ftl)
2016/12/02 11:13:10
Done.
|
| + return std::string(); |
| } |
| void AccountId::SetGaiaId(const std::string& gaia_id) { |
| @@ -127,6 +133,11 @@ void AccountId::SetUserEmail(const std::string& email) { |
| user_email_ = email; |
| } |
| +void AccountId::SetAccountType(const std::string& account_type) { |
| + DCHECK(account_type == kAd || account_type == kGoogle); |
| + account_type_ = account_type; |
| +} |
| + |
| // static |
| AccountId AccountId::FromUserEmail(const std::string& email) { |
| // TODO(alemate): DCHECK(!email.empty()); |
| @@ -145,10 +156,26 @@ AccountId AccountId::FromUserEmailGaiaId(const std::string& email, |
| return AccountId(gaia_id, email); |
| } |
| +AccountId AccountId::FromUserEmailGaiaIdAccountType( |
| + const std::string& email, |
| + const std::string& gaia_id, |
| + const std::string& account_type) { |
| + DCHECK(account_type == kGoogle || account_type == kAd); |
| + DCHECK(!(email.empty() && gaia_id.empty())); |
| + return AccountId(gaia_id, email, account_type); |
| +} |
| + |
| +AccountId AccountId::FromGaiaIdAccountType(const std::string& gaia_id, |
| + const std::string& account_type) { |
| + return FromUserEmailGaiaIdAccountType(std::string() /* email */, gaia_id, |
| + account_type); |
| +} |
| + |
| std::string AccountId::Serialize() const { |
| base::DictionaryValue value; |
| value.SetString(kGaiaIdKey, gaia_id_); |
| value.SetString(kEmailKey, user_email_); |
| + value.SetString(kAccountTypeKey, account_type_); |
| std::string serialized; |
| base::JSONWriter::Write(value, &serialized); |
| @@ -167,6 +194,7 @@ bool AccountId::Deserialize(const std::string& serialized, |
| std::string gaia_id; |
| std::string user_email; |
| + std::string account_type = kGoogle; |
| const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); |
| const bool found_user_email = |
| @@ -181,7 +209,9 @@ bool AccountId::Deserialize(const std::string& serialized, |
| if (!found_gaia_id && !found_user_email) |
| return false; |
| - *account_id = FromUserEmailGaiaId(user_email, gaia_id); |
| + dictionary_value->GetString(kAccountTypeKey, &account_type); |
| + *account_id = |
| + FromUserEmailGaiaIdAccountType(user_email, gaia_id, account_type); |
| return true; |
| } |