[go: nahoru, domu]

blob: b21ce1ed652ff679b7d64b8fd8c7dbd5557f2a6e [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2020 The Chromium Authors
Sorin Jianu2d0c54a2020-02-27 21:35:522// 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 Jianua4b3d382023-11-10 20:08:468#include <optional>
Sorin Jianu2d0c54a2020-02-27 21:35:529#include <string>
10#include <vector>
11
Keishi Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
Sorin Jianu2d0c54a2020-02-27 21:35:5213#include "base/memory/ref_counted.h"
14#include "base/sequence_checker.h"
Matt Menkeb1732aac2022-06-02 13:48:0315#include "base/values.h"
Xiaoling Bao9d44237f2023-01-18 21:00:2716#include "chrome/updater/updater_scope.h"
S. Ganesh73daf2c2022-06-14 01:03:5517
18#if BUILDFLAG(IS_WIN)
19#include <windows.h>
20#endif
Sorin Jianu2d0c54a2020-02-27 21:35:5221
22class PrefService;
Joshua Pawlicki95356f02022-01-12 19:45:1723class PrefRegistrySimple;
Sorin Jianu2d0c54a2020-02-27 21:35:5224
25namespace base {
Michael Changb15df0e22020-03-02 22:21:1226class FilePath;
Sorin Jianuf5425c322022-01-29 01:14:2727class Time;
Sorin Jianu2d0c54a2020-02-27 21:35:5228class Version;
Michael Changb15df0e22020-03-02 22:21:1229} // namespace base
Sorin Jianu2d0c54a2020-02-27 21:35:5230
31namespace updater {
32
Michael Changb15df0e22020-03-02 22:21:1233struct RegistrationRequest;
34
Sorin Jianu2d0c54a2020-02-27 21:35:5235// PersistedData uses the PrefService to persist updater data that outlives
36// the updater processes.
Sorin Jianu94f92a12020-07-22 19:39:3337class PersistedData : public base::RefCountedThreadSafe<PersistedData> {
Sorin Jianu2d0c54a2020-02-27 21:35:5238 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 Bao9d44237f2023-01-18 21:00:2742 PersistedData(UpdaterScope scope, PrefService* pref_service);
Sorin Jianu2d0c54a2020-02-27 21:35:5243 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 Pawlicki44e4fdb2023-10-16 19:06:4651 // 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 Jianu2d0c54a2020-02-27 21:35:5259 // 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 Changb15df0e22020-03-02 22:21:1263 // 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 Baobb3afae2021-12-03 22:09:3272 // 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 Pawlicki89132a02021-10-14 19:53:1476 // These functions access the AP for the specified id.
S. Ganeshaeaff96f2023-06-21 17:37:0977 std::string GetAP(const std::string& id);
Joshua Pawlicki89132a02021-10-14 19:53:1478 void SetAP(const std::string& id, const std::string& ap);
Michael Changb15df0e22020-03-02 22:21:1279
Joshua Pawlicki44e4fdb2023-10-16 19:06:4680 // 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 Baobf0a5162023-03-29 01:37:2488 // 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 Jianua4b3d382023-11-10 20:08:4693 std::optional<int> GetDateLastActive(const std::string& id) const;
Xiaoling Bao7152ba82023-03-27 19:19:2794 void SetDateLastActive(const std::string& id, int dla);
Sorin Jianua4b3d382023-11-10 20:08:4695 std::optional<int> GetDateLastRollcall(const std::string& id) const;
Xiaoling Bao7152ba82023-03-27 19:19:2796 void SetDateLastRollcall(const std::string& id, int dlrc);
97
S. Ganeshbf055432023-05-05 22:38:3998 // 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 Pawlicki0363e822023-02-14 17:55:55106 // This function sets any non-empty field in the registration request object
107 // into the persistent data store.
Michael Changb15df0e22020-03-02 22:21:12108 void RegisterApp(const RegistrationRequest& rq);
109
Michael Chang1b9300912020-09-10 17:36:28110 // This function removes a registered application from the persistent store.
111 bool RemoveApp(const std::string& id);
112
Sorin Jianu2d0c54a2020-02-27 21:35:52113 // 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 Pawlicki95356f02022-01-12 19:45:17117 // 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 Pawlickif64b0282023-02-02 17:13:27122 // 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 Jianuf5425c322022-01-29 01:14:27128 // 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. Ganesh73daf2c2022-06-14 01:03:55142#if BUILDFLAG(IS_WIN)
143 // Retrieves the previously stored OS version.
Sorin Jianua4b3d382023-11-10 20:08:46144 std::optional<OSVERSIONINFOEX> GetLastOSVersion() const;
S. Ganesh73daf2c2022-06-14 01:03:55145
146 // Stores the current os version.
147 void SetLastOSVersion();
148#endif
149
Sorin Jianu2d0c54a2020-02-27 21:35:52150 private:
Sorin Jianu94f92a12020-07-22 19:39:33151 friend class base::RefCountedThreadSafe<PersistedData>;
Sorin Jianu2d0c54a2020-02-27 21:35:52152 ~PersistedData();
153
Joshua Pawlickic9616ce2020-08-11 17:24:48154 // Returns nullptr if the app key does not exist.
Matt Menkeb1732aac2022-06-02 13:48:03155 const base::Value::Dict* GetAppKey(const std::string& id) const;
Joshua Pawlickic9616ce2020-08-11 17:24:48156
157 // Returns an existing or newly created app key under a root pref.
Matt Menkeaee78692022-09-26 15:45:52158 base::Value::Dict* GetOrCreateAppKey(const std::string& id,
159 base::Value::Dict& root);
Joshua Pawlickic9616ce2020-08-11 17:24:48160
Sorin Jianua4b3d382023-11-10 20:08:46161 std::optional<int> GetInteger(const std::string& id,
162 const std::string& key) const;
Xiaoling Bao7152ba82023-03-27 19:19:27163 void SetInteger(const std::string& id, const std::string& key, int value);
Sorin Jianu2d0c54a2020-02-27 21:35:52164 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 Jianuf5425c322022-01-29 01:14:27168
Sorin Jianu2d0c54a2020-02-27 21:35:52169 SEQUENCE_CHECKER(sequence_checker_);
170
Xiaoling Bao9d44237f2023-01-18 21:00:27171 const UpdaterScope scope_;
Paul Semel89e1f63c2023-06-19 13:34:10172 raw_ptr<PrefService, DanglingUntriaged> pref_service_ = nullptr;
Sorin Jianu2d0c54a2020-02-27 21:35:52173};
174
Joshua Pawlicki95356f02022-01-12 19:45:17175void RegisterPersistedDataPrefs(scoped_refptr<PrefRegistrySimple> registry);
176
Sorin Jianu2d0c54a2020-02-27 21:35:52177} // namespace updater
178
179#endif // CHROME_UPDATER_PERSISTED_DATA_H_