Commit 4badd702 authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Prepare //components/sync for Value::GetList() switch

Value::GetList() will soon stop returning references to the underlying
vector. Instead it will return a thin wrapper that only allows iteration
and member access. This change prepares //components/sync* for this
switch.

Bug: 646113
Change-Id: I65cfed19643ddebd1db2dfb159493ab0c039b4a9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1887695
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710985}
parent 1633384e
...@@ -101,15 +101,13 @@ bool DeviceInfoPrefs::IsRecentLocalCacheGuid( ...@@ -101,15 +101,13 @@ bool DeviceInfoPrefs::IsRecentLocalCacheGuid(
void DeviceInfoPrefs::AddLocalCacheGuid(const std::string& cache_guid) { void DeviceInfoPrefs::AddLocalCacheGuid(const std::string& cache_guid) {
ListPrefUpdate update_cache_guids(pref_service_, ListPrefUpdate update_cache_guids(pref_service_,
kDeviceInfoRecentGUIDsWithTimestamps); kDeviceInfoRecentGUIDsWithTimestamps);
base::Value::ListStorage* recent_local_cache_guids =
&(update_cache_guids.Get()->GetList());
for (auto it = recent_local_cache_guids->begin(); for (auto it = update_cache_guids->GetList().begin();
it != recent_local_cache_guids->end(); it++) { it != update_cache_guids->GetList().end(); it++) {
if (MatchesGuidInDictionary(*it, cache_guid)) { if (MatchesGuidInDictionary(*it, cache_guid)) {
// Remove it from the list, to be reinserted below, in the first // Remove it from the list, to be reinserted below, in the first
// position. // position.
recent_local_cache_guids->erase(it); update_cache_guids->EraseListIter(it);
break; break;
} }
} }
...@@ -120,36 +118,25 @@ void DeviceInfoPrefs::AddLocalCacheGuid(const std::string& cache_guid) { ...@@ -120,36 +118,25 @@ void DeviceInfoPrefs::AddLocalCacheGuid(const std::string& cache_guid) {
kTimestampKey, kTimestampKey,
base::Value(clock_->Now().ToDeltaSinceWindowsEpoch().InDays())); base::Value(clock_->Now().ToDeltaSinceWindowsEpoch().InDays()));
recent_local_cache_guids->emplace(recent_local_cache_guids->begin(), update_cache_guids->Insert(update_cache_guids->GetList().begin(),
std::move(new_entry)); std::move(new_entry));
while (recent_local_cache_guids->size() > kMaxLocalCacheGuidsStored) { while (update_cache_guids->GetList().size() > kMaxLocalCacheGuidsStored) {
recent_local_cache_guids->pop_back(); update_cache_guids->EraseListIter(update_cache_guids->GetList().end() - 1);
} }
} }
void DeviceInfoPrefs::GarbageCollectExpiredCacheGuids() { void DeviceInfoPrefs::GarbageCollectExpiredCacheGuids() {
ListPrefUpdate update_cache_guids(pref_service_, ListPrefUpdate update_cache_guids(pref_service_,
kDeviceInfoRecentGUIDsWithTimestamps); kDeviceInfoRecentGUIDsWithTimestamps);
base::Value::ListStorage* recent_local_cache_guids = update_cache_guids->EraseListValueIf([this](const auto& dict) {
&(update_cache_guids.Get())->GetList(); base::Optional<int> days_since_epoch = dict.FindIntKey(kTimestampKey);
for (auto it = recent_local_cache_guids->begin();
it != recent_local_cache_guids->end(); it++) {
base::Optional<int> days_since_epoch = it->FindIntKey(kTimestampKey);
const base::Time creation_time = const base::Time creation_time =
days_since_epoch ? base::Time::FromDeltaSinceWindowsEpoch( days_since_epoch ? base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromDays(*days_since_epoch)) base::TimeDelta::FromDays(*days_since_epoch))
: base::Time::Min(); : base::Time::Min();
if (creation_time < clock_->Now() - kMaxTimeDeltaLocalCacheGuidsStored) { return creation_time < clock_->Now() - kMaxTimeDeltaLocalCacheGuidsStored;
// Because the list of cache_guid prefs is maintained to be in reverse });
// chronological order (from the newest cache guid to the oldest), it
// is safe to remove all of the entries after one that is found to be
// expired.
recent_local_cache_guids->erase(it, recent_local_cache_guids->end());
return;
}
}
} }
} // namespace syncer } // namespace syncer
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/test/simple_test_clock.h" #include "base/test/simple_test_clock.h"
#include "base/values.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/scoped_user_pref_update.h" #include "components/prefs/scoped_user_pref_update.h"
#include "components/prefs/testing_pref_service.h" #include "components/prefs/testing_pref_service.h"
...@@ -31,13 +32,11 @@ TEST_F(DeviceInfoPrefsTest, ShouldMigrateFromObsoletePref) { ...@@ -31,13 +32,11 @@ TEST_F(DeviceInfoPrefsTest, ShouldMigrateFromObsoletePref) {
ListPrefUpdate cache_guids_update(&pref_service_, ListPrefUpdate cache_guids_update(&pref_service_,
kObsoleteDeviceInfoRecentGUIDs); kObsoleteDeviceInfoRecentGUIDs);
base::Value::ListStorage* recent_local_cache_guids =
&cache_guids_update.Get()->GetList();
recent_local_cache_guids->emplace(recent_local_cache_guids->begin(), cache_guids_update->Insert(cache_guids_update->GetList().begin(),
base::Value("old_guid1")); base::Value("old_guid1"));
recent_local_cache_guids->emplace(recent_local_cache_guids->begin(), cache_guids_update->Insert(cache_guids_update->GetList().begin(),
base::Value("old_guid2")); base::Value("old_guid2"));
ASSERT_FALSE(device_info_prefs_.IsRecentLocalCacheGuid("old_guid1")); ASSERT_FALSE(device_info_prefs_.IsRecentLocalCacheGuid("old_guid1"));
ASSERT_FALSE(device_info_prefs_.IsRecentLocalCacheGuid("old_guid2")); ASSERT_FALSE(device_info_prefs_.IsRecentLocalCacheGuid("old_guid2"));
......
...@@ -322,10 +322,9 @@ std::unique_ptr<base::Value> PrefModelAssociator::MergeListValues( ...@@ -322,10 +322,9 @@ std::unique_ptr<base::Value> PrefModelAssociator::MergeListValues(
DCHECK(to_value.type() == base::Value::Type::LIST); DCHECK(to_value.type() == base::Value::Type::LIST);
base::Value result = to_value.Clone(); base::Value result = to_value.Clone();
base::Value::ListStorage& list = result.GetList();
for (const auto& value : from_value.GetList()) { for (const auto& value : from_value.GetList()) {
if (!base::Contains(list, value)) if (!base::Contains(result.GetList(), value))
list.emplace_back(value.Clone()); result.Append(value.Clone());
} }
return base::Value::ToUniquePtrValue(std::move(result)); return base::Value::ToUniquePtrValue(std::move(result));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment