fake_device_info_tracker.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "components/sync_device_info/fake_device_info_tracker.h"
  5. #include <map>
  6. #include "base/check.h"
  7. #include "base/notreached.h"
  8. #include "base/ranges/algorithm.h"
  9. #include "components/sync/protocol/sync_enums.pb.h"
  10. #include "components/sync_device_info/device_info.h"
  11. namespace {
  12. // static
  13. std::unique_ptr<syncer::DeviceInfo> CloneDeviceInfo(
  14. const syncer::DeviceInfo& device_info) {
  15. return std::make_unique<syncer::DeviceInfo>(
  16. device_info.guid(), device_info.client_name(),
  17. device_info.chrome_version(), device_info.sync_user_agent(),
  18. device_info.device_type(), device_info.signin_scoped_device_id(),
  19. device_info.manufacturer_name(), device_info.model_name(),
  20. device_info.full_hardware_class(), device_info.last_updated_timestamp(),
  21. device_info.pulse_interval(),
  22. device_info.send_tab_to_self_receiving_enabled(),
  23. device_info.sharing_info(), device_info.paask_info(),
  24. device_info.fcm_registration_token(),
  25. device_info.interested_data_types());
  26. }
  27. } // namespace
  28. namespace syncer {
  29. FakeDeviceInfoTracker::FakeDeviceInfoTracker() = default;
  30. FakeDeviceInfoTracker::~FakeDeviceInfoTracker() = default;
  31. bool FakeDeviceInfoTracker::IsSyncing() const {
  32. return !devices_.empty();
  33. }
  34. std::unique_ptr<DeviceInfo> FakeDeviceInfoTracker::GetDeviceInfo(
  35. const std::string& client_id) const {
  36. for (const DeviceInfo* device : devices_) {
  37. if (device->guid() == client_id) {
  38. return CloneDeviceInfo(*device);
  39. }
  40. }
  41. return nullptr;
  42. }
  43. std::vector<std::unique_ptr<DeviceInfo>>
  44. FakeDeviceInfoTracker::GetAllDeviceInfo() const {
  45. std::vector<std::unique_ptr<DeviceInfo>> list;
  46. for (const DeviceInfo* device : devices_) {
  47. list.push_back(CloneDeviceInfo(*device));
  48. }
  49. return list;
  50. }
  51. void FakeDeviceInfoTracker::AddObserver(Observer* observer) {
  52. observers_.AddObserver(observer);
  53. }
  54. void FakeDeviceInfoTracker::RemoveObserver(Observer* observer) {
  55. observers_.RemoveObserver(observer);
  56. }
  57. std::map<sync_pb::SyncEnums_DeviceType, int>
  58. FakeDeviceInfoTracker::CountActiveDevicesByType() const {
  59. if (device_count_per_type_override_) {
  60. return *device_count_per_type_override_;
  61. }
  62. std::map<sync_pb::SyncEnums_DeviceType, int> count_by_type;
  63. for (const auto* device : devices_) {
  64. count_by_type[device->device_type()]++;
  65. }
  66. return count_by_type;
  67. }
  68. void FakeDeviceInfoTracker::ForcePulseForTest() {
  69. NOTREACHED();
  70. }
  71. bool FakeDeviceInfoTracker::IsRecentLocalCacheGuid(
  72. const std::string& cache_guid) const {
  73. return local_device_cache_guid_ == cache_guid;
  74. }
  75. void FakeDeviceInfoTracker::Add(const DeviceInfo* device) {
  76. devices_.push_back(device);
  77. for (auto& observer : observers_) {
  78. observer.OnDeviceInfoChange();
  79. }
  80. }
  81. void FakeDeviceInfoTracker::Replace(const DeviceInfo* old_device,
  82. const DeviceInfo* new_device) {
  83. std::vector<const DeviceInfo*>::iterator it =
  84. base::ranges::find(devices_, old_device);
  85. DCHECK(devices_.end() != it) << "Tracker doesn't contain device";
  86. *it = new_device;
  87. for (auto& observer : observers_) {
  88. observer.OnDeviceInfoChange();
  89. }
  90. }
  91. void FakeDeviceInfoTracker::OverrideActiveDeviceCount(
  92. const std::map<sync_pb::SyncEnums_DeviceType, int>& counts) {
  93. device_count_per_type_override_ = counts;
  94. for (auto& observer : observers_) {
  95. observer.OnDeviceInfoChange();
  96. }
  97. }
  98. void FakeDeviceInfoTracker::SetLocalCacheGuid(const std::string& cache_guid) {
  99. // ensure that this cache guid is present in the tracker.
  100. DCHECK(GetDeviceInfo(cache_guid));
  101. local_device_cache_guid_ = cache_guid;
  102. }
  103. } // namespace syncer