[go: nahoru, domu]

blob: 4579a446e74760d0bec41e4564035a37e8dc8ca3 [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
8#include <string>
9#include <vector>
10
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
Sorin Jianu2d0c54a2020-02-27 21:35:5212#include "base/memory/ref_counted.h"
13#include "base/sequence_checker.h"
Matt Menkeb1732aac2022-06-02 13:48:0314#include "base/values.h"
Xiaoling Bao9d44237f2023-01-18 21:00:2715#include "chrome/updater/updater_scope.h"
S. Ganesh73daf2c2022-06-14 01:03:5516#include "third_party/abseil-cpp/absl/types/optional.h"
17
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
51 // These functions access |fingerprint| data for the specified |id|.
52 std::string GetFingerprint(const std::string& id) const;
53 void SetFingerprint(const std::string& id, const std::string& fp);
54
Michael Changb15df0e22020-03-02 22:21:1255 // These functions access the existence checker path for the specified id.
56 base::FilePath GetExistenceCheckerPath(const std::string& id) const;
57 void SetExistenceCheckerPath(const std::string& id,
58 const base::FilePath& ecp);
59
60 // These functions access the brand code for the specified id.
61 std::string GetBrandCode(const std::string& id) const;
62 void SetBrandCode(const std::string& id, const std::string& bc);
63
Xiaoling Baobb3afae2021-12-03 22:09:3264 // These functions access the brand path for the specified id.
65 base::FilePath GetBrandPath(const std::string& id) const;
66 void SetBrandPath(const std::string& id, const base::FilePath& bp);
67
Joshua Pawlicki89132a02021-10-14 19:53:1468 // These functions access the AP for the specified id.
69 std::string GetAP(const std::string& id) const;
70 void SetAP(const std::string& id, const std::string& ap);
Michael Changb15df0e22020-03-02 22:21:1271
Xiaoling Baobf0a5162023-03-29 01:37:2472 // These functions get/set the client-regulated-counting data for the
73 // specified id. The functions are for app migration only.
74 // The getters return nullopt when the persisted data does not have the
75 // corresponding value, or any node subtype is not expected along the
76 // path to the target value.
77 absl::optional<int> GetDateLastActive(const std::string& id) const;
Xiaoling Bao7152ba82023-03-27 19:19:2778 void SetDateLastActive(const std::string& id, int dla);
Xiaoling Baobf0a5162023-03-29 01:37:2479 absl::optional<int> GetDateLastRollcall(const std::string& id) const;
Xiaoling Bao7152ba82023-03-27 19:19:2780 void SetDateLastRollcall(const std::string& id, int dlrc);
81
S. Ganeshbf055432023-05-05 22:38:3982 // These functions access the cohort values for the specified id.
83 std::string GetCohort(const std::string& id) const;
84 void SetCohort(const std::string& id, const std::string& cohort);
85 std::string GetCohortName(const std::string& id) const;
86 void SetCohortName(const std::string& id, const std::string& cohort_name);
87 std::string GetCohortHint(const std::string& id) const;
88 void SetCohortHint(const std::string& id, const std::string& cohort_hint);
89
Joshua Pawlicki0363e822023-02-14 17:55:5590 // This function sets any non-empty field in the registration request object
91 // into the persistent data store.
Michael Changb15df0e22020-03-02 22:21:1292 void RegisterApp(const RegistrationRequest& rq);
93
Michael Chang1b9300912020-09-10 17:36:2894 // This function removes a registered application from the persistent store.
95 bool RemoveApp(const std::string& id);
96
Sorin Jianu2d0c54a2020-02-27 21:35:5297 // Returns the app ids of the applications registered in prefs, if the
98 // application has a valid version.
99 std::vector<std::string> GetAppIds() const;
100
Joshua Pawlicki95356f02022-01-12 19:45:17101 // HadApps is set when the updater processes a registration for an app other
102 // than itself, and is never unset, even if the app is uninstalled.
103 bool GetHadApps() const;
104 void SetHadApps();
105
Joshua Pawlickif64b0282023-02-02 17:13:27106 // UsageStatsEnabled reflects whether the updater as a whole is allowed to
107 // send usage stats, and is set or reset periodically based on the usage
108 // stats opt-in state of each product.
109 bool GetUsageStatsEnabled() const;
110 void SetUsageStatsEnabled(bool usage_stats_enabled);
111
Sorin Jianuf5425c322022-01-29 01:14:27112 // LastChecked is set when the updater completed successfully a call to
113 // `UpdateService::UpdateAll` as indicated by the `UpdateService::Result`
114 // argument of the completion callback. This means that the execution path
115 // for updating all applications works end to end, including communicating
116 // with the backend.
117 base::Time GetLastChecked() const;
118 void SetLastChecked(const base::Time& time);
119
120 // LastStarted is set when `UpdateService::RunPeriodicTasks` is called. This
121 // indicates that the mechanism to initiate automated update checks is
122 // working.
123 base::Time GetLastStarted() const;
124 void SetLastStarted(const base::Time& time);
125
S. Ganesh73daf2c2022-06-14 01:03:55126#if BUILDFLAG(IS_WIN)
127 // Retrieves the previously stored OS version.
128 absl::optional<OSVERSIONINFOEX> GetLastOSVersion() const;
129
130 // Stores the current os version.
131 void SetLastOSVersion();
132#endif
133
Sorin Jianu2d0c54a2020-02-27 21:35:52134 private:
Sorin Jianu94f92a12020-07-22 19:39:33135 friend class base::RefCountedThreadSafe<PersistedData>;
Sorin Jianu2d0c54a2020-02-27 21:35:52136 ~PersistedData();
137
Joshua Pawlickic9616ce2020-08-11 17:24:48138 // Returns nullptr if the app key does not exist.
Matt Menkeb1732aac2022-06-02 13:48:03139 const base::Value::Dict* GetAppKey(const std::string& id) const;
Joshua Pawlickic9616ce2020-08-11 17:24:48140
141 // Returns an existing or newly created app key under a root pref.
Matt Menkeaee78692022-09-26 15:45:52142 base::Value::Dict* GetOrCreateAppKey(const std::string& id,
143 base::Value::Dict& root);
Joshua Pawlickic9616ce2020-08-11 17:24:48144
Xiaoling Baobf0a5162023-03-29 01:37:24145 absl::optional<int> GetInteger(const std::string& id,
146 const std::string& key) const;
Xiaoling Bao7152ba82023-03-27 19:19:27147 void SetInteger(const std::string& id, const std::string& key, int value);
Sorin Jianu2d0c54a2020-02-27 21:35:52148 std::string GetString(const std::string& id, const std::string& key) const;
149 void SetString(const std::string& id,
150 const std::string& key,
151 const std::string& value);
Sorin Jianuf5425c322022-01-29 01:14:27152
Sorin Jianu2d0c54a2020-02-27 21:35:52153 SEQUENCE_CHECKER(sequence_checker_);
154
Xiaoling Bao9d44237f2023-01-18 21:00:27155 const UpdaterScope scope_;
Paul Semel89e1f63c2023-06-19 13:34:10156 raw_ptr<PrefService, DanglingUntriaged> pref_service_ = nullptr;
Sorin Jianu2d0c54a2020-02-27 21:35:52157};
158
Joshua Pawlicki95356f02022-01-12 19:45:17159void RegisterPersistedDataPrefs(scoped_refptr<PrefRegistrySimple> registry);
160
Sorin Jianu2d0c54a2020-02-27 21:35:52161} // namespace updater
162
163#endif // CHROME_UPDATER_PERSISTED_DATA_H_