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" | |
9 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "base/values.h" | |
10 #include "google_apis/gaia/gaia_auth_util.h" | 13 #include "google_apis/gaia/gaia_auth_util.h" |
11 | 14 |
12 namespace { | 15 namespace { |
13 | 16 |
14 // Known account types. | 17 // Known account types. |
15 const char kGoogle[] = "google"; | 18 const char kGoogle[] = "google"; |
16 | 19 |
20 // Serialization keys | |
21 const char kGaiaIdKey[] = "g"; | |
22 const char kEmailKey[] = "e"; | |
stevenjb
2015/11/11 20:39:23
nit: Use 'gaia' and 'email' even short term.
Alexander Alekseev
2015/11/12 06:53:04
Done.
| |
23 | |
17 struct GoogleStringSingleton { | 24 struct GoogleStringSingleton { |
18 GoogleStringSingleton() : google(kGoogle) {} | 25 GoogleStringSingleton() : google(kGoogle) {} |
19 | 26 |
20 static GoogleStringSingleton* GetInstance() { | 27 static GoogleStringSingleton* GetInstance() { |
21 return base::Singleton<GoogleStringSingleton>::get(); | 28 return base::Singleton<GoogleStringSingleton>::get(); |
22 } | 29 } |
23 | 30 |
24 const std::string google; | 31 const std::string google; |
25 }; | 32 }; |
26 | 33 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 return AccountId(gaia_id, std::string() /* email */); | 115 return AccountId(gaia_id, std::string() /* email */); |
109 } | 116 } |
110 | 117 |
111 // static | 118 // static |
112 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, | 119 AccountId AccountId::FromUserEmailGaiaId(const std::string& email, |
113 const std::string& gaia_id) { | 120 const std::string& gaia_id) { |
114 DCHECK(!(email.empty() && gaia_id.empty())); | 121 DCHECK(!(email.empty() && gaia_id.empty())); |
115 return AccountId(gaia_id, email); | 122 return AccountId(gaia_id, email); |
116 } | 123 } |
117 | 124 |
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 } | |
stevenjb
2015/11/11 20:39:23
nit: no {}
Alexander Alekseev
2015/11/12 06:53:04
Done.
| |
145 | |
146 std::string gaia_id; | |
147 std::string user_email; | |
148 | |
149 const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); | |
150 const bool found_user_email = | |
151 dictionary_value->GetString(kEmailKey, &user_email); | |
152 | |
153 DCHECK(found_gaia_id); | |
154 DCHECK(found_user_email); | |
155 | |
156 if (!found_gaia_id && !found_user_email) | |
157 return false; | |
stevenjb
2015/11/11 20:39:23
We shouldn't combine DCHECK and if(). Log an error
Alexander Alekseev
2015/11/12 06:53:04
Done.
| |
158 | |
159 *account_id = FromUserEmailGaiaId(user_email, gaia_id); | |
160 | |
161 return true; | |
162 } | |
163 | |
118 const AccountId& EmptyAccountId() { | 164 const AccountId& EmptyAccountId() { |
119 return AccountId::EmptyAccountId::GetInstance()->user_id; | 165 return AccountId::EmptyAccountId::GetInstance()->user_id; |
120 } | 166 } |
121 | 167 |
122 namespace BASE_HASH_NAMESPACE { | 168 namespace BASE_HASH_NAMESPACE { |
123 | 169 |
124 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { | 170 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { |
125 return hash<std::string>()(user_id.GetUserEmail()); | 171 return hash<std::string>()(user_id.GetUserEmail()); |
126 } | 172 } |
127 | 173 |
128 } // namespace BASE_HASH_NAMESPAC | 174 } // namespace BASE_HASH_NAMESPAC |
OLD | NEW |