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