Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_UPDATER_PERSISTED_DATA_H_ |
| 6 | #define CHROME_UPDATER_PERSISTED_DATA_H_ |
| 7 | |
Sorin Jianu | a4b3d38 | 2023-11-10 20:08:46 | [diff] [blame^] | 8 | #include <optional> |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "base/sequence_checker.h" |
Matt Menke | b1732aac | 2022-06-02 13:48:03 | [diff] [blame] | 15 | #include "base/values.h" |
Xiaoling Bao | 9d44237f | 2023-01-18 21:00:27 | [diff] [blame] | 16 | #include "chrome/updater/updater_scope.h" |
S. Ganesh | 73daf2c | 2022-06-14 01:03:55 | [diff] [blame] | 17 | |
| 18 | #if BUILDFLAG(IS_WIN) |
| 19 | #include <windows.h> |
| 20 | #endif |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 21 | |
| 22 | class PrefService; |
Joshua Pawlicki | 95356f0 | 2022-01-12 19:45:17 | [diff] [blame] | 23 | class PrefRegistrySimple; |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 24 | |
| 25 | namespace base { |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 26 | class FilePath; |
Sorin Jianu | f5425c32 | 2022-01-29 01:14:27 | [diff] [blame] | 27 | class Time; |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 28 | class Version; |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 29 | } // namespace base |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 30 | |
| 31 | namespace updater { |
| 32 | |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 33 | struct RegistrationRequest; |
| 34 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 35 | // PersistedData uses the PrefService to persist updater data that outlives |
| 36 | // the updater processes. |
Sorin Jianu | 94f92a1 | 2020-07-22 19:39:33 | [diff] [blame] | 37 | class PersistedData : public base::RefCountedThreadSafe<PersistedData> { |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 38 | public: |
| 39 | // Constructs a provider using the specified |pref_service|. |
| 40 | // The associated preferences are assumed to already be registered. |
| 41 | // The |pref_service| must outlive the instance of this class. |
Xiaoling Bao | 9d44237f | 2023-01-18 21:00:27 | [diff] [blame] | 42 | PersistedData(UpdaterScope scope, PrefService* pref_service); |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 43 | PersistedData(const PersistedData&) = delete; |
| 44 | PersistedData& operator=(const PersistedData&) = delete; |
| 45 | |
| 46 | // These functions access |pv| data for the specified |id|. Returns an empty |
| 47 | // version, if the version is not found. |
| 48 | base::Version GetProductVersion(const std::string& id) const; |
| 49 | void SetProductVersion(const std::string& id, const base::Version& pv); |
| 50 | |
Joshua Pawlicki | 44e4fdb | 2023-10-16 19:06:46 | [diff] [blame] | 51 | // These functions access the version path for the specified id. |
| 52 | base::FilePath GetProductVersionPath(const std::string& id) const; |
| 53 | void SetProductVersionPath(const std::string& id, const base::FilePath& path); |
| 54 | |
| 55 | // These functions access the version key for the specified id. |
| 56 | std::string GetProductVersionKey(const std::string& id) const; |
| 57 | void SetProductVersionKey(const std::string& id, const std::string& value); |
| 58 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 59 | // These functions access |fingerprint| data for the specified |id|. |
| 60 | std::string GetFingerprint(const std::string& id) const; |
| 61 | void SetFingerprint(const std::string& id, const std::string& fp); |
| 62 | |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 63 | // These functions access the existence checker path for the specified id. |
| 64 | base::FilePath GetExistenceCheckerPath(const std::string& id) const; |
| 65 | void SetExistenceCheckerPath(const std::string& id, |
| 66 | const base::FilePath& ecp); |
| 67 | |
| 68 | // These functions access the brand code for the specified id. |
| 69 | std::string GetBrandCode(const std::string& id) const; |
| 70 | void SetBrandCode(const std::string& id, const std::string& bc); |
| 71 | |
Xiaoling Bao | bb3afae | 2021-12-03 22:09:32 | [diff] [blame] | 72 | // These functions access the brand path for the specified id. |
| 73 | base::FilePath GetBrandPath(const std::string& id) const; |
| 74 | void SetBrandPath(const std::string& id, const base::FilePath& bp); |
| 75 | |
Joshua Pawlicki | 89132a0 | 2021-10-14 19:53:14 | [diff] [blame] | 76 | // These functions access the AP for the specified id. |
S. Ganesh | aeaff96f | 2023-06-21 17:37:09 | [diff] [blame] | 77 | std::string GetAP(const std::string& id); |
Joshua Pawlicki | 89132a0 | 2021-10-14 19:53:14 | [diff] [blame] | 78 | void SetAP(const std::string& id, const std::string& ap); |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 79 | |
Joshua Pawlicki | 44e4fdb | 2023-10-16 19:06:46 | [diff] [blame] | 80 | // These functions access the AP path for the specified id. |
| 81 | base::FilePath GetAPPath(const std::string& id) const; |
| 82 | void SetAPPath(const std::string& id, const base::FilePath& path); |
| 83 | |
| 84 | // These functions access the AP key for the specified id. |
| 85 | std::string GetAPKey(const std::string& id) const; |
| 86 | void SetAPKey(const std::string& id, const std::string& value); |
| 87 | |
Xiaoling Bao | bf0a516 | 2023-03-29 01:37:24 | [diff] [blame] | 88 | // These functions get/set the client-regulated-counting data for the |
| 89 | // specified id. The functions are for app migration only. |
| 90 | // The getters return nullopt when the persisted data does not have the |
| 91 | // corresponding value, or any node subtype is not expected along the |
| 92 | // path to the target value. |
Sorin Jianu | a4b3d38 | 2023-11-10 20:08:46 | [diff] [blame^] | 93 | std::optional<int> GetDateLastActive(const std::string& id) const; |
Xiaoling Bao | 7152ba8 | 2023-03-27 19:19:27 | [diff] [blame] | 94 | void SetDateLastActive(const std::string& id, int dla); |
Sorin Jianu | a4b3d38 | 2023-11-10 20:08:46 | [diff] [blame^] | 95 | std::optional<int> GetDateLastRollcall(const std::string& id) const; |
Xiaoling Bao | 7152ba8 | 2023-03-27 19:19:27 | [diff] [blame] | 96 | void SetDateLastRollcall(const std::string& id, int dlrc); |
| 97 | |
S. Ganesh | bf05543 | 2023-05-05 22:38:39 | [diff] [blame] | 98 | // These functions access the cohort values for the specified id. |
| 99 | std::string GetCohort(const std::string& id) const; |
| 100 | void SetCohort(const std::string& id, const std::string& cohort); |
| 101 | std::string GetCohortName(const std::string& id) const; |
| 102 | void SetCohortName(const std::string& id, const std::string& cohort_name); |
| 103 | std::string GetCohortHint(const std::string& id) const; |
| 104 | void SetCohortHint(const std::string& id, const std::string& cohort_hint); |
| 105 | |
Joshua Pawlicki | 0363e82 | 2023-02-14 17:55:55 | [diff] [blame] | 106 | // This function sets any non-empty field in the registration request object |
| 107 | // into the persistent data store. |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 108 | void RegisterApp(const RegistrationRequest& rq); |
| 109 | |
Michael Chang | 1b930091 | 2020-09-10 17:36:28 | [diff] [blame] | 110 | // This function removes a registered application from the persistent store. |
| 111 | bool RemoveApp(const std::string& id); |
| 112 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 113 | // Returns the app ids of the applications registered in prefs, if the |
| 114 | // application has a valid version. |
| 115 | std::vector<std::string> GetAppIds() const; |
| 116 | |
Joshua Pawlicki | 95356f0 | 2022-01-12 19:45:17 | [diff] [blame] | 117 | // HadApps is set when the updater processes a registration for an app other |
| 118 | // than itself, and is never unset, even if the app is uninstalled. |
| 119 | bool GetHadApps() const; |
| 120 | void SetHadApps(); |
| 121 | |
Joshua Pawlicki | f64b028 | 2023-02-02 17:13:27 | [diff] [blame] | 122 | // UsageStatsEnabled reflects whether the updater as a whole is allowed to |
| 123 | // send usage stats, and is set or reset periodically based on the usage |
| 124 | // stats opt-in state of each product. |
| 125 | bool GetUsageStatsEnabled() const; |
| 126 | void SetUsageStatsEnabled(bool usage_stats_enabled); |
| 127 | |
Sorin Jianu | f5425c32 | 2022-01-29 01:14:27 | [diff] [blame] | 128 | // LastChecked is set when the updater completed successfully a call to |
| 129 | // `UpdateService::UpdateAll` as indicated by the `UpdateService::Result` |
| 130 | // argument of the completion callback. This means that the execution path |
| 131 | // for updating all applications works end to end, including communicating |
| 132 | // with the backend. |
| 133 | base::Time GetLastChecked() const; |
| 134 | void SetLastChecked(const base::Time& time); |
| 135 | |
| 136 | // LastStarted is set when `UpdateService::RunPeriodicTasks` is called. This |
| 137 | // indicates that the mechanism to initiate automated update checks is |
| 138 | // working. |
| 139 | base::Time GetLastStarted() const; |
| 140 | void SetLastStarted(const base::Time& time); |
| 141 | |
S. Ganesh | 73daf2c | 2022-06-14 01:03:55 | [diff] [blame] | 142 | #if BUILDFLAG(IS_WIN) |
| 143 | // Retrieves the previously stored OS version. |
Sorin Jianu | a4b3d38 | 2023-11-10 20:08:46 | [diff] [blame^] | 144 | std::optional<OSVERSIONINFOEX> GetLastOSVersion() const; |
S. Ganesh | 73daf2c | 2022-06-14 01:03:55 | [diff] [blame] | 145 | |
| 146 | // Stores the current os version. |
| 147 | void SetLastOSVersion(); |
| 148 | #endif |
| 149 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 150 | private: |
Sorin Jianu | 94f92a1 | 2020-07-22 19:39:33 | [diff] [blame] | 151 | friend class base::RefCountedThreadSafe<PersistedData>; |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 152 | ~PersistedData(); |
| 153 | |
Joshua Pawlicki | c9616ce | 2020-08-11 17:24:48 | [diff] [blame] | 154 | // Returns nullptr if the app key does not exist. |
Matt Menke | b1732aac | 2022-06-02 13:48:03 | [diff] [blame] | 155 | const base::Value::Dict* GetAppKey(const std::string& id) const; |
Joshua Pawlicki | c9616ce | 2020-08-11 17:24:48 | [diff] [blame] | 156 | |
| 157 | // Returns an existing or newly created app key under a root pref. |
Matt Menke | aee7869 | 2022-09-26 15:45:52 | [diff] [blame] | 158 | base::Value::Dict* GetOrCreateAppKey(const std::string& id, |
| 159 | base::Value::Dict& root); |
Joshua Pawlicki | c9616ce | 2020-08-11 17:24:48 | [diff] [blame] | 160 | |
Sorin Jianu | a4b3d38 | 2023-11-10 20:08:46 | [diff] [blame^] | 161 | std::optional<int> GetInteger(const std::string& id, |
| 162 | const std::string& key) const; |
Xiaoling Bao | 7152ba8 | 2023-03-27 19:19:27 | [diff] [blame] | 163 | void SetInteger(const std::string& id, const std::string& key, int value); |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 164 | std::string GetString(const std::string& id, const std::string& key) const; |
| 165 | void SetString(const std::string& id, |
| 166 | const std::string& key, |
| 167 | const std::string& value); |
Sorin Jianu | f5425c32 | 2022-01-29 01:14:27 | [diff] [blame] | 168 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 169 | SEQUENCE_CHECKER(sequence_checker_); |
| 170 | |
Xiaoling Bao | 9d44237f | 2023-01-18 21:00:27 | [diff] [blame] | 171 | const UpdaterScope scope_; |
Paul Semel | 89e1f63c | 2023-06-19 13:34:10 | [diff] [blame] | 172 | raw_ptr<PrefService, DanglingUntriaged> pref_service_ = nullptr; |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 173 | }; |
| 174 | |
Joshua Pawlicki | 95356f0 | 2022-01-12 19:45:17 | [diff] [blame] | 175 | void RegisterPersistedDataPrefs(scoped_refptr<PrefRegistrySimple> registry); |
| 176 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 177 | } // namespace updater |
| 178 | |
| 179 | #endif // CHROME_UPDATER_PERSISTED_DATA_H_ |