Chromium Code Reviews| Index: components/signin/core/account_id/account_id.cc |
| diff --git a/components/signin/core/account_id/account_id.cc b/components/signin/core/account_id/account_id.cc |
| index 65b237fa2758b5c19d604db08dc0259f9b0338c2..7f44c33f5e90faf67c742cedb19860f525af7967 100644 |
| --- a/components/signin/core/account_id/account_id.cc |
| +++ b/components/signin/core/account_id/account_id.cc |
| @@ -6,7 +6,10 @@ |
| #include <functional> |
| +#include "base/json/json_reader.h" |
| +#include "base/json/json_writer.h" |
| #include "base/memory/singleton.h" |
| +#include "base/values.h" |
| #include "google_apis/gaia/gaia_auth_util.h" |
| namespace { |
| @@ -14,6 +17,10 @@ namespace { |
| // Known account types. |
| const char kGoogle[] = "google"; |
| +// Serialization keys |
| +const char kGaiaIdKey[] = "g"; |
| +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.
|
| + |
| struct GoogleStringSingleton { |
| GoogleStringSingleton() : google(kGoogle) {} |
| @@ -115,6 +122,45 @@ AccountId AccountId::FromUserEmailGaiaId(const std::string& email, |
| return AccountId(gaia_id, email); |
| } |
| +std::string AccountId::Serialize() const { |
| + base::DictionaryValue value; |
| + value.SetString(kGaiaIdKey, gaia_id_); |
| + value.SetString(kEmailKey, user_email_); |
| + |
| + std::string serialized; |
| + base::JSONWriter::Write(value, &serialized); |
| + return serialized; |
| +} |
| + |
| +// static |
| +bool AccountId::Deserialize(const std::string& serialized, |
| + AccountId* account_id) { |
| + base::JSONReader reader; |
| + scoped_ptr<const base::Value> value(reader.Read(serialized)); |
| + const base::DictionaryValue* dictionary_value = NULL; |
| + |
| + if (!value || !value->GetAsDictionary(&dictionary_value)) { |
| + return false; |
| + } |
|
stevenjb
2015/11/11 20:39:23
nit: no {}
Alexander Alekseev
2015/11/12 06:53:04
Done.
|
| + |
| + std::string gaia_id; |
| + std::string user_email; |
| + |
| + const bool found_gaia_id = dictionary_value->GetString(kGaiaIdKey, &gaia_id); |
| + const bool found_user_email = |
| + dictionary_value->GetString(kEmailKey, &user_email); |
| + |
| + DCHECK(found_gaia_id); |
| + DCHECK(found_user_email); |
| + |
| + if (!found_gaia_id && !found_user_email) |
| + 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.
|
| + |
| + *account_id = FromUserEmailGaiaId(user_email, gaia_id); |
| + |
| + return true; |
| +} |
| + |
| const AccountId& EmptyAccountId() { |
| return AccountId::EmptyAccountId::GetInstance()->user_id; |
| } |