Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 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 | |
| 5 | #include "components/sync_device_info/device_info_prefs.h" |
| 6 | |
| 7 | #include <algorithm> |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 8 | #include <utility> |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 9 | |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 10 | #include "base/time/clock.h" |
| 11 | #include "base/time/default_clock.h" |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 12 | #include "components/prefs/pref_registry_simple.h" |
| 13 | #include "components/prefs/pref_service.h" |
| 14 | #include "components/prefs/scoped_user_pref_update.h" |
| 15 | |
| 16 | namespace syncer { |
| 17 | namespace { |
| 18 | |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 19 | // Preference name for storing recently used cache GUIDs and their timestamps |
| 20 | // in days since Windows epoch. Most recent first. |
| 21 | const char kDeviceInfoRecentGUIDsWithTimestamps[] = |
| 22 | "sync.local_device_guids_with_timestamp"; |
| 23 | |
| 24 | // Keys used in the dictionaries stored in prefs. |
| 25 | const char kCacheGuidKey[] = "cache_guid"; |
| 26 | const char kTimestampKey[] = "timestamp"; |
| 27 | |
| 28 | // The max time a local device's cached GUIDs will be stored. |
| 29 | constexpr base::TimeDelta kMaxTimeDeltaLocalCacheGuidsStored = |
| 30 | base::TimeDelta::FromDays(10); |
| 31 | |
| 32 | // The max number of local device most recent cached GUIDs that will be stored |
| 33 | // in preferences. |
| 34 | constexpr int kMaxLocalCacheGuidsStored = 30; |
| 35 | |
| 36 | // Returns true iff |dict| is a dictionary with a cache GUID that is equal to |
| 37 | // |cache_guid|. |
| 38 | bool MatchesGuidInDictionary(const base::Value& dict, |
| 39 | const std::string& cache_guid) { |
| 40 | if (!dict.is_dict()) { |
| 41 | return false; |
| 42 | } |
| 43 | const std::string* v_cache_guid = dict.FindStringKey(kCacheGuidKey); |
| 44 | return v_cache_guid && *v_cache_guid == cache_guid; |
| 45 | } |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | // static |
| 50 | void DeviceInfoPrefs::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 51 | registry->RegisterListPref(kDeviceInfoRecentGUIDsWithTimestamps); |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | DeviceInfoPrefs::DeviceInfoPrefs(PrefService* pref_service, |
| 55 | const base::Clock* clock) |
| 56 | : pref_service_(pref_service), clock_(clock) { |
| 57 | DCHECK(pref_service_); |
| 58 | DCHECK(clock_); |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | DeviceInfoPrefs::~DeviceInfoPrefs() {} |
| 62 | |
| 63 | bool DeviceInfoPrefs::IsRecentLocalCacheGuid( |
| 64 | const std::string& cache_guid) const { |
Jan Wilken Dörrie | 8d9034f1 | 2019-11-28 14:48:57 | [diff] [blame] | 65 | base::Value::ConstListView recent_local_cache_guids = |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 66 | pref_service_->GetList(kDeviceInfoRecentGUIDsWithTimestamps)->GetList(); |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 67 | |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 68 | for (const auto& v : recent_local_cache_guids) { |
| 69 | if (MatchesGuidInDictionary(v, cache_guid)) { |
| 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return false; |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void DeviceInfoPrefs::AddLocalCacheGuid(const std::string& cache_guid) { |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 78 | ListPrefUpdate update_cache_guids(pref_service_, |
| 79 | kDeviceInfoRecentGUIDsWithTimestamps); |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 80 | |
Jan Wilken Dörrie | 4badd702 | 2019-10-30 21:36:20 | [diff] [blame] | 81 | for (auto it = update_cache_guids->GetList().begin(); |
| 82 | it != update_cache_guids->GetList().end(); it++) { |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 83 | if (MatchesGuidInDictionary(*it, cache_guid)) { |
| 84 | // Remove it from the list, to be reinserted below, in the first |
| 85 | // position. |
Jan Wilken Dörrie | 4badd702 | 2019-10-30 21:36:20 | [diff] [blame] | 86 | update_cache_guids->EraseListIter(it); |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 87 | break; |
| 88 | } |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 89 | } |
| 90 | |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 91 | base::Value new_entry(base::Value::Type::DICTIONARY); |
| 92 | new_entry.SetKey(kCacheGuidKey, base::Value(cache_guid)); |
| 93 | new_entry.SetKey( |
| 94 | kTimestampKey, |
| 95 | base::Value(clock_->Now().ToDeltaSinceWindowsEpoch().InDays())); |
| 96 | |
Jan Wilken Dörrie | 4badd702 | 2019-10-30 21:36:20 | [diff] [blame] | 97 | update_cache_guids->Insert(update_cache_guids->GetList().begin(), |
| 98 | std::move(new_entry)); |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 99 | |
Jan Wilken Dörrie | 4badd702 | 2019-10-30 21:36:20 | [diff] [blame] | 100 | while (update_cache_guids->GetList().size() > kMaxLocalCacheGuidsStored) { |
| 101 | update_cache_guids->EraseListIter(update_cache_guids->GetList().end() - 1); |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 105 | void DeviceInfoPrefs::GarbageCollectExpiredCacheGuids() { |
| 106 | ListPrefUpdate update_cache_guids(pref_service_, |
| 107 | kDeviceInfoRecentGUIDsWithTimestamps); |
Jan Wilken Dörrie | 4badd702 | 2019-10-30 21:36:20 | [diff] [blame] | 108 | update_cache_guids->EraseListValueIf([this](const auto& dict) { |
Mikel Astiz | d55c5b0 | 2020-04-23 10:40:15 | [diff] [blame] | 109 | // Avoid crashes if the preference contains corrupt entries that are not |
| 110 | // dictionaries, and meanwhile clean up these corrupt entries. |
| 111 | if (!dict.is_dict()) { |
| 112 | return true; |
| 113 | } |
| 114 | |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 115 | absl::optional<int> days_since_epoch = dict.FindIntKey(kTimestampKey); |
Mikel Astiz | d55c5b0 | 2020-04-23 10:40:15 | [diff] [blame] | 116 | |
| 117 | // Avoid crashes if the dictionary contains no timestamp and meanwhile clean |
| 118 | // up these corrupt entries. |
| 119 | if (!days_since_epoch.has_value()) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | const base::Time creation_time = base::Time::FromDeltaSinceWindowsEpoch( |
| 124 | base::TimeDelta::FromDays(*days_since_epoch)); |
Jan Wilken Dörrie | 4badd702 | 2019-10-30 21:36:20 | [diff] [blame] | 125 | return creation_time < clock_->Now() - kMaxTimeDeltaLocalCacheGuidsStored; |
| 126 | }); |
Mikel Astiz | 9637de2 | 2019-10-02 16:10:27 | [diff] [blame] | 127 | } |
| 128 | |
Jeffrey Cohen | 053be24d | 2019-07-23 23:44:05 | [diff] [blame] | 129 | } // namespace syncer |