1 // Copyright 2024 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/key_data_provider_prefs.h" 6 7 #include <memory> 8 #include <optional> 9 #include <string_view> 10 11 #include "components/metrics/structured/key_data_prefs_delegate.h" 12 #include "components/metrics/structured/structured_metrics_validator.h" 13 14 namespace metrics::structured { 15 KeyDataProviderPrefs(PrefService * local_state,std::string_view pref_name)16KeyDataProviderPrefs::KeyDataProviderPrefs(PrefService* local_state, 17 std::string_view pref_name) 18 : key_data_( 19 std::make_unique<KeyDataPrefsDelegate>(local_state, pref_name)) {} 20 21 KeyDataProviderPrefs::~KeyDataProviderPrefs() = default; 22 IsReady()23bool KeyDataProviderPrefs::IsReady() { 24 return true; 25 } 26 GetId(const std::string & project_name)27std::optional<uint64_t> KeyDataProviderPrefs::GetId( 28 const std::string& project_name) { 29 // Validates the project. If valid, retrieve the metadata associated 30 // with the event. 31 const auto* project_validator = 32 validator::Validators::Get()->GetProjectValidator(project_name); 33 34 if (!project_validator) { 35 return std::nullopt; 36 } 37 return key_data_.Id(project_validator->project_hash(), 38 project_validator->key_rotation_period()); 39 } 40 GetKeyData(const std::string & project_name)41KeyData* KeyDataProviderPrefs::GetKeyData(const std::string& project_name) { 42 return &key_data_; 43 } 44 Purge()45void KeyDataProviderPrefs::Purge() { 46 key_data_.Purge(); 47 } 48 49 } // namespace metrics::structured 50