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