forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersisted_data.cc
403 lines (341 loc) · 13.4 KB
/
persisted_data.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/updater/persisted_data.h"
#include <string>
#include <vector>
#include "base/base64.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/sequence_checker.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "base/version.h"
#include "build/build_config.h"
#include "chrome/updater/registration_data.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/update_client/persisted_data.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include "base/strings/sys_string_conversions.h"
#include "chrome/updater/util/win_util.h"
#endif
namespace {
constexpr char kPV[] = "pv"; // Key for storing product version.
constexpr char kFP[] = "fp"; // Key for storing fingerprint.
constexpr char kECP[] = "ecp"; // Key for storing existence checker path.
constexpr char kBC[] = "bc"; // Key for storing brand code.
constexpr char kBP[] = "bp"; // Key for storing brand path.
constexpr char kAP[] = "ap"; // Key for storing ap.
constexpr char kDLA[] = "dla"; // Key for storing date-last-active.
constexpr char kDLRC[] = "dlrc"; // Key for storing date-last-rollcall.
constexpr char kHadApps[] = "had_apps";
constexpr char kUsageStatsEnabledKey[] = "usage_stats_enabled";
constexpr char kLastChecked[] = "last_checked";
constexpr char kLastStarted[] = "last_started";
constexpr char kLastOSVersion[] = "last_os_version";
} // namespace
namespace updater {
PersistedData::PersistedData(UpdaterScope scope, PrefService* pref_service)
: scope_(scope), pref_service_(pref_service) {
CHECK(pref_service_);
CHECK(pref_service_->FindPreference(update_client::kPersistedDataPreference));
}
PersistedData::~PersistedData() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
base::Version PersistedData::GetProductVersion(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return base::Version(GetString(id, kPV));
}
void PersistedData::SetProductVersion(const std::string& id,
const base::Version& pv) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(pv.IsValid());
SetString(id, kPV, pv.GetString());
}
std::string PersistedData::GetFingerprint(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetString(id, kFP);
}
void PersistedData::SetFingerprint(const std::string& id,
const std::string& fingerprint) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetString(id, kFP, fingerprint);
}
base::FilePath PersistedData::GetExistenceCheckerPath(
const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return base::FilePath::FromUTF8Unsafe(GetString(id, kECP));
}
void PersistedData::SetExistenceCheckerPath(const std::string& id,
const base::FilePath& ecp) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetString(id, kECP, ecp.AsUTF8Unsafe());
}
std::string PersistedData::GetBrandCode(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetString(id, kBC);
}
void PersistedData::SetBrandCode(const std::string& id, const std::string& bc) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetString(id, kBC, bc);
}
base::FilePath PersistedData::GetBrandPath(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return base::FilePath::FromUTF8Unsafe(GetString(id, kBP));
}
void PersistedData::SetBrandPath(const std::string& id,
const base::FilePath& bp) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetString(id, kBP, bp.AsUTF8Unsafe());
}
std::string PersistedData::GetAP(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetString(id, kAP);
}
void PersistedData::SetAP(const std::string& id, const std::string& ap) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetString(id, kAP, ap);
#if BUILDFLAG(IS_WIN)
// For backwards compatibility, we record the AP in ClientState as well.
// (Some applications read it from there.)
// Chromium Updater has both local and global pref stores. In practice, if
// this `PersistedData` is using a local pref store, `id` will be the
// qualification app and the ClientState value is not important, so it is
// acceptable for each instance of the updater to overwrite it with various
// values. Else, this is the global pref store and reflecting the value in
// registry is correct. Clients should transition to requesting the
// registration info for their application via RPC.
SetRegistryKey(UpdaterScopeToHKeyRoot(scope_),
GetAppClientStateKey(base::SysUTF8ToWide(id)), L"ap",
base::SysUTF8ToWide(ap));
#endif
}
absl::optional<int> PersistedData::GetDateLastActive(
const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetInteger(id, kDLA);
}
void PersistedData::SetDateLastActive(const std::string& id, int dla) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetInteger(id, kDLA, dla);
}
absl::optional<int> PersistedData::GetDateLastRollcall(
const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return GetInteger(id, kDLRC);
}
void PersistedData::SetDateLastRollcall(const std::string& id, int dlrc) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
SetInteger(id, kDLRC, dlrc);
}
void PersistedData::RegisterApp(const RegistrationRequest& rq) {
VLOG(2) << __func__ << ": Registering " << rq.app_id << " at version "
<< rq.version;
if (rq.version.IsValid()) {
SetProductVersion(rq.app_id, rq.version);
}
if (!rq.existence_checker_path.empty()) {
SetExistenceCheckerPath(rq.app_id, rq.existence_checker_path);
}
if (!rq.brand_code.empty()) {
SetBrandCode(rq.app_id, rq.brand_code);
}
if (!rq.brand_path.empty()) {
SetBrandPath(rq.app_id, rq.brand_path);
}
if (!rq.ap.empty()) {
SetAP(rq.app_id, rq.ap);
}
if (rq.dla) {
SetDateLastActive(rq.app_id, rq.dla.value());
}
if (rq.dlrc) {
SetDateLastRollcall(rq.app_id, rq.dlrc.value());
}
}
bool PersistedData::RemoveApp(const std::string& id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!pref_service_)
return false;
ScopedDictPrefUpdate update(pref_service_,
update_client::kPersistedDataPreference);
base::Value::Dict* apps = update->FindDict("apps");
return apps ? apps->Remove(id) : false;
}
std::vector<std::string> PersistedData::GetAppIds() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// The prefs is a dictionary of dictionaries, where each inner dictionary
// corresponds to an app:
// {"updateclientdata":{"apps":{"{44FC7FE2-65CE-487C-93F4-EDEE46EEAAAB}":{...
const base::Value::Dict& dict =
pref_service_->GetDict(update_client::kPersistedDataPreference);
const base::Value::Dict* apps = dict.FindDict("apps");
if (!apps)
return {};
std::vector<std::string> app_ids;
for (auto it = apps->begin(); it != apps->end(); ++it) {
const auto& app_id = it->first;
const auto pv = GetProductVersion(app_id);
if (pv.IsValid())
app_ids.push_back(app_id);
}
return app_ids;
}
const base::Value::Dict* PersistedData::GetAppKey(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!pref_service_)
return nullptr;
const base::Value::Dict& dict =
pref_service_->GetDict(update_client::kPersistedDataPreference);
const base::Value::Dict* apps = dict.FindDict("apps");
if (!apps)
return nullptr;
return apps->FindDict(base::ToLowerASCII(id));
}
std::string PersistedData::GetString(const std::string& id,
const std::string& key) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const base::Value::Dict* app_key = GetAppKey(id);
if (!app_key)
return {};
const std::string* value = app_key->FindString(key);
if (!value)
return {};
return *value;
}
base::Value::Dict* PersistedData::GetOrCreateAppKey(const std::string& id,
base::Value::Dict& root) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::Value::Dict* apps = root.EnsureDict("apps");
base::Value::Dict* app = apps->EnsureDict(base::ToLowerASCII(id));
return app;
}
absl::optional<int> PersistedData::GetInteger(const std::string& id,
const std::string& key) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!pref_service_) {
return absl::nullopt;
}
ScopedDictPrefUpdate update(pref_service_,
update_client::kPersistedDataPreference);
base::Value::Dict* apps = update->FindDict("apps");
if (!apps) {
return absl::nullopt;
}
base::Value::Dict* app = apps->FindDict(base::ToLowerASCII(id));
if (!app) {
return absl::nullopt;
}
return app->FindInt(key);
}
void PersistedData::SetInteger(const std::string& id,
const std::string& key,
int value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!pref_service_) {
return;
}
ScopedDictPrefUpdate update(pref_service_,
update_client::kPersistedDataPreference);
GetOrCreateAppKey(id, update.Get())->Set(key, value);
}
void PersistedData::SetString(const std::string& id,
const std::string& key,
const std::string& value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!pref_service_)
return;
ScopedDictPrefUpdate update(pref_service_,
update_client::kPersistedDataPreference);
GetOrCreateAppKey(id, update.Get())->Set(key, value);
}
bool PersistedData::GetHadApps() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_ && pref_service_->GetBoolean(kHadApps);
}
void PersistedData::SetHadApps() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (pref_service_)
pref_service_->SetBoolean(kHadApps, true);
}
bool PersistedData::GetUsageStatsEnabled() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_ && pref_service_->GetBoolean(kUsageStatsEnabledKey);
}
void PersistedData::SetUsageStatsEnabled(bool usage_stats_enabled) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (pref_service_) {
pref_service_->SetBoolean(kUsageStatsEnabledKey, usage_stats_enabled);
}
}
base::Time PersistedData::GetLastChecked() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetTime(kLastChecked);
}
void PersistedData::SetLastChecked(const base::Time& time) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (pref_service_)
pref_service_->SetTime(kLastChecked, time);
}
base::Time PersistedData::GetLastStarted() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetTime(kLastStarted);
}
void PersistedData::SetLastStarted(const base::Time& time) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (pref_service_)
pref_service_->SetTime(kLastStarted, time);
}
#if BUILDFLAG(IS_WIN)
absl::optional<OSVERSIONINFOEX> PersistedData::GetLastOSVersion() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Unpacks the os version from a base-64-encoded string internally.
const std::string encoded_os_version =
pref_service_->GetString(kLastOSVersion);
if (encoded_os_version.empty())
return absl::nullopt;
const absl::optional<std::vector<uint8_t>> decoded_os_version =
base::Base64Decode(encoded_os_version);
if (!decoded_os_version ||
decoded_os_version->size() != sizeof(OSVERSIONINFOEX)) {
return absl::nullopt;
}
return *reinterpret_cast<const OSVERSIONINFOEX*>(decoded_os_version->data());
}
void PersistedData::SetLastOSVersion() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!pref_service_)
return;
// Get and set the current OS version.
absl::optional<OSVERSIONINFOEX> os_version = GetOSVersion();
if (!os_version)
return;
// The os version is internally stored as a base-64-encoded string.
std::string encoded_os_version;
base::Base64Encode(
base::StringPiece(reinterpret_cast<const char*>(&os_version.value()),
sizeof(OSVERSIONINFOEX)),
&encoded_os_version);
return pref_service_->SetString(kLastOSVersion, encoded_os_version);
}
#endif
// Register persisted data prefs, except for kPersistedDataPreference.
// kPersistedDataPreference is registered by update_client::RegisterPrefs.
void RegisterPersistedDataPrefs(scoped_refptr<PrefRegistrySimple> registry) {
registry->RegisterBooleanPref(kHadApps, false);
registry->RegisterBooleanPref(kUsageStatsEnabledKey, false);
registry->RegisterTimePref(kLastChecked, {});
registry->RegisterTimePref(kLastStarted, {});
registry->RegisterStringPref(kLastOSVersion, {});
}
} // namespace updater