1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_ACCOUNT_ID_ACCOUNT_ID_H_
#define COMPONENTS_ACCOUNT_ID_ACCOUNT_ID_H_
#include <stddef.h>
#include <functional>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include "base/component_export.h"
#include "google_apis/gaia/gaia_id.h"
enum class AccountType {
// Unspecified account (eg. other domains)
UNKNOWN,
// aka Gaia account.
GOOGLE,
// ACTIVE_DIRECTORY account type was deprecated.
};
class AccountIdLiteral;
// Type that contains enough information to identify user.
//
// TODO(alemate): Rename functions and fields to reflect different types of
// accounts. (see crbug.com/672253)
class COMPONENT_EXPORT(COMPONENTS_ACCOUNT_ID) AccountId {
public:
using Literal = AccountIdLiteral;
// Creates an empty account id.
//
// Note: This constructor is public as it is required for mojo serialization
// To create an AccountId object, prefer using the static FromXXXX methods or
// the EmptyAccountId method when creating an empty account id.
AccountId();
AccountId(const AccountId& other);
AccountId& operator=(const AccountId& other);
// If any of the comparable AccountIds has AccountType == UNKNOWN then it
// compares emails.
// If both are not UNKNOWN and not equal then it returns false.
// If AccountType == GOOGLE then it checks if either ids or emails are equal.
bool operator==(const AccountId& other) const;
bool operator<(const AccountId& right) const;
bool empty() const;
bool is_valid() const;
void clear();
AccountType GetAccountType() const;
const GaiaId& GetGaiaId() const;
// Users of AccountId should make no assumptions on the format of email.
// I.e. it cannot be used as account identifier, because it is (in general)
// non-comparable.
const std::string& GetUserEmail() const;
// Returns true if |GetAccountIdKey| would return valid key.
bool HasAccountIdKey() const;
// This returns prefixed some string that can be used as a storage key.
// You should make no assumptions on the format of this string.
const std::string GetAccountIdKey() const;
void SetUserEmail(std::string_view email);
static AccountId FromNonCanonicalEmail(std::string_view email,
const GaiaId& gaia_id,
AccountType account_type);
// This method is to be used during transition period only.
// AccountId with UNKNOWN AccountType;
static AccountId FromUserEmail(std::string_view user_email);
// This method is the preferred way to construct AccountId if you have
// full account information.
// AccountId with GOOGLE AccountType;
static AccountId FromUserEmailGaiaId(std::string_view user_email,
const GaiaId& gaia_id);
// Translation functions between AccountType and std::string. Used for
// serialization.
static AccountType StringToAccountType(std::string_view account_type_string);
static const char* AccountTypeToString(AccountType account_type);
// These are (for now) unstable and cannot be used to store serialized data to
// persistent storage. Only in-memory storage is safe.
// Serialize() returns JSON dictionary,
// Deserialize() restores AccountId after serialization.
std::string Serialize() const;
static std::optional<AccountId> Deserialize(std::string_view serialized);
private:
COMPONENT_EXPORT(COMPONENTS_ACCOUNT_ID)
friend std::ostream& operator<<(std::ostream&, const AccountId&);
AccountId(std::string_view user_email,
AccountType account_type,
const GaiaId& gaia_id);
std::string user_email_;
AccountType account_type_ = AccountType::UNKNOWN;
// ID for AccountType::GOOGLE, empty otherwise.
GaiaId gaia_id_;
};
// Overload << operator to allow logging of AccountIds.
COMPONENT_EXPORT(COMPONENTS_ACCOUNT_ID)
std::ostream& operator<<(std::ostream& stream, const AccountId& account_id);
// Returns a reference to a singleton.
COMPONENT_EXPORT(COMPONENTS_ACCOUNT_ID)
const AccountId& EmptyAccountId();
namespace std {
// Implement hashing of AccountId, so it can be used as a key in STL containers.
template <>
struct hash<AccountId> {
COMPONENT_EXPORT(COMPONENTS_ACCOUNT_ID)
std::size_t operator()(const AccountId& user_id) const;
};
} // namespace std
#endif // COMPONENTS_ACCOUNT_ID_ACCOUNT_ID_H_
|