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
|
// Copyright 2023 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/metrics/structured/lib/key_data_file_delegate.h"
#include <utility>
#include "base/files/file_path.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "components/metrics/structured/lib/histogram_util.h"
#include "components/metrics/structured/lib/key_util.h"
#include "components/metrics/structured/lib/persistent_proto.h"
#include "components/metrics/structured/lib/proto/key.pb.h"
namespace metrics::structured {
KeyDataFileDelegate::KeyDataFileDelegate(
const base::FilePath& path,
base::TimeDelta save_delay,
base::OnceClosure on_initialized_callback)
: on_initialized_callback_(std::move(on_initialized_callback)) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
proto_ = std::make_unique<PersistentProto<KeyDataProto>>(
path, save_delay,
base::BindOnce(&KeyDataFileDelegate::OnRead, weak_factory_.GetWeakPtr()),
base::BindRepeating(&KeyDataFileDelegate::OnWrite,
weak_factory_.GetWeakPtr()));
}
KeyDataFileDelegate::~KeyDataFileDelegate() = default;
bool KeyDataFileDelegate::IsReady() const {
return is_initialized_;
}
const KeyProto* KeyDataFileDelegate::GetKey(uint64_t project_name_hash) const {
const auto& keys = proto_.get()->get()->keys();
auto it = keys.find(project_name_hash);
if (it != keys.end()) {
return &it->second;
}
return nullptr;
}
void KeyDataFileDelegate::UpsertKey(uint64_t project_name_hash,
base::TimeDelta last_key_rotation,
base::TimeDelta key_rotation_period) {
KeyProto& key = (*(proto_.get()->get()->mutable_keys()))[project_name_hash];
key.set_key(util::GenerateNewKey());
key.set_last_rotation(last_key_rotation.InDays());
key.set_rotation_period(key_rotation_period.InDays());
proto_->QueueWrite();
}
void KeyDataFileDelegate::Purge() {
proto_->Purge();
}
void KeyDataFileDelegate::OnRead(ReadStatus status) {
is_initialized_ = true;
switch (status) {
case ReadStatus::kOk:
case ReadStatus::kMissing:
break;
case ReadStatus::kReadError:
LogInternalError(StructuredMetricsError::kKeyReadError);
break;
case ReadStatus::kParseError:
LogInternalError(StructuredMetricsError::kKeyParseError);
break;
}
std::move(on_initialized_callback_).Run();
}
void KeyDataFileDelegate::OnWrite(WriteStatus status) {
switch (status) {
case WriteStatus::kOk:
break;
case WriteStatus::kWriteError:
LogInternalError(StructuredMetricsError::kKeyWriteError);
break;
case WriteStatus::kSerializationError:
LogInternalError(StructuredMetricsError::kKeySerializationError);
break;
}
}
void KeyDataFileDelegate::WriteNowForTesting() {
proto_.get()->StartWriteForTesting(); // IN-TEST
}
} // namespace metrics::structured
|