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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync_device_info/fake_device_info_tracker.h"
#include <algorithm>
#include <map>
#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "components/sync/protocol/sync_enums.pb.h"
#include "components/sync_device_info/device_info.h"
namespace syncer {
FakeDeviceInfoTracker::FakeDeviceInfoTracker() = default;
FakeDeviceInfoTracker::~FakeDeviceInfoTracker() = default;
bool FakeDeviceInfoTracker::IsSyncing() const {
return !devices_.empty();
}
const DeviceInfo* FakeDeviceInfoTracker::GetDeviceInfo(
const std::string& client_id) const {
for (const DeviceInfo* device : devices_) {
if (device->guid() == client_id) {
return device;
}
}
return nullptr;
}
std::vector<const DeviceInfo*> FakeDeviceInfoTracker::GetAllDeviceInfo() const {
std::vector<const DeviceInfo*> devices;
for (const DeviceInfo* device : devices_) {
devices.push_back(device);
}
return devices;
}
std::vector<const DeviceInfo*> FakeDeviceInfoTracker::GetAllChromeDeviceInfo()
const {
return GetAllDeviceInfo();
}
void FakeDeviceInfoTracker::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void FakeDeviceInfoTracker::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
std::map<DeviceInfo::FormFactor, int>
FakeDeviceInfoTracker::CountActiveDevicesByType() const {
if (device_count_per_type_override_) {
return *device_count_per_type_override_;
}
std::map<DeviceInfo::FormFactor, int> count_by_type;
for (const syncer::DeviceInfo* device : devices_) {
count_by_type[device->form_factor()]++;
}
return count_by_type;
}
void FakeDeviceInfoTracker::ForcePulseForTest() {
NOTREACHED();
}
bool FakeDeviceInfoTracker::IsRecentLocalCacheGuid(
const std::string& cache_guid) const {
return local_device_cache_guid_ == cache_guid;
}
void FakeDeviceInfoTracker::Add(const DeviceInfo* device) {
devices_.push_back(device);
for (auto& observer : observers_) {
observer.OnDeviceInfoChange();
}
}
void FakeDeviceInfoTracker::Add(const std::vector<const DeviceInfo*>& devices) {
for (auto* device : devices) {
devices_.push_back(device);
}
for (auto& observer : observers_) {
observer.OnDeviceInfoChange();
}
}
void FakeDeviceInfoTracker::Add(std::unique_ptr<DeviceInfo> device) {
owned_devices_.push_back(std::move(device));
Add(owned_devices_.back().get());
}
void FakeDeviceInfoTracker::Remove(const DeviceInfo* device) {
const auto to_remove = std::ranges::remove(devices_, device);
CHECK(!to_remove.empty());
devices_.erase(to_remove.begin(), to_remove.end());
}
void FakeDeviceInfoTracker::Replace(const DeviceInfo* old_device,
const DeviceInfo* new_device) {
auto it = std::ranges::find(devices_, old_device);
CHECK(devices_.end() != it) << "Tracker doesn't contain device";
*it = new_device;
for (auto& observer : observers_) {
observer.OnDeviceInfoChange();
}
}
void FakeDeviceInfoTracker::OverrideActiveDeviceCount(
const std::map<DeviceInfo::FormFactor, int>& counts) {
device_count_per_type_override_ = counts;
for (auto& observer : observers_) {
observer.OnDeviceInfoChange();
}
}
void FakeDeviceInfoTracker::SetLocalCacheGuid(const std::string& cache_guid) {
// ensure that this cache guid is present in the tracker.
DCHECK(GetDeviceInfo(cache_guid));
local_device_cache_guid_ = cache_guid;
}
} // namespace syncer
|