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[] = "gaia_id"; |
| 22 const char kEmailKey[] = "email"; |
| 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 |
| 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 |
118 const AccountId& EmptyAccountId() { | 166 const AccountId& EmptyAccountId() { |
119 return AccountId::EmptyAccountId::GetInstance()->user_id; | 167 return AccountId::EmptyAccountId::GetInstance()->user_id; |
120 } | 168 } |
121 | 169 |
122 namespace BASE_HASH_NAMESPACE { | 170 namespace BASE_HASH_NAMESPACE { |
123 | 171 |
124 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { | 172 std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { |
125 return hash<std::string>()(user_id.GetUserEmail()); | 173 return hash<std::string>()(user_id.GetUserEmail()); |
126 } | 174 } |
127 | 175 |
128 } // namespace BASE_HASH_NAMESPAC | 176 } // namespace BASE_HASH_NAMESPAC |
OLD | NEW |