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 | 8 |
9 #include "base/json/json_reader.h" | |
10 #include "base/json/json_writer.h" | |
11 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
12 #include "base/values.h" | |
13 #include "google_apis/gaia/gaia_auth_util.h" | 10 #include "google_apis/gaia/gaia_auth_util.h" |
14 | 11 |
15 namespace { | 12 namespace { |
16 | 13 |
17 // Known account types. | 14 // Known account types. |
18 const char kGoogle[] = "google"; | 15 const char kGoogle[] = "google"; |
19 | 16 |
20 // Serialization keys | |
21 const char kGaiaIdKey[] = "gaia_id"; | |
22 const char kEmailKey[] = "email"; | |
23 | |
24 struct GoogleStringSingleton { | 17 struct GoogleStringSingleton { |
25 GoogleStringSingleton() : google(kGoogle) {} | 18 GoogleStringSingleton() : google(kGoogle) {} |
26 | 19 |
27 static GoogleStringSingleton* GetInstance() { | 20 static GoogleStringSingleton* GetInstance() { |
28 return base::Singleton<GoogleStringSingleton>::get(); | 21 return base::Singleton<GoogleStringSingleton>::get(); |
29 } | 22 } |
30 | 23 |
31 const std::string google; | 24 const std::string google; |
32 }; | 25 }; |
33 | 26 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 return AccountId(gaia_id, std::string() /* email */); | 108 return AccountId(gaia_id, std::string() /* email */); |
116 } | 109 } |
117 | 110 |
118 // static | 111 // static |
119 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, | 112 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, |
120 const std::string& gaia_id) { | 113 const std::string& gaia_id) { |
121 DCHECK(!(email.empty() && gaia_id.empty())); | 114 DCHECK(!(email.empty() && gaia_id.empty())); |
122 return AccountId(gaia_id, email); | 115 return AccountId(gaia_id, email); |
123 } | 116 } |
124 | 117 |
125 std::string AccountId::Serialize() const { | |
126 base::DictionaryValue value; | |
127 value.SetString(kGaiaIdKey, gaia_id_); | |
128 value.SetString(kEmailKey, user_email_); | |
129 | |
130 std::string serialized; | |
131 base::JSONWriter::Write(value, &serialized); | |
132 return serialized; | |
133 } | |
134 | |
135 // static | |
136 bool AccountId::Deserialize(const std::string& serialized, | |
137 AccountId* account_id) { | |
138 base::JSONReader reader; | |
139 scoped_ptr<const base::Value> value(reader.Read(serialized)); | |
140 const base::DictionaryValue* dictionary_value = NULL; | |
141 | |
142 if (!value || !value->GetAsDictionary(&dictionary_value)) | |
143 return false; | |
144 | |
145 std::string gaia_id; | |
146 std::string user_email; | |
147 | |
148 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); | |
149 const bool found_user_email = | |
150 dictionary_value->GetString(kEmailKey, &user_email); | |
151 | |
152 if (!found_gaia_id) | |
153 LOG(ERROR) << "gaia_id is not found in '" << serialized << "'"; | |
154 | |
155 if (!found_user_email) | |
156 LOG(ERROR) << "user_email is not found in '" << serialized << "'"; | |
157 | |
158 if (!found_gaia_id && !found_user_email) | |
159 return false; | |
160 | |
161 *account_id = FromUserEmailGaiaId(user_email, gaia_id); | |
162 | |
163 return true; | |
164 } | |
165 | |
166 const AccountId& EmptyAccountId() { | 118 const AccountId& EmptyAccountId() { |
167 return AccountId::EmptyAccountId::GetInstance()->user_id; | 119 return AccountId::EmptyAccountId::GetInstance()->user_id; |
168 } | 120 } |
169 | 121 |
170 namespace BASE_HASH_NAMESPACE { | 122 namespace BASE_HASH_NAMESPACE { |
171 | 123 |
172 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { | 124 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { |
173 return hash<std::string>()(user_id.GetUserEmail()); | 125 return hash<std::string>()(user_id.GetUserEmail()); |
174 } | 126 } |
175 | 127 |
176 } // namespace BASE_HASH_NAMESPAC | 128 } // namespace BASE_HASH_NAMESPAC |
OLD | NEW |