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