account_id.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_ACCOUNT_ID_ACCOUNT_ID_H_
  5. #define COMPONENTS_ACCOUNT_ID_ACCOUNT_ID_H_
  6. #include <stddef.h>
  7. #include <functional>
  8. #include <ostream>
  9. #include <string>
  10. enum class AccountType { UNKNOWN, GOOGLE, ACTIVE_DIRECTORY };
  11. // Type that contains enough information to identify user.
  12. //
  13. // TODO(alemate): we are in the process of moving away from std::string as a
  14. // type for storing user identifier to AccountId. At this time GaiaId is mostly
  15. // empty, so this type is used as a replacement for e-mail string.
  16. // But in near future AccountId will become full feature user identifier.
  17. // TODO(alemate): Rename functions and fields to reflect different types of
  18. // accounts. (see crbug.com/672253)
  19. class AccountId {
  20. public:
  21. // Creates an empty account id.
  22. //
  23. // Note: This constructor is public as it is required for mojo serialization
  24. // To create an AccountId object, prefer using the static FromXXXX methods or
  25. // the EmptyAccountId method when creating an empty account id.
  26. AccountId();
  27. AccountId(const AccountId& other);
  28. AccountId& operator=(const AccountId& other);
  29. // If any of the comparable AccountIds has AccountType == UNKNOWN then it
  30. // compares emails.
  31. // If both are not UNKNOWN and not equal then it returns false.
  32. // If AccountType == GOOGLE then it checks if either ids or emails are equal.
  33. // If AccountType == ACTIVE_DIRECTORY then it checks if ids and emails are
  34. // equal.
  35. bool operator==(const AccountId& other) const;
  36. bool operator!=(const AccountId& other) const;
  37. bool operator<(const AccountId& right) const;
  38. // empty() is deprecated. Use !is_valid() instead.
  39. bool empty() const;
  40. bool is_valid() const;
  41. void clear();
  42. AccountType GetAccountType() const;
  43. const std::string& GetGaiaId() const;
  44. const std::string& GetObjGuid() const;
  45. // Users of AccountId should make no assumptions on the format of email.
  46. // I.e. it cannot be used as account identifier, because it is (in general)
  47. // non-comparable.
  48. const std::string& GetUserEmail() const;
  49. // Returns true if |GetAccountIdKey| would return valid key.
  50. bool HasAccountIdKey() const;
  51. // This returns prefixed some string that can be used as a storage key.
  52. // You should make no assumptions on the format of this string.
  53. const std::string GetAccountIdKey() const;
  54. void SetUserEmail(const std::string& email);
  55. // This method is to be used during transition period only.
  56. // AccountId with UNKNOWN AccountType;
  57. static AccountId FromUserEmail(const std::string& user_email);
  58. // AccountId with GOOGLE AccountType;
  59. // This method is to be used during transition period only.
  60. static AccountId FromGaiaId(const std::string& gaia_id);
  61. // This method is the preferred way to construct AccountId if you have
  62. // full account information.
  63. // AccountId with GOOGLE AccountType;
  64. static AccountId FromUserEmailGaiaId(const std::string& user_email,
  65. const std::string& gaia_id);
  66. // These methods are used to construct Active Directory AccountIds.
  67. // AccountId with ACTIVE_DIRECTORY AccountType;
  68. static AccountId AdFromUserEmailObjGuid(const std::string& email,
  69. const std::string& obj_guid);
  70. // Translation functions between AccountType and std::string. Used for
  71. // serialization.
  72. static AccountType StringToAccountType(
  73. const std::string& account_type_string);
  74. static std::string AccountTypeToString(const AccountType& account_type);
  75. // These are (for now) unstable and cannot be used to store serialized data to
  76. // persistent storage. Only in-memory storage is safe.
  77. // Serialize() returns JSON dictionary,
  78. // Deserialize() restores AccountId after serialization.
  79. std::string Serialize() const;
  80. static bool Deserialize(const std::string& serialized,
  81. AccountId* out_account_id);
  82. private:
  83. friend std::ostream& operator<<(std::ostream&, const AccountId&);
  84. AccountId(const std::string& id,
  85. const std::string& user_email,
  86. const AccountType& account_type);
  87. std::string id_;
  88. std::string user_email_;
  89. AccountType account_type_ = AccountType::UNKNOWN;
  90. };
  91. // Overload << operator to allow logging of AccountIds.
  92. std::ostream& operator<<(std::ostream& stream, const AccountId& account_id);
  93. // Returns a reference to a singleton.
  94. const AccountId& EmptyAccountId();
  95. namespace std {
  96. // Implement hashing of AccountId, so it can be used as a key in STL containers.
  97. template <>
  98. struct hash<AccountId> {
  99. std::size_t operator()(const AccountId& user_id) const;
  100. };
  101. } // namespace std
  102. #endif // COMPONENTS_ACCOUNT_ID_ACCOUNT_ID_H_