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 |
| 64 AccountId::AccountId(const AccountId& other) | 57 AccountId::AccountId(const AccountId& other) |
| 65 : gaia_id_(other.gaia_id_), user_email_(other.user_email_) {} | 58 : gaia_id_(other.gaia_id_), |
| 59 user_email_(other.user_email_), | |
| 60 account_type_(other.account_type_) {} | |
| 66 | 61 |
| 67 bool AccountId::operator==(const AccountId& other) const { | 62 bool AccountId::operator==(const AccountId& other) const { |
| 68 return (this == &other) || | 63 return (this == &other) || |
| 69 (gaia_id_ == other.gaia_id_ && user_email_ == other.user_email_) || | 64 (gaia_id_ == other.gaia_id_ && user_email_ == other.user_email_) || |
| 70 (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || | 65 (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || |
| 71 (!user_email_.empty() && user_email_ == other.user_email_); | 66 (!user_email_.empty() && user_email_ == other.user_email_); |
| 72 } | 67 } |
| 73 | 68 |
| 74 bool AccountId::operator!=(const AccountId& other) const { | 69 bool AccountId::operator!=(const AccountId& other) const { |
| 75 return !operator==(other); | 70 return !operator==(other); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 87 bool AccountId::is_valid() const { | 82 bool AccountId::is_valid() const { |
| 88 return /* !gaia_id_.empty() && */ !user_email_.empty(); | 83 return /* !gaia_id_.empty() && */ !user_email_.empty(); |
| 89 } | 84 } |
| 90 | 85 |
| 91 void AccountId::clear() { | 86 void AccountId::clear() { |
| 92 gaia_id_.clear(); | 87 gaia_id_.clear(); |
| 93 user_email_.clear(); | 88 user_email_.clear(); |
| 94 } | 89 } |
| 95 | 90 |
| 96 const std::string& AccountId::GetAccountType() const { | 91 const std::string& AccountId::GetAccountType() const { |
| 97 return GoogleStringSingleton::GetInstance()->google; | 92 return account_type_; |
| 98 } | 93 } |
| 99 | 94 |
| 100 const std::string& AccountId::GetGaiaId() const { | 95 const std::string& AccountId::GetGaiaId() const { |
| 101 return gaia_id_; | 96 return gaia_id_; |
| 102 } | 97 } |
| 103 | 98 |
| 104 const std::string& AccountId::GetUserEmail() const { | 99 const std::string& AccountId::GetUserEmail() const { |
| 105 return user_email_; | 100 return user_email_; |
| 106 } | 101 } |
| 107 | 102 |
| 108 const std::string AccountId::GetAccountIdKey() const { | 103 const std::string AccountId::GetAccountIdKey() const { |
| 109 #ifdef NDEBUG | 104 #ifdef NDEBUG |
| 110 if (gaia_id_.empty()) | 105 if (gaia_id_.empty()) |
| 111 LOG(FATAL) << "GetAccountIdKey(): no gaia id for " << Serialize(); | 106 LOG(FATAL) << "GetAccountIdKey(): no gaia id for " << Serialize(); |
| 112 | 107 |
| 113 #else | 108 #else |
| 114 CHECK(!gaia_id_.empty()); | 109 CHECK(!gaia_id_.empty()); |
| 115 #endif | 110 #endif |
| 116 | 111 |
| 117 return std::string(kKeyGaiaIdPrefix) + gaia_id_; | 112 if (GetAccountType() == kGoogle) |
| 113 return std::string(kKeyGaiaIdPrefix) + gaia_id_; | |
| 114 else if (GetAccountType() == kAd) | |
| 115 return std::string(kKeyAdIdPrefix) + gaia_id_; | |
| 116 else | |
| 117 LOG(FATAL) << "Unknown account type " << GetAccountType(); | |
| 118 return std::string(); | |
| 118 } | 119 } |
| 119 | 120 |
| 120 void AccountId::SetGaiaId(const std::string& gaia_id) { | 121 void AccountId::SetGaiaId(const std::string& gaia_id) { |
| 121 DCHECK(!gaia_id.empty()); | 122 DCHECK(!gaia_id.empty()); |
| 122 gaia_id_ = gaia_id; | 123 gaia_id_ = gaia_id; |
| 123 } | 124 } |
| 124 | 125 |
| 125 void AccountId::SetUserEmail(const std::string& email) { | 126 void AccountId::SetUserEmail(const std::string& email) { |
| 126 DCHECK(!email.empty()); | 127 DCHECK(!email.empty()); |
| 127 user_email_ = email; | 128 user_email_ = email; |
| 128 } | 129 } |
| 129 | 130 |
| 131 void AccountId::SetAccountType(const std::string& account_type) { | |
| 132 DCHECK(account_type == kAd || account_type == kGoogle); | |
| 133 account_type_ = account_type; | |
| 134 } | |
| 135 | |
| 130 // static | 136 // static |
| 131 AccountId AccountId::FromUserEmail(const std::string& email) { | 137 AccountId AccountId::FromUserEmail(const std::string& email) { |
| 132 // TODO(alemate): DCHECK(!email.empty()); | 138 // TODO(alemate): DCHECK(!email.empty()); |
| 133 return AccountId(std::string() /* gaia_id */, email); | 139 return AccountId(std::string() /* gaia_id */, email); |
| 134 } | 140 } |
| 135 | 141 |
| 136 AccountId AccountId::FromGaiaId(const std::string& gaia_id) { | 142 AccountId AccountId::FromGaiaId(const std::string& gaia_id) { |
| 137 DCHECK(!gaia_id.empty()); | 143 DCHECK(!gaia_id.empty()); |
| 138 return AccountId(gaia_id, std::string() /* email */); | 144 return AccountId(gaia_id, std::string() /* email */); |
| 139 } | 145 } |
| 140 | 146 |
| 141 // static | 147 // static |
| 142 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, | 148 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, |
| 143 const std::string& gaia_id) { | 149 const std::string& gaia_id) { |
| 144 DCHECK(!(email.empty() && gaia_id.empty())); | 150 DCHECK(!(email.empty() && gaia_id.empty())); |
| 145 return AccountId(gaia_id, email); | 151 return AccountId(gaia_id, email); |
| 146 } | 152 } |
| 147 | 153 |
| 148 std::string AccountId::Serialize() const { | 154 std::string AccountId::Serialize() const { |
| 149 base::DictionaryValue value; | 155 base::DictionaryValue value; |
| 150 value.SetString(kGaiaIdKey, gaia_id_); | 156 value.SetString(kGaiaIdKey, gaia_id_); |
| 151 value.SetString(kEmailKey, user_email_); | 157 value.SetString(kEmailKey, user_email_); |
| 158 value.SetString(kAccountTypeKey, account_type_); | |
| 152 | 159 |
| 153 std::string serialized; | 160 std::string serialized; |
| 154 base::JSONWriter::Write(value, &serialized); | 161 base::JSONWriter::Write(value, &serialized); |
| 155 return serialized; | 162 return serialized; |
| 156 } | 163 } |
| 157 | 164 |
| 158 // static | 165 // static |
| 159 bool AccountId::Deserialize(const std::string& serialized, | 166 bool AccountId::Deserialize(const std::string& serialized, |
| 160 AccountId* account_id) { | 167 AccountId* account_id) { |
| 161 base::JSONReader reader; | 168 base::JSONReader reader; |
| 162 std::unique_ptr<const base::Value> value(reader.Read(serialized)); | 169 std::unique_ptr<const base::Value> value(reader.Read(serialized)); |
| 163 const base::DictionaryValue* dictionary_value = NULL; | 170 const base::DictionaryValue* dictionary_value = NULL; |
| 164 | 171 |
| 165 if (!value || !value->GetAsDictionary(&dictionary_value)) | 172 if (!value || !value->GetAsDictionary(&dictionary_value)) |
| 166 return false; | 173 return false; |
| 167 | 174 |
| 168 std::string gaia_id; | 175 std::string gaia_id; |
| 169 std::string user_email; | 176 std::string user_email; |
| 177 std::string account_type = kGoogle; | |
| 170 | 178 |
| 171 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); | 179 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); |
| 172 const bool found_user_email = | 180 const bool found_user_email = |
| 173 dictionary_value->GetString(kEmailKey, &user_email); | 181 dictionary_value->GetString(kEmailKey, &user_email); |
| 174 | 182 |
| 175 if (!found_gaia_id) | 183 if (!found_gaia_id) |
| 176 LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; | 184 LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; |
| 177 | 185 |
| 178 if (!found_user_email) | 186 if (!found_user_email) |
| 179 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; | 187 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; |
| 180 | 188 |
| 181 if (!found_gaia_id && !found_user_email) | 189 if (!found_gaia_id && !found_user_email) |
| 182 return false; | 190 return false; |
| 183 | 191 |
| 184 *account_id = FromUserEmailGaiaId(user_email, gaia_id); | 192 *account_id = FromUserEmailGaiaId(user_email, gaia_id); |
|
Alexander Alekseev
2016/11/24 07:12:40
Could you just add function "FromAccountTypeUserEm
Roman Sorokin (ftl)
2016/12/02 12:35:12
Done.
| |
| 193 if (dictionary_value->GetString(kAccountTypeKey, &account_type)) | |
| 194 account_id->SetAccountType(account_type); | |
| 185 | 195 |
| 186 return true; | 196 return true; |
| 187 } | 197 } |
| 188 | 198 |
| 189 const AccountId& EmptyAccountId() { | 199 const AccountId& EmptyAccountId() { |
| 190 return AccountId::EmptyAccountId::GetInstance()->user_id; | 200 return AccountId::EmptyAccountId::GetInstance()->user_id; |
| 191 } | 201 } |
| 192 | 202 |
| 193 namespace BASE_HASH_NAMESPACE { | 203 namespace BASE_HASH_NAMESPACE { |
| 194 | 204 |
| 195 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { | 205 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { |
| 196 return hash<std::string>()(user_id.GetUserEmail()); | 206 return hash<std::string>()(user_id.GetUserEmail()); |
| 197 } | 207 } |
| 198 | 208 |
| 199 } // namespace BASE_HASH_NAMESPACE | 209 } // namespace BASE_HASH_NAMESPACE |
| OLD | NEW |