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

#include "third_party/blink/public/common/global_privacy_control/global_privacy_control_util.h"

#include "base/feature_list.h"
#include "base/metrics/histogram_macros.h"
#include "base/rand_util.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/renderer_preferences/renderer_preferences.h"

namespace blink {
namespace {
// UMA key for GPC source samples.
constexpr char kGlobalPrivacyControlSourceHistogram[] =
    "Network.GlobalPrivacyControlSource.Subsampled";

// Subsampling probability for the GPC source histogram.
inline constexpr double kGlobalPrivacyControlSourceHistogramSampleProbability =
    0.001;
}  // namespace

bool IsGlobalPrivacyControlFeatureEnabled() {
  // TODO(crbug.com/40745270): `kGlobalPrivacyControlForce` currently enables
  // this but it should be removed once we have a real setting to test.
  return base::FeatureList::IsEnabled(features::kGlobalPrivacyControlForce) ||
         base::FeatureList::IsEnabled(features::kGlobalPrivacyControlTest);
}

bool IsGlobalPrivacyControlFeatureAndSettingEnabled(
    const RendererPreferences& renderer_preferences) {
  // TODO(crbug.com/40745270): `kGlobalPrivacyControlForce` currently enables
  // this but it should be removed once we have a real setting to test.
  if (base::FeatureList::IsEnabled(features::kGlobalPrivacyControlForce)) {
    return true;
  }
  return renderer_preferences.is_global_privacy_control_setting_enabled &&
         base::FeatureList::IsEnabled(features::kGlobalPrivacyControlTest);
}

void MaybeRecordGlobalPrivacyControlSourceMetric(
    GPCSignalSourceType source_type) {
  if (base::FeatureList::IsEnabled(
          features::kGlobalPrivacyControlAlwaysSample) ||
      base::ShouldRecordSubsampledMetric(
          kGlobalPrivacyControlSourceHistogramSampleProbability)) {
    UMA_HISTOGRAM_ENUMERATION(kGlobalPrivacyControlSourceHistogram,
                              source_type);
  }
}

}  // namespace blink
