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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/update_client/persisted_data.h"
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "base/values.h"
#include "base/version.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/activity_data_service.h"
#include "components/update_client/update_client_errors.h"
namespace update_client {
const char kPersistedDataPreference[] = "updateclientdata";
const char kLastUpdateCheckErrorPreference[] =
"updateclientlastupdatecheckerror";
const char kLastUpdateCheckErrorCategoryPreference[] =
"updateclientlastupdatecheckerrorcategory";
const char kLastUpdateCheckErrorExtraCode1Preference[] =
"updateclientlastupdatecheckerrorextracode1";
namespace {
const char kThrottleUpdatesUntilPreference[] = "updateclientthrottleuntil";
class PersistedDataImpl : public PersistedData {
public:
// Constructs a provider using the specified |pref_service| and
// |activity_data_service|.
// The associated preferences are assumed to already be registered.
// The |pref_service| and |activity_data_service| must outlive the entire
// update_client.
PersistedDataImpl(
base::RepeatingCallback<PrefService*()> pref_service_provider,
std::unique_ptr<ActivityDataService> activity_data_service);
PersistedDataImpl(const PersistedDataImpl&) = delete;
PersistedDataImpl& operator=(const PersistedDataImpl&) = delete;
~PersistedDataImpl() override;
// This is called only via update_client's RegisterUpdateClientPreferences.
static void RegisterPrefs(PrefRegistrySimple* registry);
// PersistedData overrides:
int GetDateLastRollCall(const std::string& id) const override;
int GetDateLastActive(const std::string& id) const override;
std::string GetPingFreshness(const std::string& id) const override;
void SetDateLastData(const std::vector<std::string>& ids,
int datenum,
base::OnceClosure callback) override;
void SetDateLastActive(const std::string& id, int dla) override;
void SetDateLastRollCall(const std::string& id, int dlrc) override;
int GetInstallDate(const std::string& id) const override;
void SetInstallDate(const std::string& id, int install_date) override;
std::string GetInstallId(const std::string& app_id) const override;
void SetInstallId(const std::string& app_id,
const std::string& install_id) override;
std::string GetCohort(const std::string& id) const override;
std::string GetCohortHint(const std::string& id) const override;
std::string GetCohortName(const std::string& id) const override;
void SetCohort(const std::string& id, const std::string& cohort) override;
void SetCohortHint(const std::string& id,
const std::string& cohort_hint) override;
void SetCohortName(const std::string& id,
const std::string& cohort_name) override;
void GetActiveBits(const std::vector<std::string>& ids,
base::OnceCallback<void(const std::set<std::string>&)>
callback) const override;
int GetDaysSinceLastRollCall(const std::string& id) const override;
int GetDaysSinceLastActive(const std::string& id) const override;
base::Version GetProductVersion(const std::string& id) const override;
void SetProductVersion(const std::string& id,
const base::Version& pv) override;
base::Version GetMaxPreviousProductVersion(
const std::string& id) const override;
void SetMaxPreviousProductVersion(const std::string& id,
const base::Version& max_version) override;
std::string GetFingerprint(const std::string& id) const override;
void SetFingerprint(const std::string& id,
const std::string& fingerprint) override;
base::Time GetThrottleUpdatesUntil() const override;
void SetThrottleUpdatesUntil(base::Time time) override;
void SetLastUpdateCheckError(const CategorizedError& error) override;
private:
// Returns nullptr if the app key does not exist.
const base::Value::Dict* GetAppKey(const std::string& id) const;
// Returns an existing or newly created app key under a root pref.
base::Value::Dict* GetOrCreateAppKey(const std::string& id,
base::Value::Dict& root);
// Returns fallback if the key does not exist.
int GetInt(const std::string& id, const std::string& key, int fallback) const;
// Returns the empty string if the key does not exist.
std::string GetString(const std::string& id, const std::string& key) const;
void SetString(const std::string& id,
const std::string& key,
const std::string& value);
void SetDateLastDataHelper(const std::vector<std::string>& ids,
int datenum,
base::OnceClosure callback,
const std::set<std::string>& active_ids);
SEQUENCE_CHECKER(sequence_checker_);
base::RepeatingCallback<PrefService*()> pref_service_provider_;
std::unique_ptr<ActivityDataService> activity_data_service_;
};
PersistedDataImpl::PersistedDataImpl(
base::RepeatingCallback<PrefService*()> pref_service_provider,
std::unique_ptr<ActivityDataService> activity_data_service)
: pref_service_provider_(pref_service_provider),
activity_data_service_(std::move(activity_data_service)) {
PrefService* prefs = pref_service_provider_.Run();
CHECK(!prefs || prefs->FindPreference(kPersistedDataPreference));
}
PersistedDataImpl::~PersistedDataImpl() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
const base::Value::Dict* PersistedDataImpl::GetAppKey(
const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service) {
return nullptr;
}
const base::Value& dict = pref_service->GetValue(kPersistedDataPreference);
if (!dict.is_dict()) {
return nullptr;
}
const base::Value::Dict* apps = dict.GetDict().FindDict("apps");
if (!apps) {
return nullptr;
}
return apps->FindDict(base::ToLowerASCII(id));
}
int PersistedDataImpl::GetInt(const std::string& id,
const std::string& key,
int fallback) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const base::Value::Dict* app_key = GetAppKey(id);
if (!app_key) {
return fallback;
}
return app_key->FindInt(key).value_or(fallback);
}
std::string PersistedDataImpl::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;
}
int PersistedDataImpl::GetDateLastRollCall(const std::string& id) const {
return GetInt(id, "dlrc", kDateUnknown);
}
int PersistedDataImpl::GetDateLastActive(const std::string& id) const {
return GetInt(id, "dla", kDateUnknown);
}
std::string PersistedDataImpl::GetPingFreshness(const std::string& id) const {
std::string result = GetString(id, "pf");
return !result.empty() ? base::StringPrintf("{%s}", result.c_str()) : result;
}
int PersistedDataImpl::GetInstallDate(const std::string& id) const {
return GetInt(id, "installdate", kDateUnknown);
}
std::string PersistedDataImpl::GetCohort(const std::string& id) const {
return GetString(id, "cohort");
}
std::string PersistedDataImpl::GetCohortName(const std::string& id) const {
return GetString(id, "cohortname");
}
std::string PersistedDataImpl::GetCohortHint(const std::string& id) const {
return GetString(id, "cohorthint");
}
std::string PersistedDataImpl::GetInstallId(const std::string& id) const {
return GetString(id, "iid");
}
base::Value::Dict* PersistedDataImpl::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->FindDict(base::ToLowerASCII(id));
if (!app) {
app = &apps->Set(base::ToLowerASCII(id), base::Value::Dict())->GetDict();
app->Set("installdate", kDateFirstTime);
}
return app;
}
void PersistedDataImpl::SetDateLastDataHelper(
const std::vector<std::string>& ids,
int datenum,
base::OnceClosure callback,
const std::set<std::string>& active_ids) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service) {
std::move(callback).Run();
return;
}
ScopedDictPrefUpdate update(pref_service, kPersistedDataPreference);
for (const auto& id : ids) {
base::Value::Dict* app_key = GetOrCreateAppKey(id, update.Get());
app_key->Set("dlrc", datenum);
app_key->Set("pf", base::Uuid::GenerateRandomV4().AsLowercaseString());
if (GetInstallDate(id) == kDateFirstTime) {
app_key->Set("installdate", datenum);
}
if (active_ids.find(id) != active_ids.end()) {
app_key->Set("dla", datenum);
app_key->Remove("iid");
}
}
std::move(callback).Run();
}
void PersistedDataImpl::SetDateLastData(const std::vector<std::string>& ids,
int datenum,
base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service || datenum < 0) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, std::move(callback));
return;
}
if (!activity_data_service_) {
SetDateLastDataHelper(ids, datenum, std::move(callback), {});
return;
}
activity_data_service_->GetAndClearActiveBits(
ids, base::BindOnce(&PersistedDataImpl::SetDateLastDataHelper,
base::Unretained(this), ids, datenum,
std::move(callback)));
}
void PersistedDataImpl::SetDateLastActive(const std::string& id, int dla) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service) {
return;
}
ScopedDictPrefUpdate update(pref_service, kPersistedDataPreference);
base::Value::Dict* app_key = GetOrCreateAppKey(id, update.Get());
app_key->Set("dla", dla);
}
void PersistedDataImpl::SetDateLastRollCall(const std::string& id, int dlrc) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service) {
return;
}
ScopedDictPrefUpdate update(pref_service, kPersistedDataPreference);
base::Value::Dict* app_key = GetOrCreateAppKey(id, update.Get());
app_key->Set("dlrc", dlrc);
}
void PersistedDataImpl::SetInstallDate(const std::string& id,
int install_date) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service) {
return;
}
ScopedDictPrefUpdate update(pref_service, kPersistedDataPreference);
base::Value::Dict* app_key = GetOrCreateAppKey(id, update.Get());
app_key->Set("installdate", install_date);
}
void PersistedDataImpl::SetString(const std::string& id,
const std::string& key,
const std::string& value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service) {
return;
}
ScopedDictPrefUpdate update(pref_service, kPersistedDataPreference);
GetOrCreateAppKey(id, update.Get())->Set(key, value);
}
void PersistedDataImpl::SetCohort(const std::string& id,
const std::string& cohort) {
SetString(id, "cohort", cohort);
}
void PersistedDataImpl::SetCohortName(const std::string& id,
const std::string& cohort_name) {
SetString(id, "cohortname", cohort_name);
}
void PersistedDataImpl::SetCohortHint(const std::string& id,
const std::string& cohort_hint) {
SetString(id, "cohorthint", cohort_hint);
}
void PersistedDataImpl::SetInstallId(const std::string& app_id,
const std::string& install_id) {
SetString(app_id, "iid", install_id);
}
void PersistedDataImpl::GetActiveBits(
const std::vector<std::string>& ids,
base::OnceCallback<void(const std::set<std::string>&)> callback) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!activity_data_service_) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), std::set<std::string>{}));
return;
}
activity_data_service_->GetActiveBits(ids, std::move(callback));
}
int PersistedDataImpl::GetDaysSinceLastRollCall(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return activity_data_service_
? activity_data_service_->GetDaysSinceLastRollCall(id)
: kDaysUnknown;
}
int PersistedDataImpl::GetDaysSinceLastActive(const std::string& id) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return activity_data_service_
? activity_data_service_->GetDaysSinceLastActive(id)
: kDaysUnknown;
}
base::Version PersistedDataImpl::GetProductVersion(
const std::string& id) const {
return base::Version(GetString(id, "pv"));
}
void PersistedDataImpl::SetProductVersion(const std::string& id,
const base::Version& pv) {
CHECK(pv.IsValid());
SetString(id, "pv", pv.GetString());
}
base::Version PersistedDataImpl::GetMaxPreviousProductVersion(
const std::string& id) const {
return base::Version(GetString(id, "max_pv"));
}
void PersistedDataImpl::SetMaxPreviousProductVersion(
const std::string& id,
const base::Version& max_version) {
CHECK(max_version.IsValid());
auto existing_max = GetMaxPreviousProductVersion(id);
if (!existing_max.IsValid() || max_version > existing_max) {
SetString(id, "max_pv", max_version.GetString());
}
}
std::string PersistedDataImpl::GetFingerprint(const std::string& id) const {
return GetString(id, "fp");
}
void PersistedDataImpl::SetFingerprint(const std::string& id,
const std::string& fingerprint) {
SetString(id, "fp", fingerprint);
}
base::Time PersistedDataImpl::GetThrottleUpdatesUntil() const {
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service) {
return base::Time();
}
return pref_service->GetTime(kThrottleUpdatesUntilPreference);
}
void PersistedDataImpl::SetThrottleUpdatesUntil(base::Time time) {
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service) {
return;
}
pref_service->SetTime(kThrottleUpdatesUntilPreference, time);
}
void PersistedDataImpl::SetLastUpdateCheckError(const CategorizedError& error) {
PrefService* pref_service = pref_service_provider_.Run();
if (!pref_service) {
return;
}
pref_service->SetInteger(kLastUpdateCheckErrorPreference, error.code);
pref_service->SetInteger(kLastUpdateCheckErrorCategoryPreference,
static_cast<int>(error.category));
pref_service->SetInteger(kLastUpdateCheckErrorExtraCode1Preference,
error.extra);
}
} // namespace
std::unique_ptr<PersistedData> CreatePersistedData(
base::RepeatingCallback<PrefService*()> pref_service_provider,
std::unique_ptr<ActivityDataService> activity_data_service) {
return std::make_unique<PersistedDataImpl>(pref_service_provider,
std::move(activity_data_service));
}
void RegisterPersistedDataPrefs(PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(kPersistedDataPreference);
registry->RegisterTimePref(kThrottleUpdatesUntilPreference, base::Time());
registry->RegisterIntegerPref(kLastUpdateCheckErrorPreference, 0);
registry->RegisterIntegerPref(kLastUpdateCheckErrorCategoryPreference, 0);
registry->RegisterIntegerPref(kLastUpdateCheckErrorExtraCode1Preference, 0);
}
} // namespace update_client
|