[go: nahoru, domu]

blob: e17f5f332f807b5480f0b2a493b1ce3f7aeb4f72 [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.
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 Jianu94f92a12020-07-22 19:39:3342class PersistedData : public base::RefCountedThreadSafe<PersistedData> {
Sorin Jianu2d0c54a2020-02-27 21:35:5243 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 Bao9d44237f2023-01-18 21:00:2747 PersistedData(UpdaterScope scope, PrefService* pref_service);
Sorin Jianu2d0c54a2020-02-27 21:35:5248 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 Changb15df0e22020-03-02 22:21:1260 // 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 Baobb3afae2021-12-03 22:09:3269 // 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 Pawlicki89132a02021-10-14 19:53:1473 // 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 Changb15df0e22020-03-02 22:21:1276
77 // This function sets everything in the registration request object into the
78 // persistent data store.
79 void RegisterApp(const RegistrationRequest& rq);
80
Michael Chang1b9300912020-09-10 17:36:2881 // This function removes a registered application from the persistent store.
82 bool RemoveApp(const std::string& id);
83
Sorin Jianu2d0c54a2020-02-27 21:35:5284 // Returns the app ids of the applications registered in prefs, if the
85 // application has a valid version.
86 std::vector<std::string> GetAppIds() const;
87
Joshua Pawlicki95356f02022-01-12 19:45:1788 // HadApps is set when the updater processes a registration for an app other
89 // than itself, and is never unset, even if the app is uninstalled.
90 bool GetHadApps() const;
91 void SetHadApps();
92
Joshua Pawlickif64b0282023-02-02 17:13:2793 // UsageStatsEnabled reflects whether the updater as a whole is allowed to
94 // send usage stats, and is set or reset periodically based on the usage
95 // stats opt-in state of each product.
96 bool GetUsageStatsEnabled() const;
97 void SetUsageStatsEnabled(bool usage_stats_enabled);
98
Sorin Jianuf5425c322022-01-29 01:14:2799 // LastChecked is set when the updater completed successfully a call to
100 // `UpdateService::UpdateAll` as indicated by the `UpdateService::Result`
101 // argument of the completion callback. This means that the execution path
102 // for updating all applications works end to end, including communicating
103 // with the backend.
104 base::Time GetLastChecked() const;
105 void SetLastChecked(const base::Time& time);
106
107 // LastStarted is set when `UpdateService::RunPeriodicTasks` is called. This
108 // indicates that the mechanism to initiate automated update checks is
109 // working.
110 base::Time GetLastStarted() const;
111 void SetLastStarted(const base::Time& time);
112
S. Ganesh73daf2c2022-06-14 01:03:55113#if BUILDFLAG(IS_WIN)
114 // Retrieves the previously stored OS version.
115 absl::optional<OSVERSIONINFOEX> GetLastOSVersion() const;
116
117 // Stores the current os version.
118 void SetLastOSVersion();
119#endif
120
Sorin Jianu2d0c54a2020-02-27 21:35:52121 private:
Sorin Jianu94f92a12020-07-22 19:39:33122 friend class base::RefCountedThreadSafe<PersistedData>;
Sorin Jianu2d0c54a2020-02-27 21:35:52123 ~PersistedData();
124
Joshua Pawlickic9616ce2020-08-11 17:24:48125 // Returns nullptr if the app key does not exist.
Matt Menkeb1732aac2022-06-02 13:48:03126 const base::Value::Dict* GetAppKey(const std::string& id) const;
Joshua Pawlickic9616ce2020-08-11 17:24:48127
128 // Returns an existing or newly created app key under a root pref.
Matt Menkeaee78692022-09-26 15:45:52129 base::Value::Dict* GetOrCreateAppKey(const std::string& id,
130 base::Value::Dict& root);
Joshua Pawlickic9616ce2020-08-11 17:24:48131
Sorin Jianu2d0c54a2020-02-27 21:35:52132 std::string GetString(const std::string& id, const std::string& key) const;
133 void SetString(const std::string& id,
134 const std::string& key,
135 const std::string& value);
Sorin Jianuf5425c322022-01-29 01:14:27136
Sorin Jianu2d0c54a2020-02-27 21:35:52137 SEQUENCE_CHECKER(sequence_checker_);
138
Xiaoling Bao9d44237f2023-01-18 21:00:27139 const UpdaterScope scope_;
Keishi Hattori0e45c022021-11-27 09:25:52140 raw_ptr<PrefService> pref_service_ = nullptr; // Not owned by this class.
Sorin Jianu2d0c54a2020-02-27 21:35:52141};
142
Joshua Pawlicki95356f02022-01-12 19:45:17143void RegisterPersistedDataPrefs(scoped_refptr<PrefRegistrySimple> registry);
144
Sorin Jianu2d0c54a2020-02-27 21:35:52145} // namespace updater
146
147#endif // CHROME_UPDATER_PERSISTED_DATA_H_