// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/enterprise/device_trust/core/signals/signals_service_impl.h"

#include <memory>
#include <utility>

#include "base/barrier_closure.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/task/bind_post_task.h"
#include "base/values.h"
#include "components/enterprise/device_trust/core/metrics_utils.h"
#include "components/enterprise/device_trust/core/signals/decorators/common/signals_decorator.h"
#include "components/enterprise/device_trust/core/signals/signals_filterer.h"

namespace enterprise_connectors {

namespace {

constexpr char kLatencyHistogramVariant[] = "Full";

}  // namespace

SignalsServiceImpl::SignalsServiceImpl(
    std::vector<std::unique_ptr<SignalsDecorator>> signals_decorators,
    std::unique_ptr<SignalsFilterer> signals_filterer)
    : signals_decorators_(std::move(signals_decorators)),
      signals_filterer_(std::move(signals_filterer)) {
  CHECK(signals_filterer_);
}

SignalsServiceImpl::~SignalsServiceImpl() = default;

void SignalsServiceImpl::CollectSignals(CollectSignalsCallback callback) {
  auto start_time = base::TimeTicks::Now();
  auto signals = std::make_unique<base::DictValue>();
  auto* signals_ptr = signals.get();

  auto barrier_closure = base::BarrierClosure(
      signals_decorators_.size(),
      base::BindPostTaskToCurrentDefault(
          base::BindOnce(&SignalsServiceImpl::OnSignalsDecorated,
                         weak_ptr_factory_.GetWeakPtr(), std::move(callback),
                         start_time, std::move(signals))));

  for (const auto& decorator : signals_decorators_) {
    decorator->Decorate(*signals_ptr, barrier_closure);
  }
}

void SignalsServiceImpl::OnSignalsDecorated(
    CollectSignalsCallback callback,
    base::TimeTicks start_time,
    std::unique_ptr<base::DictValue> signals) {
  LogSignalsCollectionLatency(kLatencyHistogramVariant, start_time);

  if (!signals) {
    base::DictValue empty_dictionary;
    // Must be the last statement: the callback may destroy 'this'.
    std::move(callback).Run(std::move(empty_dictionary));
  } else {
    signals_filterer_->Filter(*signals);
    // Must be the last statement: the callback may destroy 'this'.
    std::move(callback).Run(std::move(*signals));
  }
}

}  // namespace enterprise_connectors
