File: key_data_prefs_delegate.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 3,994 bytes parent folder | download | duplicates (9)
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
// Copyright 2024 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/key_data_prefs_delegate.h"

#include <optional>
#include <string_view>
#include <utility>

#include "base/check.h"
#include "base/logging.h"
#include "base/values.h"
#include "components/metrics/structured/lib/key_data.h"
#include "components/metrics/structured/lib/key_util.h"
#include "components/metrics/structured/lib/proto/key.pb.h"
#include "components/metrics/structured/project_validator.h"
#include "components/metrics/structured/structured_metrics_validator.h"
#include "components/prefs/scoped_user_pref_update.h"

namespace metrics::structured {

KeyDataPrefsDelegate::KeyDataPrefsDelegate(PrefService* local_state,
                                           std::string_view pref_name)
    : local_state_(local_state), pref_name_(pref_name) {
  CHECK(local_state_);
  CHECK(!pref_name_.empty());

  LoadKeysFromPrefs();
}

KeyDataPrefsDelegate::~KeyDataPrefsDelegate() = default;

bool KeyDataPrefsDelegate::IsReady() const {
  return true;
}

const KeyProto* KeyDataPrefsDelegate::GetKey(uint64_t project_name_hash) const {
  CHECK(IsReady());
  auto it = proto_.keys().find(project_name_hash);
  if (it != proto_.keys().end()) {
    return &it->second;
  }
  return nullptr;
}

void KeyDataPrefsDelegate::UpsertKey(uint64_t project_name_hash,
                                     base::TimeDelta last_key_rotation,
                                     base::TimeDelta key_rotation_period) {
  KeyProto& key_proto = (*(proto_.mutable_keys()))[project_name_hash];
  key_proto.set_key(util::GenerateNewKey());
  key_proto.set_last_rotation(last_key_rotation.InDays());
  key_proto.set_rotation_period(key_rotation_period.InDays());

  UpdatePrefsByProject(project_name_hash, key_proto);
}

void KeyDataPrefsDelegate::Purge() {
  // Clears in-memory keys.
  proto_.mutable_keys()->clear();

  // Clears persisted keys.
  local_state_->ClearPref(pref_name_);
}

void KeyDataPrefsDelegate::LoadKeysFromPrefs() {
  const base::Value::Dict& keys_pref = local_state_->GetDict(pref_name_);

  // Use the validators to get the project name to project hash mapping.
  const validator::Validators* validators = validator::Validators::Get();

  auto* proto_keys = proto_.mutable_keys();

  for (const auto [project_name, project_keys] : keys_pref) {
    std::optional<const ProjectValidator*> project_validator =
        validators->GetProjectValidator(project_name);

    // Check if a project was found for the name.
    if (!project_validator.has_value()) {
      continue;
    }

    const uint64_t project_hash = (*project_validator)->project_hash();
    const base::Value::Dict* value_dict = project_keys.GetIfDict();
    if (!value_dict) {
      LOG(ERROR) << "Key Pref value was expected to be a dict.";
      continue;
    }

    std::optional<KeyProto> key_data =
        util::CreateKeyProtoFromValue(*value_dict);

    if (!key_data.has_value()) {
      LOG(ERROR) << "Failed to convert pref value into key data.";
      continue;
    }

    (*proto_keys)[project_hash] = *key_data;
  }
}

void KeyDataPrefsDelegate::UpdatePrefsByProject(uint64_t project_name_hash,
                                                const KeyProto& key_proto) {
  ScopedDictPrefUpdate pref_updater(local_state_, pref_name_);

  base::Value::Dict& dict = pref_updater.Get();

  // Get the name of the project for |project_name_hash| to be used to store the
  // keys in prefs.
  const validator::Validators* validators = validator::Validators::Get();
  std::optional<std::string_view> project_name =
      validators->GetProjectName(project_name_hash);

  if (!project_name.has_value()) {
    LOG(ERROR) << "Attempting to store key for invalid project: "
               << project_name_hash;
    return;
  }

  dict.Set(*project_name, util::CreateValueFromKeyProto(key_proto));
}

}  // namespace metrics::structured