Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/signin/core/account_id/account_id.h" | 5 #include "components/signin/core/account_id/account_id.h" |
| 6 | 6 |
| 7 #include <functional> | 7 #include <functional> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "google_apis/gaia/gaia_auth_util.h" | 15 #include "google_apis/gaia/gaia_auth_util.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Known account types. | |
| 20 const char kGoogle[] = "google"; | |
| 21 | |
| 22 // Serialization keys | 19 // Serialization keys |
| 23 const char kGaiaIdKey[] = "gaia_id"; | 20 const char kGaiaIdKey[] = "gaia_id"; |
| 24 const char kEmailKey[] = "email"; | 21 const char kEmailKey[] = "email"; |
| 22 const char kAccountTypeKey[] = "account_type"; | |
| 25 | 23 |
| 26 // Prefix for GetAccountIdKey(). | 24 // Prefix for GetAccountIdKey(). |
| 27 const char kKeyGaiaIdPrefix[] = "g-"; | 25 const char kKeyGaiaIdPrefix[] = "g-"; |
| 28 | 26 const char kKeyAdIdPrefix[] = "a-"; |
| 29 struct GoogleStringSingleton { | |
| 30 GoogleStringSingleton() : google(kGoogle) {} | |
| 31 | |
| 32 static GoogleStringSingleton* GetInstance() { | |
| 33 return base::Singleton<GoogleStringSingleton>::get(); | |
| 34 } | |
| 35 | |
| 36 const std::string google; | |
| 37 }; | |
| 38 | 27 |
| 39 } // anonymous namespace | 28 } // anonymous namespace |
| 40 | 29 |
| 30 const std::string AccountId::kGoogle = "google"; | |
| 31 // TODO(rsorokin): Fix account id after migration. (see crbug.com/668130). | |
| 32 const std::string AccountId::kAd = "ad"; | |
| 33 | |
| 41 struct AccountId::EmptyAccountId { | 34 struct AccountId::EmptyAccountId { |
| 42 EmptyAccountId() : user_id() {} | 35 EmptyAccountId() : user_id() {} |
| 43 const AccountId user_id; | 36 const AccountId user_id; |
| 44 | 37 |
| 45 static EmptyAccountId* GetInstance() { | 38 static EmptyAccountId* GetInstance() { |
| 46 return base::Singleton<EmptyAccountId>::get(); | 39 return base::Singleton<EmptyAccountId>::get(); |
| 47 } | 40 } |
| 48 }; | 41 }; |
| 49 | 42 |
| 50 AccountId::AccountId() {} | 43 AccountId::AccountId() {} |
| 51 | 44 |
| 52 AccountId::AccountId(const std::string& gaia_id, const std::string& user_email) | 45 AccountId::AccountId(const std::string& gaia_id, const std::string& user_email) |
| 53 : gaia_id_(gaia_id), user_email_(user_email) { | 46 : gaia_id_(gaia_id), user_email_(user_email) { |
| 54 // Fail if e-mail looks similar to GaiaIdKey. | 47 // Fail if e-mail looks similar to GaiaIdKey. |
| 55 LOG_ASSERT(!base::StartsWith(user_email, kKeyGaiaIdPrefix, | 48 LOG_ASSERT(!base::StartsWith(user_email, kKeyGaiaIdPrefix, |
| 56 base::CompareCase::SENSITIVE) || | 49 base::CompareCase::SENSITIVE) || |
| 57 user_email.find('@') != std::string::npos) | 50 user_email.find('@') != std::string::npos) |
| 58 << "Bad e-mail: '" << user_email << "' with gaia_id='" << gaia_id << "'"; | 51 << "Bad e-mail: '" << user_email << "' with gaia_id='" << gaia_id << "'"; |
| 59 | 52 |
| 60 // TODO(alemate): DCHECK(!email.empty()); | 53 // TODO(alemate): DCHECK(!email.empty()); |
| 61 // TODO(alemate): check gaia_id is not empty once it is required. | 54 // TODO(alemate): check gaia_id is not empty once it is required. |
| 62 } | 55 } |
| 63 | 56 |
| 57 AccountId::AccountId(const std::string& gaia_id, | |
| 58 const std::string& user_email, | |
| 59 const std::string& account_type) | |
| 60 : 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.
| |
| 61 | |
| 64 AccountId::AccountId(const AccountId& other) | 62 AccountId::AccountId(const AccountId& other) |
| 65 : gaia_id_(other.gaia_id_), user_email_(other.user_email_) {} | 63 : gaia_id_(other.gaia_id_), |
| 64 user_email_(other.user_email_), | |
| 65 account_type_(other.account_type_) {} | |
| 66 | 66 |
| 67 bool AccountId::operator==(const AccountId& other) const { | 67 bool AccountId::operator==(const AccountId& other) const { |
| 68 return (this == &other) || | 68 return (this == &other) || |
| 69 (gaia_id_ == other.gaia_id_ && user_email_ == other.user_email_) || | 69 (gaia_id_ == other.gaia_id_ && user_email_ == other.user_email_) || |
| 70 (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || | 70 (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || |
| 71 (!user_email_.empty() && user_email_ == other.user_email_); | 71 (!user_email_.empty() && user_email_ == other.user_email_); |
| 72 } | 72 } |
| 73 | 73 |
| 74 bool AccountId::operator!=(const AccountId& other) const { | 74 bool AccountId::operator!=(const AccountId& other) const { |
| 75 return !operator==(other); | 75 return !operator==(other); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 87 bool AccountId::is_valid() const { | 87 bool AccountId::is_valid() const { |
| 88 return /* !gaia_id_.empty() && */ !user_email_.empty(); | 88 return /* !gaia_id_.empty() && */ !user_email_.empty(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void AccountId::clear() { | 91 void AccountId::clear() { |
| 92 gaia_id_.clear(); | 92 gaia_id_.clear(); |
| 93 user_email_.clear(); | 93 user_email_.clear(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 const std::string& AccountId::GetAccountType() const { | 96 const std::string& AccountId::GetAccountType() const { |
| 97 return GoogleStringSingleton::GetInstance()->google; | 97 return account_type_; |
| 98 } | 98 } |
| 99 | 99 |
| 100 const std::string& AccountId::GetGaiaId() const { | 100 const std::string& AccountId::GetGaiaId() const { |
| 101 return gaia_id_; | 101 return gaia_id_; |
| 102 } | 102 } |
| 103 | 103 |
| 104 const std::string& AccountId::GetUserEmail() const { | 104 const std::string& AccountId::GetUserEmail() const { |
| 105 return user_email_; | 105 return user_email_; |
| 106 } | 106 } |
| 107 | 107 |
| 108 const std::string AccountId::GetAccountIdKey() const { | 108 const std::string AccountId::GetAccountIdKey() const { |
| 109 #ifdef NDEBUG | 109 #ifdef NDEBUG |
| 110 if (gaia_id_.empty()) | 110 if (gaia_id_.empty()) |
| 111 LOG(FATAL) << "GetAccountIdKey(): no gaia id for " << Serialize(); | 111 LOG(FATAL) << "GetAccountIdKey(): no gaia id for " << Serialize(); |
| 112 | 112 |
| 113 #else | 113 #else |
| 114 CHECK(!gaia_id_.empty()); | 114 CHECK(!gaia_id_.empty()); |
| 115 #endif | 115 #endif |
| 116 | 116 |
| 117 return std::string(kKeyGaiaIdPrefix) + gaia_id_; | 117 if (GetAccountType() == kGoogle) |
| 118 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.
| |
| 119 else if (GetAccountType() == kAd) | |
| 120 return std::string(kKeyAdIdPrefix) + gaia_id_; | |
| 121 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.
| |
| 122 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.
| |
| 123 return std::string(); | |
| 118 } | 124 } |
| 119 | 125 |
| 120 void AccountId::SetGaiaId(const std::string& gaia_id) { | 126 void AccountId::SetGaiaId(const std::string& gaia_id) { |
| 121 DCHECK(!gaia_id.empty()); | 127 DCHECK(!gaia_id.empty()); |
| 122 gaia_id_ = gaia_id; | 128 gaia_id_ = gaia_id; |
| 123 } | 129 } |
| 124 | 130 |
| 125 void AccountId::SetUserEmail(const std::string& email) { | 131 void AccountId::SetUserEmail(const std::string& email) { |
| 126 DCHECK(!email.empty()); | 132 DCHECK(!email.empty()); |
| 127 user_email_ = email; | 133 user_email_ = email; |
| 128 } | 134 } |
| 129 | 135 |
| 136 void AccountId::SetAccountType(const std::string& account_type) { | |
| 137 DCHECK(account_type == kAd || account_type == kGoogle); | |
| 138 account_type_ = account_type; | |
| 139 } | |
| 140 | |
| 130 // static | 141 // static |
| 131 AccountId AccountId::FromUserEmail(const std::string& email) { | 142 AccountId AccountId::FromUserEmail(const std::string& email) { |
| 132 // TODO(alemate): DCHECK(!email.empty()); | 143 // TODO(alemate): DCHECK(!email.empty()); |
| 133 return AccountId(std::string() /* gaia_id */, email); | 144 return AccountId(std::string() /* gaia_id */, email); |
| 134 } | 145 } |
| 135 | 146 |
| 136 AccountId AccountId::FromGaiaId(const std::string& gaia_id) { | 147 AccountId AccountId::FromGaiaId(const std::string& gaia_id) { |
| 137 DCHECK(!gaia_id.empty()); | 148 DCHECK(!gaia_id.empty()); |
| 138 return AccountId(gaia_id, std::string() /* email */); | 149 return AccountId(gaia_id, std::string() /* email */); |
| 139 } | 150 } |
| 140 | 151 |
| 141 // static | 152 // static |
| 142 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, | 153 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, |
| 143 const std::string& gaia_id) { | 154 const std::string& gaia_id) { |
| 144 DCHECK(!(email.empty() && gaia_id.empty())); | 155 DCHECK(!(email.empty() && gaia_id.empty())); |
| 145 return AccountId(gaia_id, email); | 156 return AccountId(gaia_id, email); |
| 146 } | 157 } |
| 147 | 158 |
| 159 AccountId AccountId::FromUserEmailGaiaIdAccountType( | |
| 160 const std::string& email, | |
| 161 const std::string& gaia_id, | |
| 162 const std::string& account_type) { | |
| 163 DCHECK(account_type == kGoogle || account_type == kAd); | |
| 164 DCHECK(!(email.empty() && gaia_id.empty())); | |
| 165 return AccountId(gaia_id, email, account_type); | |
| 166 } | |
| 167 | |
| 168 AccountId AccountId::FromGaiaIdAccountType(const std::string& gaia_id, | |
| 169 const std::string& account_type) { | |
| 170 return FromUserEmailGaiaIdAccountType(std::string() /* email */, gaia_id, | |
| 171 account_type); | |
| 172 } | |
| 173 | |
| 148 std::string AccountId::Serialize() const { | 174 std::string AccountId::Serialize() const { |
| 149 base::DictionaryValue value; | 175 base::DictionaryValue value; |
| 150 value.SetString(kGaiaIdKey, gaia_id_); | 176 value.SetString(kGaiaIdKey, gaia_id_); |
| 151 value.SetString(kEmailKey, user_email_); | 177 value.SetString(kEmailKey, user_email_); |
| 178 value.SetString(kAccountTypeKey, account_type_); | |
| 152 | 179 |
| 153 std::string serialized; | 180 std::string serialized; |
| 154 base::JSONWriter::Write(value, &serialized); | 181 base::JSONWriter::Write(value, &serialized); |
| 155 return serialized; | 182 return serialized; |
| 156 } | 183 } |
| 157 | 184 |
| 158 // static | 185 // static |
| 159 bool AccountId::Deserialize(const std::string& serialized, | 186 bool AccountId::Deserialize(const std::string& serialized, |
| 160 AccountId* account_id) { | 187 AccountId* account_id) { |
| 161 base::JSONReader reader; | 188 base::JSONReader reader; |
| 162 std::unique_ptr<const base::Value> value(reader.Read(serialized)); | 189 std::unique_ptr<const base::Value> value(reader.Read(serialized)); |
| 163 const base::DictionaryValue* dictionary_value = NULL; | 190 const base::DictionaryValue* dictionary_value = NULL; |
| 164 | 191 |
| 165 if (!value || !value->GetAsDictionary(&dictionary_value)) | 192 if (!value || !value->GetAsDictionary(&dictionary_value)) |
| 166 return false; | 193 return false; |
| 167 | 194 |
| 168 std::string gaia_id; | 195 std::string gaia_id; |
| 169 std::string user_email; | 196 std::string user_email; |
| 197 std::string account_type = kGoogle; | |
| 170 | 198 |
| 171 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); | 199 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); |
| 172 const bool found_user_email = | 200 const bool found_user_email = |
| 173 dictionary_value->GetString(kEmailKey, &user_email); | 201 dictionary_value->GetString(kEmailKey, &user_email); |
| 174 | 202 |
| 175 if (!found_gaia_id) | 203 if (!found_gaia_id) |
| 176 LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; | 204 LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; |
| 177 | 205 |
| 178 if (!found_user_email) | 206 if (!found_user_email) |
| 179 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; | 207 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; |
| 180 | 208 |
| 181 if (!found_gaia_id && !found_user_email) | 209 if (!found_gaia_id && !found_user_email) |
| 182 return false; | 210 return false; |
| 183 | 211 |
| 184 *account_id = FromUserEmailGaiaId(user_email, gaia_id); | 212 dictionary_value->GetString(kAccountTypeKey, &account_type); |
| 213 *account_id = | |
| 214 FromUserEmailGaiaIdAccountType(user_email, gaia_id, account_type); | |
| 185 | 215 |
| 186 return true; | 216 return true; |
| 187 } | 217 } |
| 188 | 218 |
| 189 const AccountId& EmptyAccountId() { | 219 const AccountId& EmptyAccountId() { |
| 190 return AccountId::EmptyAccountId::GetInstance()->user_id; | 220 return AccountId::EmptyAccountId::GetInstance()->user_id; |
| 191 } | 221 } |
| 192 | 222 |
| 193 namespace BASE_HASH_NAMESPACE { | 223 namespace BASE_HASH_NAMESPACE { |
| 194 | 224 |
| 195 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { | 225 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { |
| 196 return hash<std::string>()(user_id.GetUserEmail()); | 226 return hash<std::string>()(user_id.GetUserEmail()); |
| 197 } | 227 } |
| 198 | 228 |
| 199 } // namespace BASE_HASH_NAMESPACE | 229 } // namespace BASE_HASH_NAMESPACE |
| OLD | NEW |