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/key_data_provider_file.h" 6 7 #include "base/functional/bind.h" 8 #include "base/logging.h" 9 #include "components/metrics/structured/recorder.h" 10 #include "components/metrics/structured/structured_metrics_validator.h" 11 12 namespace metrics::structured { 13 KeyDataProviderFile(const base::FilePath & file_path,base::TimeDelta write_delay)14KeyDataProviderFile::KeyDataProviderFile(const base::FilePath& file_path, 15 base::TimeDelta write_delay) 16 : file_path_(file_path), write_delay_(write_delay) { 17 key_data_ = 18 std::make_unique<KeyData>(file_path_, write_delay_, 19 base::BindOnce(&KeyDataProviderFile::OnKeyReady, 20 weak_ptr_factory_.GetWeakPtr())); 21 } 22 23 KeyDataProviderFile::~KeyDataProviderFile() = default; 24 IsReady()25bool KeyDataProviderFile::IsReady() { 26 return is_data_loaded_; 27 } 28 OnKeyReady()29void KeyDataProviderFile::OnKeyReady() { 30 is_data_loaded_ = true; 31 NotifyKeyReady(); 32 } 33 GetId(const std::string & project_name)34std::optional<uint64_t> KeyDataProviderFile::GetId( 35 const std::string& project_name) { 36 DCHECK(IsReady()); 37 38 // Validates the project. If valid, retrieve the metadata associated 39 // with the event. 40 auto maybe_project_validator = 41 validator::Validators::Get()->GetProjectValidator(project_name); 42 43 if (!maybe_project_validator.has_value()) { 44 return std::nullopt; 45 } 46 const auto* project_validator = maybe_project_validator.value(); 47 return key_data_->Id(project_validator->project_hash(), 48 project_validator->key_rotation_period()); 49 } 50 GetSecondaryId(const std::string & project_name)51std::optional<uint64_t> KeyDataProviderFile::GetSecondaryId( 52 const std::string& project_name) { 53 return std::nullopt; 54 } 55 GetKeyData(const std::string & project_name)56KeyData* KeyDataProviderFile::GetKeyData(const std::string& project_name) { 57 DCHECK(IsReady()); 58 return key_data_.get(); 59 } 60 61 // No-op. OnProfileAdded(const base::FilePath & profile_path)62void KeyDataProviderFile::OnProfileAdded(const base::FilePath& profile_path) {} 63 Purge()64void KeyDataProviderFile::Purge() { 65 if (IsReady()) { 66 key_data_->Purge(); 67 } 68 } 69 70 } // namespace metrics::structured 71