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