beacon_seed.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2018 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 "ash/components/multidevice/beacon_seed.h"
  5. #include <algorithm>
  6. #include "base/base64.h"
  7. #include "base/i18n/time_formatting.h"
  8. #include "base/logging.h"
  9. namespace ash::multidevice {
  10. BeaconSeed::BeaconSeed() = default;
  11. BeaconSeed::BeaconSeed(const std::string& data,
  12. base::Time start_time,
  13. base::Time end_time)
  14. : data_(data), start_time_(start_time), end_time_(end_time) {}
  15. BeaconSeed::BeaconSeed(const BeaconSeed& other) = default;
  16. BeaconSeed::~BeaconSeed() = default;
  17. bool BeaconSeed::operator==(const BeaconSeed& other) const {
  18. return data_ == other.data_ && start_time_ == other.start_time_ &&
  19. end_time_ == other.end_time_;
  20. }
  21. BeaconSeed FromCryptAuthSeed(cryptauth::BeaconSeed cryptauth_seed) {
  22. return BeaconSeed(
  23. cryptauth_seed.data(),
  24. base::Time::FromJavaTime(cryptauth_seed.start_time_millis()),
  25. base::Time::FromJavaTime(cryptauth_seed.end_time_millis()));
  26. }
  27. cryptauth::BeaconSeed ToCryptAuthSeed(BeaconSeed multidevice_seed) {
  28. cryptauth::BeaconSeed cryptauth_seed;
  29. cryptauth_seed.set_data(multidevice_seed.data());
  30. cryptauth_seed.set_start_time_millis(
  31. multidevice_seed.start_time().ToJavaTime());
  32. cryptauth_seed.set_end_time_millis(multidevice_seed.end_time().ToJavaTime());
  33. return cryptauth_seed;
  34. }
  35. BeaconSeed FromCryptAuthV2Seed(cryptauthv2::BeaconSeed cryptauth_seed) {
  36. return BeaconSeed(
  37. cryptauth_seed.data(),
  38. base::Time::FromJavaTime(cryptauth_seed.start_time_millis()),
  39. base::Time::FromJavaTime(cryptauth_seed.end_time_millis()));
  40. }
  41. cryptauthv2::BeaconSeed ToCryptAuthV2Seed(BeaconSeed multidevice_seed) {
  42. cryptauthv2::BeaconSeed cryptauth_seed;
  43. cryptauth_seed.set_data(multidevice_seed.data());
  44. cryptauth_seed.set_start_time_millis(
  45. multidevice_seed.start_time().ToJavaTime());
  46. cryptauth_seed.set_end_time_millis(multidevice_seed.end_time().ToJavaTime());
  47. return cryptauth_seed;
  48. }
  49. std::vector<cryptauth::BeaconSeed> ToCryptAuthSeedList(
  50. const std::vector<BeaconSeed>& multidevice_seed_list) {
  51. std::vector<cryptauth::BeaconSeed> cryptauth_beacon_seeds;
  52. std::transform(multidevice_seed_list.begin(), multidevice_seed_list.end(),
  53. std::back_inserter(cryptauth_beacon_seeds),
  54. [](auto multidevice_beacon_seed) {
  55. return ToCryptAuthSeed(multidevice_beacon_seed);
  56. });
  57. return cryptauth_beacon_seeds;
  58. }
  59. std::vector<BeaconSeed> FromCryptAuthSeedList(
  60. const std::vector<cryptauth::BeaconSeed>& cryptauth_seed_list) {
  61. std::vector<BeaconSeed> multidevice_beacon_seeds;
  62. std::transform(cryptauth_seed_list.begin(), cryptauth_seed_list.end(),
  63. std::back_inserter(multidevice_beacon_seeds),
  64. [](auto cryptauth_beacon_seed) {
  65. return FromCryptAuthSeed(cryptauth_beacon_seed);
  66. });
  67. return multidevice_beacon_seeds;
  68. }
  69. std::vector<BeaconSeed> FromCryptAuthV2SeedRepeatedPtrField(
  70. const google::protobuf::RepeatedPtrField<cryptauthv2::BeaconSeed>&
  71. cryptauth_seed_list) {
  72. std::vector<BeaconSeed> multidevice_beacon_seeds;
  73. std::transform(cryptauth_seed_list.begin(), cryptauth_seed_list.end(),
  74. std::back_inserter(multidevice_beacon_seeds),
  75. [](auto cryptauth_beacon_seed) {
  76. return FromCryptAuthV2Seed(cryptauth_beacon_seed);
  77. });
  78. return multidevice_beacon_seeds;
  79. }
  80. std::ostream& operator<<(std::ostream& stream, const BeaconSeed& beacon_seed) {
  81. std::string base_64_data;
  82. base::Base64Encode(beacon_seed.data(), &base_64_data);
  83. stream << "{base_64_data: \"" << base_64_data << "\", start_time: \""
  84. << base::TimeFormatShortDateAndTimeWithTimeZone(
  85. beacon_seed.start_time())
  86. << "\", "
  87. << "end_time: \""
  88. << base::TimeFormatShortDateAndTimeWithTimeZone(beacon_seed.end_time())
  89. << "\"}";
  90. return stream;
  91. }
  92. } // namespace ash::multidevice