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 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/sequence_checker.h" |
Matt Menke | b1732aac | 2022-06-02 13:48:03 | [diff] [blame] | 14 | #include "base/values.h" |
Xiaoling Bao | 9d44237f | 2023-01-18 21:00:27 | [diff] [blame] | 15 | #include "chrome/updater/updater_scope.h" |
S. Ganesh | 73daf2c | 2022-06-14 01:03:55 | [diff] [blame] | 16 | #include "third_party/abseil-cpp/absl/types/optional.h" |
| 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. |
| 37 | // |
| 38 | // This class has sequence affinity. |
| 39 | // |
| 40 | // A mechanism to remove apps or app versions from prefs is needed. |
| 41 | // TODO(sorin): crbug.com/1056450 |
Sorin Jianu | 94f92a1 | 2020-07-22 19:39:33 | [diff] [blame] | 42 | class PersistedData : public base::RefCountedThreadSafe<PersistedData> { |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 43 | public: |
| 44 | // Constructs a provider using the specified |pref_service|. |
| 45 | // The associated preferences are assumed to already be registered. |
| 46 | // The |pref_service| must outlive the instance of this class. |
Xiaoling Bao | 9d44237f | 2023-01-18 21:00:27 | [diff] [blame] | 47 | PersistedData(UpdaterScope scope, PrefService* pref_service); |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 48 | PersistedData(const PersistedData&) = delete; |
| 49 | PersistedData& operator=(const PersistedData&) = delete; |
| 50 | |
| 51 | // These functions access |pv| data for the specified |id|. Returns an empty |
| 52 | // version, if the version is not found. |
| 53 | base::Version GetProductVersion(const std::string& id) const; |
| 54 | void SetProductVersion(const std::string& id, const base::Version& pv); |
| 55 | |
| 56 | // These functions access |fingerprint| data for the specified |id|. |
| 57 | std::string GetFingerprint(const std::string& id) const; |
| 58 | void SetFingerprint(const std::string& id, const std::string& fp); |
| 59 | |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 60 | // These functions access the existence checker path for the specified id. |
| 61 | base::FilePath GetExistenceCheckerPath(const std::string& id) const; |
| 62 | void SetExistenceCheckerPath(const std::string& id, |
| 63 | const base::FilePath& ecp); |
| 64 | |
| 65 | // These functions access the brand code for the specified id. |
| 66 | std::string GetBrandCode(const std::string& id) const; |
| 67 | void SetBrandCode(const std::string& id, const std::string& bc); |
| 68 | |
Xiaoling Bao | bb3afae | 2021-12-03 22:09:32 | [diff] [blame] | 69 | // These functions access the brand path for the specified id. |
| 70 | base::FilePath GetBrandPath(const std::string& id) const; |
| 71 | void SetBrandPath(const std::string& id, const base::FilePath& bp); |
| 72 | |
Joshua Pawlicki | 89132a0 | 2021-10-14 19:53:14 | [diff] [blame] | 73 | // These functions access the AP for the specified id. |
| 74 | std::string GetAP(const std::string& id) const; |
| 75 | void SetAP(const std::string& id, const std::string& ap); |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 76 | |
Xiaoling Bao | 7152ba8 | 2023-03-27 19:19:27 | [diff] [blame^] | 77 | // These functions set the client-regulated-counting data for the specified |
| 78 | // id. The functions are for app migration only. |
| 79 | void SetDateLastActive(const std::string& id, int dla); |
| 80 | void SetDateLastRollcall(const std::string& id, int dlrc); |
| 81 | |
Joshua Pawlicki | 0363e82 | 2023-02-14 17:55:55 | [diff] [blame] | 82 | // This function sets any non-empty field in the registration request object |
| 83 | // into the persistent data store. |
Michael Chang | b15df0e2 | 2020-03-02 22:21:12 | [diff] [blame] | 84 | void RegisterApp(const RegistrationRequest& rq); |
| 85 | |
Michael Chang | 1b930091 | 2020-09-10 17:36:28 | [diff] [blame] | 86 | // This function removes a registered application from the persistent store. |
| 87 | bool RemoveApp(const std::string& id); |
| 88 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 89 | // Returns the app ids of the applications registered in prefs, if the |
| 90 | // application has a valid version. |
| 91 | std::vector<std::string> GetAppIds() const; |
| 92 | |
Joshua Pawlicki | 95356f0 | 2022-01-12 19:45:17 | [diff] [blame] | 93 | // HadApps is set when the updater processes a registration for an app other |
| 94 | // than itself, and is never unset, even if the app is uninstalled. |
| 95 | bool GetHadApps() const; |
| 96 | void SetHadApps(); |
| 97 | |
Joshua Pawlicki | f64b028 | 2023-02-02 17:13:27 | [diff] [blame] | 98 | // UsageStatsEnabled reflects whether the updater as a whole is allowed to |
| 99 | // send usage stats, and is set or reset periodically based on the usage |
| 100 | // stats opt-in state of each product. |
| 101 | bool GetUsageStatsEnabled() const; |
| 102 | void SetUsageStatsEnabled(bool usage_stats_enabled); |
| 103 | |
Sorin Jianu | f5425c32 | 2022-01-29 01:14:27 | [diff] [blame] | 104 | // LastChecked is set when the updater completed successfully a call to |
| 105 | // `UpdateService::UpdateAll` as indicated by the `UpdateService::Result` |
| 106 | // argument of the completion callback. This means that the execution path |
| 107 | // for updating all applications works end to end, including communicating |
| 108 | // with the backend. |
| 109 | base::Time GetLastChecked() const; |
| 110 | void SetLastChecked(const base::Time& time); |
| 111 | |
| 112 | // LastStarted is set when `UpdateService::RunPeriodicTasks` is called. This |
| 113 | // indicates that the mechanism to initiate automated update checks is |
| 114 | // working. |
| 115 | base::Time GetLastStarted() const; |
| 116 | void SetLastStarted(const base::Time& time); |
| 117 | |
S. Ganesh | 73daf2c | 2022-06-14 01:03:55 | [diff] [blame] | 118 | #if BUILDFLAG(IS_WIN) |
| 119 | // Retrieves the previously stored OS version. |
| 120 | absl::optional<OSVERSIONINFOEX> GetLastOSVersion() const; |
| 121 | |
| 122 | // Stores the current os version. |
| 123 | void SetLastOSVersion(); |
| 124 | #endif |
| 125 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 126 | private: |
Sorin Jianu | 94f92a1 | 2020-07-22 19:39:33 | [diff] [blame] | 127 | friend class base::RefCountedThreadSafe<PersistedData>; |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 128 | ~PersistedData(); |
| 129 | |
Joshua Pawlicki | c9616ce | 2020-08-11 17:24:48 | [diff] [blame] | 130 | // Returns nullptr if the app key does not exist. |
Matt Menke | b1732aac | 2022-06-02 13:48:03 | [diff] [blame] | 131 | const base::Value::Dict* GetAppKey(const std::string& id) const; |
Joshua Pawlicki | c9616ce | 2020-08-11 17:24:48 | [diff] [blame] | 132 | |
| 133 | // Returns an existing or newly created app key under a root pref. |
Matt Menke | aee7869 | 2022-09-26 15:45:52 | [diff] [blame] | 134 | base::Value::Dict* GetOrCreateAppKey(const std::string& id, |
| 135 | base::Value::Dict& root); |
Joshua Pawlicki | c9616ce | 2020-08-11 17:24:48 | [diff] [blame] | 136 | |
Xiaoling Bao | 7152ba8 | 2023-03-27 19:19:27 | [diff] [blame^] | 137 | void SetInteger(const std::string& id, const std::string& key, int value); |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 138 | std::string GetString(const std::string& id, const std::string& key) const; |
| 139 | void SetString(const std::string& id, |
| 140 | const std::string& key, |
| 141 | const std::string& value); |
Sorin Jianu | f5425c32 | 2022-01-29 01:14:27 | [diff] [blame] | 142 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 143 | SEQUENCE_CHECKER(sequence_checker_); |
| 144 | |
Xiaoling Bao | 9d44237f | 2023-01-18 21:00:27 | [diff] [blame] | 145 | const UpdaterScope scope_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 146 | raw_ptr<PrefService> pref_service_ = nullptr; // Not owned by this class. |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 147 | }; |
| 148 | |
Joshua Pawlicki | 95356f0 | 2022-01-12 19:45:17 | [diff] [blame] | 149 | void RegisterPersistedDataPrefs(scoped_refptr<PrefRegistrySimple> registry); |
| 150 | |
Sorin Jianu | 2d0c54a | 2020-02-27 21:35:52 | [diff] [blame] | 151 | } // namespace updater |
| 152 | |
| 153 | #endif // CHROME_UPDATER_PERSISTED_DATA_H_ |