// 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 CHROMEOS_ASH_COMPONENTS_REPORT_DEVICE_METRICS_CHURN_OBSERVATION_IMPL_H_
#define CHROMEOS_ASH_COMPONENTS_REPORT_DEVICE_METRICS_CHURN_OBSERVATION_IMPL_H_
#include "base/component_export.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/ash/components/report/device_metrics/use_case/use_case.h"
namespace network {
class SimpleURLLoader;
} // namespace network
namespace ash::report::device_metrics {
class ActiveStatus;
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_REPORT) ObservationImpl
: public UseCase {
public:
explicit ObservationImpl(UseCaseParameters* params);
ObservationImpl(const ObservationImpl&) = delete;
ObservationImpl& operator=(const ObservationImpl&) = delete;
~ObservationImpl() override;
// UseCase:
void Run(base::OnceCallback<void()> callback) override;
// Used by ReportController to destruct pending callbacks appropriately.
base::WeakPtr<ObservationImpl> GetWeakPtr();
// Testing
std::optional<FresnelImportDataRequest> GenerateImportRequestBodyForTesting();
protected:
// UseCase:
void CheckMembershipOprf() override;
void OnCheckMembershipOprfComplete(
std::unique_ptr<std::string> response_body) override;
void CheckMembershipQuery(
const private_membership::rlwe::PrivateMembershipRlweOprfResponse&
oprf_response) override;
void OnCheckMembershipQueryComplete(
std::unique_ptr<std::string> response_body) override;
void CheckIn() override;
void OnCheckInComplete(std::unique_ptr<std::string> response_body) override;
base::Time GetLastPingTimestamp() override;
void SetLastPingTimestamp(base::Time ts) override;
std::vector<private_membership::rlwe::RlwePlaintextId>
GetPsmIdentifiersToQuery() override;
std::optional<FresnelImportDataRequest> GenerateImportRequestBody() override;
private:
// Grant friend access for comprehensive testing of private/protected members.
friend class ObservationImplTestBase;
friend class ObservationImplDirectCheckInTest;
// Generates the data that is transmitted with an observation period.
std::optional<FresnelImportData> GenerateObservationImportData(int period);
// Update the churn local state fields based on the new active status value.
void UpdateLocalStateOnCheckInSuccess();
// Check if the device needs to ping today.
// Observation ping is only sent if cohort ping was successful in this month.
bool IsDevicePingRequired();
// Maintains callback that is executed once this use case is done running.
base::OnceCallback<void()> callback_;
// Maintain helper to manage the 28 bit active status bits, used for churn.
std::unique_ptr<ActiveStatus> active_status_;
// Manage Oprf, Query, and Import network requests on a single sequence.
std::unique_ptr<network::SimpleURLLoader> url_loader_;
// Automatically cancels callbacks when the referent of weakptr gets
// destroyed.
base::WeakPtrFactory<ObservationImpl> weak_factory_{this};
};
} // namespace ash::report::device_metrics
#endif // CHROMEOS_ASH_COMPONENTS_REPORT_DEVICE_METRICS_CHURN_OBSERVATION_IMPL_H_