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..f13b169f4e6e467871eacef3cab2c37c3b550363 100644 |
| --- a/components/signin/core/account_id/account_id.cc |
| +++ b/components/signin/core/account_id/account_id.cc |
| @@ -16,25 +16,19 @@ |
| namespace { |
| -// Known account types. |
| -const char kGoogle[] = "google"; |
| - |
| // Serialization keys |
| const char kGaiaIdKey[] = "gaia_id"; |
| const char kEmailKey[] = "email"; |
| +const char kObjGuid[] = "obj_guid"; |
| +const char kAccountTypeKey[] = "account_type"; |
| + |
| +// Serialization values for account type. |
| +const std::string kGoogle = "google"; |
| +const std::string kAd = "ad"; |
| // 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 |
| @@ -49,26 +43,47 @@ struct AccountId::EmptyAccountId { |
| AccountId::AccountId() {} |
| -AccountId::AccountId(const std::string& gaia_id, const std::string& user_email) |
| - : gaia_id_(gaia_id), user_email_(user_email) { |
| +AccountId::AccountId(const std::string& id, |
| + const std::string& user_email, |
| + const AccountType& account_type) |
| + : id_(id), user_email_(user_email), account_type_(account_type) { |
| + DCHECK(account_type != AccountType::UNKNOWN || id.empty()); |
| + DCHECK(account_type != AccountType::ACTIVE_DIRECTORY || !id.empty()); |
| // Fail if e-mail looks similar to GaiaIdKey. |
| - LOG_ASSERT(!base::StartsWith(user_email, kKeyGaiaIdPrefix, |
| + LOG_ASSERT(account_type != AccountType::GOOGLE || |
| + !base::StartsWith(user_email, kKeyGaiaIdPrefix, |
| base::CompareCase::SENSITIVE) || |
| user_email.find('@') != std::string::npos) |
| - << "Bad e-mail: '" << user_email << "' with gaia_id='" << gaia_id << "'"; |
| + << "Bad e-mail: '" << user_email << "' with gaia_id='" << id << "'"; |
| // TODO(alemate): DCHECK(!email.empty()); |
| // TODO(alemate): check gaia_id is not empty once it is required. |
| } |
| AccountId::AccountId(const AccountId& other) |
| - : gaia_id_(other.gaia_id_), user_email_(other.user_email_) {} |
| + : id_(other.id_), |
| + user_email_(other.user_email_), |
| + account_type_(other.account_type_) {} |
| bool AccountId::operator==(const AccountId& other) const { |
| - return (this == &other) || |
| - (gaia_id_ == other.gaia_id_ && user_email_ == other.user_email_) || |
| - (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || |
| - (!user_email_.empty() && user_email_ == other.user_email_); |
| + if (this == &other) |
| + return true; |
| + if (account_type_ == AccountType::UNKNOWN || |
|
Andrew T Wilson (Slow)
2016/12/15 19:03:24
This behavior is complex enough you really need to
Roman Sorokin (ftl)
2016/12/21 17:15:21
Done.
|
| + other.account_type_ == AccountType::UNKNOWN) |
| + return user_email_ == other.user_email_; |
| + if (account_type_ != other.account_type_) |
| + return false; |
| + switch (account_type_) { |
| + case AccountType::GOOGLE: |
| + return (id_ == other.id_ && user_email_ == other.user_email_) || |
| + (!id_.empty() && id_ == other.id_) || |
| + (!user_email_.empty() && user_email_ == other.user_email_); |
| + case AccountType::ACTIVE_DIRECTORY: |
| + return id_ == other.id_ && user_email_ == other.user_email_; |
| + default: |
| + NOTREACHED() << "Unknown account type"; |
| + } |
| + return false; |
| } |
| bool AccountId::operator!=(const AccountId& other) const { |
| @@ -81,45 +96,70 @@ bool AccountId::operator<(const AccountId& right) const { |
| } |
| bool AccountId::empty() const { |
| - return gaia_id_.empty() && user_email_.empty(); |
| + return id_.empty() && user_email_.empty() && |
| + account_type_ == AccountType::UNKNOWN; |
| } |
| bool AccountId::is_valid() const { |
| - return /* !gaia_id_.empty() && */ !user_email_.empty(); |
| + switch (account_type_) { |
| + case AccountType::GOOGLE: |
| + return /* !id_.empty() && */ !user_email_.empty(); |
| + case AccountType::ACTIVE_DIRECTORY: |
| + return !id_.empty() && !user_email_.empty(); |
| + case AccountType::UNKNOWN: |
| + return id_.empty() && !user_email_.empty(); |
| + } |
| + NOTREACHED(); |
| + return false; |
| } |
| void AccountId::clear() { |
| - gaia_id_.clear(); |
| + id_.clear(); |
| user_email_.clear(); |
| + account_type_ = AccountType::UNKNOWN; |
| } |
| -const std::string& AccountId::GetAccountType() const { |
| - return GoogleStringSingleton::GetInstance()->google; |
| +AccountType AccountId::GetAccountType() const { |
| + return account_type_; |
| } |
| const std::string& AccountId::GetGaiaId() const { |
| - return gaia_id_; |
| + if (account_type_ != AccountType::GOOGLE) |
| + NOTIMPLEMENTED() << "Failed to get gaia_id for non-Google account."; |
| + return id_; |
| +} |
| + |
| +const std::string& AccountId::GetObjGuid() const { |
| + if (account_type_ != AccountType::ACTIVE_DIRECTORY) |
| + NOTIMPLEMENTED() |
| + << "Failed to get obj_guid for non-Active Directory account."; |
| + return id_; |
| } |
| const std::string& AccountId::GetUserEmail() const { |
| return user_email_; |
| } |
| +bool AccountId::HasAccountIdKey() const { |
| + return account_type_ != AccountType::UNKNOWN && !id_.empty(); |
| +} |
| + |
| const std::string AccountId::GetAccountIdKey() const { |
| #ifdef NDEBUG |
| - if (gaia_id_.empty()) |
| - LOG(FATAL) << "GetAccountIdKey(): no gaia id for " << Serialize(); |
| - |
| + if (id_.empty()) |
| + LOG(FATAL) << "GetAccountIdKey(): no id for " << Serialize(); |
| #else |
| - CHECK(!gaia_id_.empty()); |
| + CHECK(!id_.empty()); |
| #endif |
| - |
| - return std::string(kKeyGaiaIdPrefix) + gaia_id_; |
| -} |
| - |
| -void AccountId::SetGaiaId(const std::string& gaia_id) { |
| - DCHECK(!gaia_id.empty()); |
| - gaia_id_ = gaia_id; |
| + switch (GetAccountType()) { |
| + case AccountType::GOOGLE: |
| + return std::string(kKeyGaiaIdPrefix) + id_; |
| + case AccountType::ACTIVE_DIRECTORY: |
| + return std::string(kKeyAdIdPrefix) + id_; |
| + default: |
| + NOTREACHED() << "Unknown account type"; |
| + } |
| + return std::string(); |
| } |
| void AccountId::SetUserEmail(const std::string& email) { |
| @@ -130,24 +170,74 @@ void AccountId::SetUserEmail(const std::string& email) { |
| // static |
| AccountId AccountId::FromUserEmail(const std::string& email) { |
| // TODO(alemate): DCHECK(!email.empty()); |
| - return AccountId(std::string() /* gaia_id */, email); |
| + return AccountId(std::string() /* id */, email, AccountType::UNKNOWN); |
| } |
| +// static |
| AccountId AccountId::FromGaiaId(const std::string& gaia_id) { |
| DCHECK(!gaia_id.empty()); |
| - return AccountId(gaia_id, std::string() /* email */); |
| + return AccountId(gaia_id, std::string() /* email */, AccountType::GOOGLE); |
| } |
| // static |
| AccountId AccountId::FromUserEmailGaiaId(const std::string& email, |
| const std::string& gaia_id) { |
| DCHECK(!(email.empty() && gaia_id.empty())); |
| - return AccountId(gaia_id, email); |
| + return AccountId(gaia_id, email, AccountType::GOOGLE); |
| +} |
| + |
| +// static |
| +AccountId AccountId::AdFromUserEmailObjGuid(const std::string& email, |
| + const std::string& obj_guid) { |
| + DCHECK(!email.empty() && !obj_guid.empty()); |
| + return AccountId(obj_guid, email, AccountType::ACTIVE_DIRECTORY); |
| +} |
| + |
| +// static |
| +AccountId AccountId::AdFromObjGuid(const std::string& obj_guid) { |
| + DCHECK(!obj_guid.empty()); |
| + return AccountId(obj_guid, std::string() /* email */, |
| + AccountType::ACTIVE_DIRECTORY); |
| +} |
| + |
| +// static |
| +AccountType AccountId::StringToAccountType( |
| + const std::string& account_type_string) { |
| + if (account_type_string == kGoogle) |
| + return AccountType::GOOGLE; |
| + if (account_type_string == kAd) |
| + return AccountType::ACTIVE_DIRECTORY; |
| + NOTREACHED() << "Unknown account type " << account_type_string; |
| + return AccountType::UNKNOWN; |
| +} |
| + |
| +// static |
| +std::string AccountId::AccountTypeToString(const AccountType& account_type) { |
| + switch (account_type) { |
| + case AccountType::GOOGLE: |
| + return kGoogle; |
| + case AccountType::ACTIVE_DIRECTORY: |
| + return kAd; |
| + default: |
| + NOTREACHED() << "Unknown account type"; |
| + } |
| + return std::string(); |
| } |
| std::string AccountId::Serialize() const { |
| base::DictionaryValue value; |
| - value.SetString(kGaiaIdKey, gaia_id_); |
| + switch (GetAccountType()) { |
| + case AccountType::UNKNOWN: |
| + case AccountType::GOOGLE: |
| + value.SetString(kGaiaIdKey, id_); |
| + value.SetString(kAccountTypeKey, |
| + AccountTypeToString(AccountType::GOOGLE)); |
| + break; |
| + case AccountType::ACTIVE_DIRECTORY: |
| + value.SetString(kObjGuid, id_); |
| + value.SetString(kAccountTypeKey, AccountTypeToString(GetAccountType())); |
| + break; |
| + } |
| value.SetString(kEmailKey, user_email_); |
| std::string serialized; |
| @@ -167,23 +257,62 @@ bool AccountId::Deserialize(const std::string& serialized, |
| std::string gaia_id; |
| std::string user_email; |
| + std::string obj_guid; |
| + std::string account_type_string; |
| + AccountType account_type = AccountType::GOOGLE; |
| const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); |
| const bool found_user_email = |
| dictionary_value->GetString(kEmailKey, &user_email); |
| - |
| - if (!found_gaia_id) |
| - LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; |
| - |
| - if (!found_user_email) |
| - LOG(ERROR) << "user_email is not found in '" << serialized << "'"; |
| - |
| - if (!found_gaia_id && !found_user_email) |
| - return false; |
| - |
| - *account_id = FromUserEmailGaiaId(user_email, gaia_id); |
| - |
| - return true; |
| + const bool found_obj_guid = dictionary_value->GetString(kObjGuid, &obj_guid); |
| + const bool found_account_type = |
| + dictionary_value->GetString(kAccountTypeKey, &account_type_string); |
| + if (found_account_type) |
| + account_type = StringToAccountType(account_type_string); |
| + |
| + switch (account_type) { |
| + case AccountType::GOOGLE: |
| + if (found_obj_guid) |
| + LOG(ERROR) << "AccountType is 'google' but obj_guid is found in '" |
| + << serialized << "'"; |
| + |
| + if (!found_gaia_id) |
| + LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; |
| + |
| + if (!found_user_email) |
| + LOG(ERROR) << "user_email is not found in '" << serialized << "'"; |
| + |
| + if (!found_gaia_id && !found_user_email) |
| + return false; |
| + |
| + *account_id = FromUserEmailGaiaId(user_email, gaia_id); |
| + return true; |
| + |
| + case AccountType::ACTIVE_DIRECTORY: |
| + if (found_gaia_id) |
| + LOG(ERROR) |
| + << "AccountType is 'active directory' but gaia_id is found in '" |
| + << serialized << "'"; |
| + |
| + if (!found_obj_guid) { |
| + LOG(ERROR) << "obj_guid is not found in '" << serialized << "'"; |
| + return false; |
| + } |
| + |
| + if (!found_user_email) { |
| + LOG(ERROR) << "user_email is not found in '" << serialized << "'"; |
| + } |
| + |
| + if (!found_obj_guid || !found_user_email) |
| + return false; |
| + |
| + *account_id = AdFromUserEmailObjGuid(user_email, obj_guid); |
| + return true; |
| + |
| + default: |
| + NOTREACHED() << "Unknown account type"; |
| + return false; |
| + } |
| } |
| const AccountId& EmptyAccountId() { |