1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/metrics/structured/lib/key_data_file_delegate.h"
6
7 #include <utility>
8
9 #include "base/files/file_path.h"
10 #include "base/memory/scoped_refptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/sequence_checker.h"
13 #include "base/task/bind_post_task.h"
14 #include "base/task/sequenced_task_runner.h"
15 #include "base/time/time.h"
16 #include "components/metrics/structured/lib/histogram_util.h"
17 #include "components/metrics/structured/lib/key_util.h"
18 #include "components/metrics/structured/lib/persistent_proto.h"
19 #include "components/metrics/structured/lib/proto/key.pb.h"
20
21 namespace metrics::structured {
22
KeyDataFileDelegate(const base::FilePath & path,base::TimeDelta save_delay,base::OnceClosure on_initialized_callback)23 KeyDataFileDelegate::KeyDataFileDelegate(
24 const base::FilePath& path,
25 base::TimeDelta save_delay,
26 base::OnceClosure on_initialized_callback)
27 : on_initialized_callback_(std::move(on_initialized_callback)) {
28 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
29 proto_ = std::make_unique<PersistentProto<KeyDataProto>>(
30 path, save_delay,
31 base::BindOnce(&KeyDataFileDelegate::OnRead, weak_factory_.GetWeakPtr()),
32 base::BindRepeating(&KeyDataFileDelegate::OnWrite,
33 weak_factory_.GetWeakPtr()));
34 }
35
36 KeyDataFileDelegate::~KeyDataFileDelegate() = default;
37
IsReady() const38 bool KeyDataFileDelegate::IsReady() const {
39 return is_initialized_;
40 }
41
GetKey(uint64_t project_name_hash) const42 const KeyProto* KeyDataFileDelegate::GetKey(uint64_t project_name_hash) const {
43 const auto& keys = proto_.get()->get()->keys();
44 auto it = keys.find(project_name_hash);
45 if (it != keys.end()) {
46 return &it->second;
47 }
48 return nullptr;
49 }
50
UpsertKey(uint64_t project_name_hash,base::TimeDelta last_key_rotation,base::TimeDelta key_rotation_period)51 void KeyDataFileDelegate::UpsertKey(uint64_t project_name_hash,
52 base::TimeDelta last_key_rotation,
53 base::TimeDelta key_rotation_period) {
54 KeyProto& key = (*(proto_.get()->get()->mutable_keys()))[project_name_hash];
55 key.set_key(util::GenerateNewKey());
56 key.set_last_rotation(last_key_rotation.InDays());
57 key.set_rotation_period(key_rotation_period.InDays());
58 proto_->QueueWrite();
59 }
60
Purge()61 void KeyDataFileDelegate::Purge() {
62 proto_->Purge();
63 }
64
OnRead(ReadStatus status)65 void KeyDataFileDelegate::OnRead(ReadStatus status) {
66 is_initialized_ = true;
67 switch (status) {
68 case ReadStatus::kOk:
69 case ReadStatus::kMissing:
70 break;
71 case ReadStatus::kReadError:
72 LogInternalError(StructuredMetricsError::kKeyReadError);
73 break;
74 case ReadStatus::kParseError:
75 LogInternalError(StructuredMetricsError::kKeyParseError);
76 break;
77 }
78
79 std::move(on_initialized_callback_).Run();
80 }
81
OnWrite(WriteStatus status)82 void KeyDataFileDelegate::OnWrite(WriteStatus status) {
83 switch (status) {
84 case WriteStatus::kOk:
85 break;
86 case WriteStatus::kWriteError:
87 LogInternalError(StructuredMetricsError::kKeyWriteError);
88 break;
89 case WriteStatus::kSerializationError:
90 LogInternalError(StructuredMetricsError::kKeySerializationError);
91 break;
92 }
93 }
94
WriteNowForTesting()95 void KeyDataFileDelegate::WriteNowForTesting() {
96 proto_.get()->StartWriteForTesting(); // IN-TEST
97 }
98
99 } // namespace metrics::structured
100