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 #ifndef COMPONENTS_METRICS_STRUCTURED_KEY_DATA_PROVIDER_FILE_H_ 6 #define COMPONENTS_METRICS_STRUCTURED_KEY_DATA_PROVIDER_FILE_H_ 7 8 #include <memory> 9 #include <optional> 10 11 #include "base/files/file_path.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/time/time.h" 14 #include "components/metrics/structured/key_data_provider.h" 15 16 namespace metrics::structured { 17 18 // KeyDataProvider implementation that stores the keys in a file. 19 class KeyDataProviderFile : public KeyDataProvider, KeyDataProvider::Observer { 20 public: 21 KeyDataProviderFile(const base::FilePath& file_path, 22 base::TimeDelta write_delay); 23 ~KeyDataProviderFile() override; 24 25 // KeyDataProvider: 26 bool IsReady() override; 27 void OnProfileAdded(const base::FilePath& profile_path) override; 28 std::optional<uint64_t> GetId(const std::string& project_name) override; 29 std::optional<uint64_t> GetSecondaryId( 30 const std::string& project_name) override; 31 KeyData* GetKeyData(const std::string& project_name) override; 32 void Purge() override; 33 34 // KeyDataProvider::Observer: 35 void OnKeyReady() override; 36 37 private: 38 const base::FilePath file_path_; 39 const base::TimeDelta write_delay_; 40 bool is_data_loaded_ = false; 41 42 std::unique_ptr<KeyData> key_data_; 43 base::OnceClosure on_key_ready_callback_; 44 45 base::WeakPtrFactory<KeyDataProviderFile> weak_ptr_factory_{this}; 46 }; 47 48 } // namespace metrics::structured 49 50 #endif // COMPONENTS_METRICS_STRUCTURED_KEY_DATA_PROVIDER_FILE_H_ 51