Chromium Code Reviews| Index: components/user_manager/user_id.cc |
| diff --git a/components/user_manager/user_id.cc b/components/user_manager/user_id.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9f80c70826e340ee0e1c14b242696a68f642abb2 |
| --- /dev/null |
| +++ b/components/user_manager/user_id.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/user_manager/user_id.h" |
| + |
| +#include "base/memory/singleton.h" |
| +#include "google_apis/gaia/gaia_auth_util.h" |
| + |
| +namespace { |
| + |
| +struct EmptyUserID { |
| + EmptyUserID() : user_id(std::string(), std::string()) {} |
| + const user_manager::UserID user_id; |
| + |
| + static EmptyUserID* GetInstance() { |
| + return Singleton<EmptyUserID>::get(); |
| + } |
| +}; |
| + |
| +} // anonymous namespace |
| + |
| +namespace user_manager { |
| + |
| +UserID::UserID(const std::string& gaia_id, const std::string& user_email) : gaia_id_(gaia_id), user_email_(user_email) {} |
| + |
| +UserID::UserID(const UserID& other) : gaia_id_(other.gaia_id_), user_email_(other.user_email_) {} |
| + |
| +bool UserID::operator==(const UserID& other) const { |
| + return (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || (!user_email_.empty() && user_email_ == other.user_email_); |
|
Denis Kuznetsov (DE-MUC)
2015/06/10 16:50:44
what if our gaia_id_ is not empty, but other.gaia_
|
| +} |
| + |
| +bool UserID::operator!=(const UserID& other) const { |
| + return (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || (!user_email_.empty() && user_email_ == other.user_email_); |
| +} |
| + |
| +bool UserID::operator<(const UserID& right) const { |
| + return user_email_ < right.user_email_; |
|
Denis Kuznetsov (DE-MUC)
2015/06/10 16:50:44
What if e-mails are both empty, but GaiaIDs are di
|
| +} |
| + |
| +bool UserID::empty() const { |
| + return gaia_id_.empty() && user_email_.empty(); |
| +} |
| + |
| +void UserID::clear() { |
| + gaia_id_.clear(); |
| + user_email_.clear(); |
| +} |
| + |
| +const std::string& UserID::GetGaiaId() const { |
| + return gaia_id_; |
| +} |
| + |
| +const std::string& UserID::GetUserEmail() const { |
| + return user_email_; |
| +} |
| + |
| +void UserID::SetGaiaId(const std::string& gaia_id) { |
| + gaia_id_ = gaia_id; |
| +} |
| + |
| +void UserID::SetUserEmail(const std::string& email) { |
| + user_email_ = email; |
| +} |
| + |
| +// static |
| +UserID UserID::FromUserEmail(const std::string& email) { |
| + return UserID(std::string() /* gaia_id */, email); |
| +} |
| + |
| +// static |
| +UserID UserID::Deserialize(const std::string& serialized) { |
| + // If the account_id is an email address, then canonicalize it. |
| + if (serialized.find('@') != std::string::npos) |
| + return UserID(std::string() /* gaia_id */, gaia::CanonicalizeEmail(serialized)); |
| + |
| +#ifdef OS_CHROME |
| + if (serialized == chromeos::login::kLegacyGuestUserName) |
| + return UserID(std::string() /* gaia_id */, gaia::CanonicalizeEmail(serialized)); |
| +#endif |
| + |
| + // This will change once UserID will change. |
| + return UserID(std::string() /* gaia_id */, serialized); |
| +} |
| + |
| +std::string UserID::Serialize() const { |
| + return user_email_; |
| +} |
| + |
| +const UserID& EmptyUserID() { |
| + return EmptyUserID::GetInstance()->user_id; |
| +} |
| + |
| +} // namespace user_manager |
| + |
| +namespace BASE_HASH_NAMESPACE { |
| + |
| +std::size_t |
| +hash<user_manager::UserID>::operator()(const user_manager::UserID& user_id) const { |
| + return std::hash<std::string>()(user_id.GetUserEmail()); |
| +} |
| + |
| +} // namespace BASE_HASH_NAMESPACE |