core_account_id.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2019 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. #include "google_apis/gaia/core_account_id.h"
  5. #include "base/check.h"
  6. #include "google_apis/gaia/gaia_auth_util.h"
  7. namespace {
  8. // Returns whether the string looks like an email (the test is
  9. // crude an only checks whether it includes an '@').
  10. bool IsEmailString(const std::string& string) {
  11. return string.find('@') != std::string::npos;
  12. }
  13. } // anonymous namespace
  14. CoreAccountId::CoreAccountId() = default;
  15. CoreAccountId::CoreAccountId(const CoreAccountId&) = default;
  16. CoreAccountId::CoreAccountId(CoreAccountId&&) noexcept = default;
  17. CoreAccountId::~CoreAccountId() = default;
  18. CoreAccountId& CoreAccountId::operator=(const CoreAccountId&) = default;
  19. CoreAccountId& CoreAccountId::operator=(CoreAccountId&&) noexcept = default;
  20. // static
  21. CoreAccountId CoreAccountId::FromGaiaId(const std::string& gaia_id) {
  22. if (gaia_id.empty())
  23. return CoreAccountId();
  24. DCHECK(!IsEmailString(gaia_id))
  25. << "Expected a Gaia ID and got an email [actual = " << gaia_id << "]";
  26. return CoreAccountId::FromString(gaia_id);
  27. }
  28. // static
  29. CoreAccountId CoreAccountId::FromRobotEmail(const std::string& robot_email) {
  30. if (robot_email.empty())
  31. return CoreAccountId();
  32. DCHECK(gaia::IsGoogleRobotAccountEmail(robot_email))
  33. << "Not a valid robot email [robot_email = " << robot_email << "]";
  34. return CoreAccountId::FromString(robot_email);
  35. }
  36. #if BUILDFLAG(IS_CHROMEOS_ASH)
  37. // static
  38. CoreAccountId CoreAccountId::FromEmail(const std::string& email) {
  39. if (email.empty())
  40. return CoreAccountId();
  41. DCHECK(IsEmailString(email))
  42. << "Expected an email [actual = " << email << "]";
  43. return CoreAccountId::FromString(email);
  44. }
  45. #endif
  46. // static
  47. CoreAccountId CoreAccountId::FromString(const std::string& value) {
  48. CoreAccountId account_id;
  49. account_id.id_ = value;
  50. return account_id;
  51. }
  52. bool CoreAccountId::empty() const {
  53. return id_.empty();
  54. }
  55. bool CoreAccountId::IsEmail() const {
  56. return IsEmailString(id_);
  57. }
  58. const std::string& CoreAccountId::ToString() const {
  59. return id_;
  60. }
  61. bool operator<(const CoreAccountId& lhs, const CoreAccountId& rhs) {
  62. return lhs.ToString() < rhs.ToString();
  63. }
  64. bool operator==(const CoreAccountId& lhs, const CoreAccountId& rhs) {
  65. return lhs.ToString() == rhs.ToString();
  66. }
  67. bool operator!=(const CoreAccountId& lhs, const CoreAccountId& rhs) {
  68. return lhs.ToString() != rhs.ToString();
  69. }
  70. std::ostream& operator<<(std::ostream& out, const CoreAccountId& a) {
  71. return out << a.ToString();
  72. }
  73. std::vector<std::string> ToStringList(
  74. const std::vector<CoreAccountId>& account_ids) {
  75. std::vector<std::string> account_ids_string;
  76. for (const auto& account_id : account_ids)
  77. account_ids_string.push_back(account_id.ToString());
  78. return account_ids_string;
  79. }