diff options
author | alemate <alemate@chromium.org> | 2015-10-16 15:35:47 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-10-16 22:36:43 +0000 |
commit | 6b1707c86a8b8f9f0e93dbffc849ed6e500ff43b (patch) | |
tree | 8f18f2d15c8b0ec747bca341980e6fd5815be5c9 /components/signin | |
parent | a553a3bedbf1884bdac60fbc42437bed239c50b5 (diff) | |
download | chromium_src-6b1707c86a8b8f9f0e93dbffc849ed6e500ff43b.zip chromium_src-6b1707c86a8b8f9f0e93dbffc849ed6e500ff43b.tar.gz chromium_src-6b1707c86a8b8f9f0e93dbffc849ed6e500ff43b.tar.bz2 |
This CL adds AccountId object to be used as User Account identifier.
This is part of transition to AccountId.
BUG=468875
TEST=manual
Review URL: https://codereview.chromium.org/1406143002
Cr-Commit-Position: refs/heads/master@{#354624}
Diffstat (limited to 'components/signin')
-rw-r--r-- | components/signin/core/account_id/BUILD.gn | 14 | ||||
-rw-r--r-- | components/signin/core/account_id/DEPS | 2 | ||||
-rw-r--r-- | components/signin/core/account_id/account_id.cc | 128 | ||||
-rw-r--r-- | components/signin/core/account_id/account_id.h | 72 | ||||
-rw-r--r-- | components/signin/core/browser/BUILD.gn | 1 |
5 files changed, 217 insertions, 0 deletions
diff --git a/components/signin/core/account_id/BUILD.gn b/components/signin/core/account_id/BUILD.gn new file mode 100644 index 0000000..2c0b69b --- /dev/null +++ b/components/signin/core/account_id/BUILD.gn @@ -0,0 +1,14 @@ +# Copyright 2014 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. + +source_set("account_id") { + sources = [ + "account_id.cc", + "account_id.h", + ] + + deps = [ + "//base", + ] +} diff --git a/components/signin/core/account_id/DEPS b/components/signin/core/account_id/DEPS new file mode 100644 index 0000000..48e8875 --- /dev/null +++ b/components/signin/core/account_id/DEPS @@ -0,0 +1,2 @@ +include_rules = [ +] diff --git a/components/signin/core/account_id/account_id.cc b/components/signin/core/account_id/account_id.cc new file mode 100644 index 0000000..65b237f --- /dev/null +++ b/components/signin/core/account_id/account_id.cc @@ -0,0 +1,128 @@ +// 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/signin/core/account_id/account_id.h" + +#include <functional> + +#include "base/memory/singleton.h" +#include "google_apis/gaia/gaia_auth_util.h" + +namespace { + +// Known account types. +const char kGoogle[] = "google"; + +struct GoogleStringSingleton { + GoogleStringSingleton() : google(kGoogle) {} + + static GoogleStringSingleton* GetInstance() { + return base::Singleton<GoogleStringSingleton>::get(); + } + + const std::string google; +}; + +} // anonymous namespace + +struct AccountId::EmptyAccountId { + EmptyAccountId() : user_id() {} + const AccountId user_id; + + static EmptyAccountId* GetInstance() { + return base::Singleton<EmptyAccountId>::get(); + } +}; + +AccountId::AccountId() {} + +AccountId::AccountId(const std::string& gaia_id, const std::string& user_email) + : gaia_id_(gaia_id), user_email_(user_email) { + // TODO (alemate): DCHECK(!email.empty()); + // TODO (alemate): check gaia_id is not empty once it is required. +} + +AccountId::AccountId(const AccountId& other) + : gaia_id_(other.gaia_id_), user_email_(other.user_email_) {} + +bool AccountId::operator==(const AccountId& other) const { + return (this == &other) || + (gaia_id_ == other.gaia_id_ && user_email_ == other.user_email_) || + (!gaia_id_.empty() && gaia_id_ == other.gaia_id_) || + (!user_email_.empty() && user_email_ == other.user_email_); +} + +bool AccountId::operator!=(const AccountId& other) const { + return !operator==(other); +} + +bool AccountId::operator<(const AccountId& right) const { + // TODO (alemate): update this once all AccountId members are filled. + return user_email_ < right.user_email_; +} + +bool AccountId::empty() const { + return gaia_id_.empty() && user_email_.empty(); +} + +bool AccountId::is_valid() const { + return /* !gaia_id_.empty() && */ !user_email_.empty(); +} + +void AccountId::clear() { + gaia_id_.clear(); + user_email_.clear(); +} + +const std::string& AccountId::GetAccountType() const { + return GoogleStringSingleton::GetInstance()->google; +} + +const std::string& AccountId::GetGaiaId() const { + return gaia_id_; +} + +const std::string& AccountId::GetUserEmail() const { + return user_email_; +} + +void AccountId::SetGaiaId(const std::string& gaia_id) { + DCHECK(!gaia_id.empty()); + gaia_id_ = gaia_id; +} + +void AccountId::SetUserEmail(const std::string& email) { + DCHECK(!email.empty()); + user_email_ = email; +} + +// static +AccountId AccountId::FromUserEmail(const std::string& email) { + // TODO (alemate): DCHECK(!email.empty()); + return AccountId(std::string() /* gaia_id */, email); +} + +AccountId AccountId::FromGaiaId(const std::string& gaia_id) { + DCHECK(!gaia_id.empty()); + return AccountId(gaia_id, std::string() /* email */); +} + +// static +AccountId AccountId::FromUserEmailGaiaId(const std::string& email, + const std::string& gaia_id) { + DCHECK(!(email.empty() && gaia_id.empty())); + return AccountId(gaia_id, email); +} + +const AccountId& EmptyAccountId() { + return AccountId::EmptyAccountId::GetInstance()->user_id; +} + +namespace BASE_HASH_NAMESPACE { + +std::size_t hash<AccountId>::operator()(const AccountId& user_id) const { + return hash<std::string>()(user_id.GetUserEmail()); +} + +} // namespace BASE_HASH_NAMESPAC diff --git a/components/signin/core/account_id/account_id.h b/components/signin/core/account_id/account_id.h new file mode 100644 index 0000000..73be450 --- /dev/null +++ b/components/signin/core/account_id/account_id.h @@ -0,0 +1,72 @@ +// 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. + +#ifndef COMPONENTS_SIGNIN_CORE_ACCOUNT_ID_ACCOUNT_ID_H_ +#define COMPONENTS_SIGNIN_CORE_ACCOUNT_ID_ACCOUNT_ID_H_ + +#include <string> +#include "base/containers/hash_tables.h" + +// Type that contains enough information to identify user. +// +// TODO (alemate): we are in the process of moving away from std::string as a +// type for storing user identifier to AccountId. At this time GaiaId is mostly +// empty, so this type is used as a replacement for e-mail string. +// But in near future AccountId will become full feature user identifier. +class AccountId { + public: + struct EmptyAccountId; + + AccountId(const AccountId& other); + + bool operator==(const AccountId& other) const; + bool operator!=(const AccountId& other) const; + bool operator<(const AccountId& right) const; + + // empty() is deprecated. Use !is_valid() instead. + bool empty() const; + bool is_valid() const; + void clear(); + + const std::string& GetAccountType() const; + const std::string& GetGaiaId() const; + const std::string& GetUserEmail() const; + + void SetGaiaId(const std::string& gaia_id); + void SetUserEmail(const std::string& email); + + // This method is to be used during transition period only. + static AccountId FromUserEmail(const std::string& user_email); + // This method is to be used during transition period only. + static AccountId FromGaiaId(const std::string& gaia_id); + // This method is the preferred way to construct AccountId if you have + // full account information. + static AccountId FromUserEmailGaiaId(const std::string& user_email, + const std::string& gaia_id); + + // std::string Serialize() const; + // static AccountId Deserialize(const std::string& serialized); + + private: + AccountId(); + AccountId(const std::string& gaia_id, const std::string& user_email); + + std::string gaia_id_; + std::string user_email_; +}; + +// Returns a reference to a singleton. +const AccountId& EmptyAccountId(); + +namespace BASE_HASH_NAMESPACE { + +// Implement hashing of AccountId, so it can be used as a key in STL containers. +template <> +struct hash<AccountId> { + std::size_t operator()(const AccountId& user_id) const; +}; + +} // namespace BASE_HASH_NAMESPACE + +#endif // COMPONENTS_SIGNIN_CORE_ACCOUNT_ID_ACCOUNT_ID_H_ diff --git a/components/signin/core/browser/BUILD.gn b/components/signin/core/browser/BUILD.gn index a7a0157..d27e727 100644 --- a/components/signin/core/browser/BUILD.gn +++ b/components/signin/core/browser/BUILD.gn @@ -82,6 +82,7 @@ source_set("browser") { "//components/metrics", "//components/os_crypt", "//components/webdata/common", + "//components/signin/core/account_id", ] if (is_chromeos) { |