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