forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_account_id.cc
90 lines (68 loc) · 2.43 KB
/
core_account_id.cc
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
// Copyright 2019 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 "google_apis/gaia/core_account_id.h"
#include "base/check.h"
namespace {
// Returns whether the string looks like an email (the test is
// crude an only checks whether it includes an '@').
bool IsEmailString(const std::string& string) {
return string.find('@') != std::string::npos;
}
} // anonymous namespace
CoreAccountId::CoreAccountId() = default;
CoreAccountId::CoreAccountId(const CoreAccountId&) = default;
CoreAccountId::CoreAccountId(CoreAccountId&&) noexcept = default;
CoreAccountId::~CoreAccountId() = default;
CoreAccountId& CoreAccountId::operator=(const CoreAccountId&) = default;
CoreAccountId& CoreAccountId::operator=(CoreAccountId&&) noexcept = default;
// static
CoreAccountId CoreAccountId::FromGaiaId(const std::string& gaia_id) {
if (gaia_id.empty())
return CoreAccountId();
DCHECK(!IsEmailString(gaia_id))
<< "Expected a Gaia ID and got an email [actual = " << gaia_id << "]";
return CoreAccountId::FromString(gaia_id);
}
// static
CoreAccountId CoreAccountId::FromEmail(const std::string& email) {
if (email.empty())
return CoreAccountId();
DCHECK(IsEmailString(email))
<< "Expected an email [actual = " << email << "]";
return CoreAccountId::FromString(email);
}
// static
CoreAccountId CoreAccountId::FromString(const std::string value) {
CoreAccountId account_id;
account_id.id_ = value;
return account_id;
}
bool CoreAccountId::empty() const {
return id_.empty();
}
bool CoreAccountId::IsEmail() const {
return IsEmailString(id_);
}
const std::string& CoreAccountId::ToString() const {
return id_;
}
bool operator<(const CoreAccountId& lhs, const CoreAccountId& rhs) {
return lhs.ToString() < rhs.ToString();
}
bool operator==(const CoreAccountId& lhs, const CoreAccountId& rhs) {
return lhs.ToString() == rhs.ToString();
}
bool operator!=(const CoreAccountId& lhs, const CoreAccountId& rhs) {
return lhs.ToString() != rhs.ToString();
}
std::ostream& operator<<(std::ostream& out, const CoreAccountId& a) {
return out << a.ToString();
}
std::vector<std::string> ToStringList(
const std::vector<CoreAccountId>& account_ids) {
std::vector<std::string> account_ids_string;
for (const auto& account_id : account_ids)
account_ids_string.push_back(account_id.ToString());
return account_ids_string;
}