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_H_ 6 #define COMPONENTS_METRICS_STRUCTURED_KEY_DATA_PROVIDER_H_ 7 8 #include <optional> 9 10 #include "base/functional/callback_forward.h" 11 #include "base/observer_list.h" 12 #include "base/observer_list_types.h" 13 #include "components/metrics/structured/key_data.h" 14 15 namespace base { 16 class FilePath; 17 } 18 19 namespace metrics::structured { 20 21 // Interface to provide key data to be used for hashing projects. 22 // 23 // There are two types of keys: device keys and profile keys. Device keys will 24 // be ready only InitializeDeviceKey has been called while profile keys should 25 // be ready once InitializeProfileKey has been called. 26 class KeyDataProvider { 27 public: 28 // Observer to be notified of events regarding the KeyDataProvider state. 29 class Observer : public base::CheckedObserver { 30 public: 31 // Called when a key is ready to be used. 32 virtual void OnKeyReady() = 0; 33 }; 34 35 KeyDataProvider(); 36 37 KeyDataProvider(const KeyDataProvider& key_data_provider) = delete; 38 KeyDataProvider& operator=(const KeyDataProvider& key_data_provider) = delete; 39 40 virtual ~KeyDataProvider(); 41 42 void AddObserver(Observer* observer); 43 void RemoveObserver(Observer* observer); 44 45 // Returns true if the keys are ready to be used. 46 virtual bool IsReady() = 0; 47 48 // Called whenever a profile is added. 49 virtual void OnProfileAdded(const base::FilePath& profile_path) = 0; 50 51 // Retrieves the ID for given |project_name|. 52 // 53 // If no valid key is found for |project_name|, this function will return 54 // absl::nullopt. 55 virtual std::optional<uint64_t> GetId(const std::string& project_name) = 0; 56 57 // Retrieves the secondary ID for given |project_name|. 58 // 59 // If no valid secondary key is found for |project_name|, this function will 60 // return absl::nullopt. 61 // 62 // TODO(b/290096302): Refactor event sequence populator so there is no 63 // dependency on concepts such as device/profile in //components. 64 virtual std::optional<uint64_t> GetSecondaryId( 65 const std::string& project_name) = 0; 66 67 // Retrieves the key data to be used for |project_name|. Returns nullptr if 68 // the KeyData is not available for given |project_name|. 69 virtual KeyData* GetKeyData(const std::string& project_name) = 0; 70 71 // Deletes all key data associated with the provider. 72 virtual void Purge() = 0; 73 74 protected: 75 // Notifies observers that the key is ready. 76 void NotifyKeyReady(); 77 78 private: 79 base::ObserverList<Observer> observers_; 80 }; 81 82 } // namespace metrics::structured 83 84 #endif // COMPONENTS_METRICS_STRUCTURED_KEY_DATA_PROVIDER_H_ 85