chromium/content/browser/attribution_reporting/attribution_manager_impl.cc

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

#include "content/browser/attribution_reporting/attribution_manager_impl.h"

#include <stddef.h>

#include <cmath>
#include <functional>
#include <optional>
#include <set>
#include <utility>
#include <vector>

#include "base/barrier_closure.h"
#include "base/check.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/functional/overloaded.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ref.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/observer_list.h"
#include "base/ranges/algorithm.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/task/updateable_sequenced_task_runner.h"
#include "base/threading/sequence_bound.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "components/attribution_reporting/aggregatable_filtering_id_max_bytes.h"
#include "components/attribution_reporting/constants.h"
#include "components/attribution_reporting/os_registration.h"
#include "components/attribution_reporting/registration.mojom.h"
#include "components/attribution_reporting/source_registration.h"
#include "components/attribution_reporting/suitable_origin.h"
#include "components/attribution_reporting/trigger_registration.h"
#include "content/browser/aggregation_service/aggregation_service.h"
#include "content/browser/aggregation_service/aggregation_service_impl.h"
#include "content/browser/aggregation_service/report_scheduler_timer.h"
#include "content/browser/attribution_reporting/aggregatable_attribution_utils.h"
#include "content/browser/attribution_reporting/aggregatable_debug_report.h"
#include "content/browser/attribution_reporting/attribution_cookie_checker.h"
#include "content/browser/attribution_reporting/attribution_cookie_checker_impl.h"
#include "content/browser/attribution_reporting/attribution_data_host_manager.h"
#include "content/browser/attribution_reporting/attribution_data_host_manager_impl.h"
#include "content/browser/attribution_reporting/attribution_debug_report.h"
#include "content/browser/attribution_reporting/attribution_features.h"
#include "content/browser/attribution_reporting/attribution_info.h"
#include "content/browser/attribution_reporting/attribution_observer.h"
#include "content/browser/attribution_reporting/attribution_os_level_manager.h"
#include "content/browser/attribution_reporting/attribution_report.h"
#include "content/browser/attribution_reporting/attribution_report_network_sender.h"
#include "content/browser/attribution_reporting/attribution_report_sender.h"
#include "content/browser/attribution_reporting/attribution_reporting.mojom.h"
#include "content/browser/attribution_reporting/attribution_resolver.h"
#include "content/browser/attribution_reporting/attribution_resolver_delegate.h"
#include "content/browser/attribution_reporting/attribution_resolver_delegate_impl.h"
#include "content/browser/attribution_reporting/attribution_resolver_impl.h"
#include "content/browser/attribution_reporting/attribution_trigger.h"
#include "content/browser/attribution_reporting/create_report_result.h"
#include "content/browser/attribution_reporting/os_registration.h"
#include "content/browser/attribution_reporting/send_result.h"
#include "content/browser/attribution_reporting/storable_source.h"
#include "content/browser/attribution_reporting/store_source_result.h"
#include "content/browser/attribution_reporting/stored_source.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/attribution_data_model.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browsing_data_filter_builder.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "net/base/schemeful_site.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/mojom/network_change_manager.mojom-forward.h"
#include "storage/browser/quota/special_storage_policy.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/gurl.h"
#include "url/origin.h"

#if BUILDFLAG(IS_ANDROID)
#include "content/browser/attribution_reporting/attribution_os_level_manager_android.h"
#endif

namespace content {

namespace {

ScopedUseInMemoryStorageForTesting;

OsRegistrationItem;
OsRegistrationResult;
RegistrationType;

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// LINT.IfChange(ConversionReportSendOutcome)
enum class ConversionReportSendOutcome {};
// LINT.ThenChange(//tools/metrics/histograms/metadata/attribution_reporting/enums.xml:ConversionReportSendOutcome)

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// LINT.IfChange(ConversionReportSendRetryCount)
enum class ConversionReportSendRetryCount {};
// LINT.ThenChange(//tools/metrics/histograms/metadata/attribution_reporting/enums.xml:ConversionReportSendRetryCount)

const base::TimeDelta kPrivacySandboxAttestationsTimeout =;

}  // namespace

// This class consolidates logic regarding when to schedule the browser to send
// attribution reports. It talks directly to the `AttributionResolver` to help
// make these decisions.
//
// While the class does not make large changes to the underlying database, it
// is responsible for notifying the `AttributionResolver` when the browser comes
// back online, which mutates report times for some scheduled reports.
class AttributionManagerImpl::ReportScheduler
    : public ReportSchedulerTimer::Delegate {};

namespace {

bool IsStorageKeySessionOnly(
    scoped_refptr<storage::SpecialStoragePolicy> storage_policy,
    const blink::StorageKey& storage_key) {}

const base::TimeDelta ReportRetryDelay(bool is_first_retry) {}

void RecordStoreSourceStatus(const StoreSourceResult& result) {}

void RecordCreateReportStatus(const CreateReportResult& result) {}

// If `retry_attempts` <= 2, represents the number of retries before success.
// If `retry_attempts == 3`, represents failure after two retries.
void RecordReportRetriesEventLevel(int retry_attempts) {}
void RecordReportRetriesAggregatable(int retry_attempts) {}

ConversionReportSendOutcome ConvertToConversionReportSendOutcome(
    SendResult::Status status) {}

void RecordNetworkConnectionTypeOnFailure(
    AttributionReport::Type report_type,
    network::mojom::ConnectionType connection_type) {}

void RecordAssembleAggregatableReportStatus(
    AssembleAggregatableReportStatus status) {}

void LogAggregatableReportHistogramCustomTimes(const char* suffix,
                                               bool has_trigger_context_id,
                                               base::TimeDelta sample,
                                               base::TimeDelta min,
                                               base::TimeDelta max,
                                               size_t buckets) {}

// Called when |report| is to be sent over network for event-level reports or
// to be assembled for aggregatable reports, for logging metrics.
void LogMetricsOnReportSend(const AttributionReport& report, base::Time now) {}

// Called when |report| is sent, failed or dropped, for logging metrics.
void LogMetricsOnReportCompleted(const AttributionReport& report,
                                 SendResult::Status status) {}

// Called when `report` is sent successfully.
void LogMetricsOnReportSent(const AttributionReport& report) {}

bool HasNonDefaultFilteringId(const AttributionTrigger& trigger) {}

void RecordAggregatableFilteringIdUsage(const AttributionTrigger& trigger) {}

std::unique_ptr<AttributionResolverDelegate> MakeResolverDelegate(
    bool debug_mode) {}

bool IsOperationAllowed(
    StoragePartitionImpl& storage_partition,
    ContentBrowserClient::AttributionReportingOperation operation,
    content::RenderFrameHost* rfh,
    const url::Origin* source_origin,
    const url::Origin* destination_origin,
    const url::Origin* reporting_origin,
    bool* can_bypass = nullptr) {}

std::unique_ptr<AttributionOsLevelManager> CreateOsLevelManager() {}

// Returns new report time if any.
std::optional<base::Time> HandleTransientFailureOnSendReport(
    const AttributionReport& report) {}

bool g_run_in_memory =;

}  // namespace

struct AttributionManagerImpl::PendingReportTimings {};

struct AttributionManagerImpl::SourceOrTriggerRFH {};

std::optional<base::TimeDelta> GetFailedReportDelay(int failed_send_attempts) {}

ScopedUseInMemoryStorageForTesting::ScopedUseInMemoryStorageForTesting()
    :{}

ScopedUseInMemoryStorageForTesting::~ScopedUseInMemoryStorageForTesting() {}

bool AttributionManagerImpl::IsReportAllowed(
    const AttributionReport& report) const {}

// static
std::unique_ptr<AttributionManagerImpl>
AttributionManagerImpl::CreateForTesting(
    const base::FilePath& user_data_directory,
    size_t max_pending_events,
    scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy,
    std::unique_ptr<AttributionResolverDelegate> resolver_delegate,
    std::unique_ptr<AttributionCookieChecker> cookie_checker,
    std::unique_ptr<AttributionReportSender> report_sender,
    std::unique_ptr<AttributionOsLevelManager> os_level_manager,
    StoragePartitionImpl* storage_partition,
    scoped_refptr<base::UpdateableSequencedTaskRunner> resolver_task_runner) {}

AttributionManagerImpl::AttributionManagerImpl(
    StoragePartitionImpl* storage_partition,
    const base::FilePath& user_data_directory,
    scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy)
    :{}

AttributionManagerImpl::AttributionManagerImpl(
    StoragePartitionImpl* storage_partition,
    const base::FilePath& user_data_directory,
    size_t max_pending_events,
    scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy,
    std::unique_ptr<AttributionResolverDelegate> resolver_delegate,
    std::unique_ptr<AttributionCookieChecker> cookie_checker,
    std::unique_ptr<AttributionReportSender> report_sender,
    std::unique_ptr<AttributionOsLevelManager> os_level_manager,
    scoped_refptr<base::UpdateableSequencedTaskRunner> resolver_task_runner,
    bool debug_mode)
    :{}

AttributionManagerImpl::~AttributionManagerImpl() {}

void AttributionManagerImpl::AddObserver(AttributionObserver* observer) {}

void AttributionManagerImpl::RemoveObserver(AttributionObserver* observer) {}

AttributionDataHostManager* AttributionManagerImpl::GetDataHostManager() {}

void AttributionManagerImpl::HandleSource(
    StorableSource source,
    GlobalRenderFrameHostId render_frame_id) {}

void AttributionManagerImpl::RecordPendingAggregatableReportsTimings() {}

void AttributionManagerImpl::OnSourceStored(
    std::optional<uint64_t> cleared_debug_key,
    StoreSourceResult result) {}

void AttributionManagerImpl::HandleTrigger(
    AttributionTrigger trigger,
    GlobalRenderFrameHostId render_frame_id) {}

void AttributionManagerImpl::StoreTrigger(AttributionTrigger trigger,
                                          bool is_debug_cookie_set) {}

void AttributionManagerImpl::MaybeEnqueueEvent(SourceOrTriggerRFH event) {}

void AttributionManagerImpl::PrepareNextEvent() {}

void AttributionManagerImpl::ProcessNextEvent(bool registration_allowed,
                                              bool is_debug_cookie_set) {}

void AttributionManagerImpl::StoreSource(StorableSource source) {}

void AttributionManagerImpl::AddPendingAggregatableReportTiming(
    const AttributionReport& report) {}

void AttributionManagerImpl::OnReportStored(
    std::optional<uint64_t> cleared_debug_key,
    bool is_debug_cookie_set,
    CreateReportResult result) {}

void AttributionManagerImpl::MaybeSendDebugReport(AttributionReport&& report) {}

void AttributionManagerImpl::GetActiveSourcesForWebUI(
    base::OnceCallback<void(std::vector<StoredSource>)> callback) {}

void AttributionManagerImpl::GetPendingReportsForInternalUse(
    int limit,
    base::OnceCallback<void(std::vector<AttributionReport>)> callback) {}

void AttributionManagerImpl::SendReportForWebUI(AttributionReport::Id id,
                                                base::OnceClosure done) {}

void AttributionManagerImpl::ClearData(
    base::Time delete_begin,
    base::Time delete_end,
    StoragePartition::StorageKeyMatcherFunction filter,
    BrowsingDataFilterBuilder* filter_builder,
    bool delete_rate_limit_data,
    base::OnceClosure done) {}

void AttributionManagerImpl::OnUserVisibleTaskStarted() {}

void AttributionManagerImpl::OnUserVisibleTaskComplete() {}

void AttributionManagerImpl::OnClearDataComplete(bool was_user_visible) {}

void AttributionManagerImpl::GetAllDataKeys(
    base::OnceCallback<void(std::set<DataKey>)> callback) {}

void AttributionManagerImpl::RemoveAttributionDataByDataKey(
    const DataKey& data_key,
    base::OnceClosure callback) {}

void AttributionManagerImpl::GetReportsToSend() {}

void AttributionManagerImpl::OnGetReportToSendFromWebUI(
    base::OnceClosure done,
    std::optional<AttributionReport> report) {}

void AttributionManagerImpl::SendReports(
    std::vector<AttributionReport> reports) {}

// If `web_ui_callback` is null, assumes that `report` is being sent at its
// intended time, and logs metrics for it. Otherwise, does not log metrics.
void AttributionManagerImpl::SendReport(base::OnceClosure web_ui_callback,
                                        const base::Time now,
                                        AttributionReport report) {}

void AttributionManagerImpl::MarkReportCompleted(
    AttributionReport::Id report_id) {}

void AttributionManagerImpl::PrepareToSendReport(AttributionReport report,
                                                 bool is_debug_report,
                                                 ReportSentCallback callback) {}

void AttributionManagerImpl::SendReport(AttributionReport report,
                                        bool is_debug_report,
                                        ReportSentCallback callback) {}

void AttributionManagerImpl::OnReportSent(base::OnceClosure done,
                                          const AttributionReport& report,
                                          SendResult info) {}

void AttributionManagerImpl::NotifyReportSent(bool is_debug_report,
                                              const AttributionReport& report,
                                              SendResult info) {}

void AttributionManagerImpl::NotifyDebugReportSent(
    const AttributionDebugReport& report,
    const int status) {}

void AttributionManagerImpl::AssembleAggregatableReport(
    AttributionReport report,
    bool is_debug_report,
    ReportSentCallback callback) {}

void AttributionManagerImpl::OnAggregatableReportAssembled(
    AttributionReport report,
    bool is_debug_report,
    ReportSentCallback callback,
    AggregatableReportRequest,
    std::optional<AggregatableReport> assembled_report,
    AggregationService::AssemblyStatus) {}

void AttributionManagerImpl::NotifySourcesChanged() {}

void AttributionManagerImpl::NotifyReportsChanged() {}

void AttributionManagerImpl::MaybeSendAggregatableDebugReport(
    const StoreSourceResult& result) {}

void AttributionManagerImpl::MaybeSendAggregatableDebugReport(
    const CreateReportResult& result) {}

void AttributionManagerImpl::OnAggregatableDebugReportProcessed(
    ProcessAggregatableDebugReportResult result) {}

void AttributionManagerImpl::OnAggregatableDebugReportAssembled(
    ProcessAggregatableDebugReportResult result,
    AggregatableReportRequest,
    std::optional<AggregatableReport> assembled_report,
    AggregationService::AssemblyStatus) {}

void AttributionManagerImpl::NotifyAggregatableDebugReportSent(
    const AggregatableDebugReport& report,
    base::ValueView report_body,
    attribution_reporting::mojom::ProcessAggregatableDebugReportResult
        process_result,
    SendAggregatableDebugReportResult send_result) {}

void AttributionManagerImpl::MaybeSendVerboseDebugReport(
    const StoreSourceResult& result) {}

void AttributionManagerImpl::MaybeSendVerboseDebugReport(
    bool is_debug_cookie_set,
    const CreateReportResult& result) {}

void AttributionManagerImpl::HandleOsRegistration(OsRegistration registration) {}

void AttributionManagerImpl::PrepareNextOsEvent() {}

void AttributionManagerImpl::ProcessNextOsEvent(
    const std::vector<bool>& is_debug_key_allowed) {}

void AttributionManagerImpl::NotifyTotalOsRegistrationFailure(
    const OsRegistration& registration,
    OsRegistrationResult result) {}

void AttributionManagerImpl::NotifyOsRegistration(
    base::Time time,
    const OsRegistrationItem& registration,
    const url::Origin& top_level_origin,
    bool is_debug_key_allowed,
    attribution_reporting::mojom::RegistrationType type,
    attribution_reporting::mojom::OsRegistrationResult result) {}

void AttributionManagerImpl::OnOsRegistration(
    const std::vector<bool>& is_debug_key_allowed,
    const OsRegistration& registration,
    const std::vector<bool>& success) {}

void AttributionManagerImpl::SetDebugMode(std::optional<bool> enabled,
                                          base::OnceClosure done) {}

void AttributionManagerImpl::MaybeSendVerboseDebugReports(
    const OsRegistration& registration) {}

void AttributionManagerImpl::OnAttestationsLoaded() {}

bool AttributionManagerImpl::IsReady() const {}

void AttributionManagerImpl::ReportRegistrationHeaderError(
    attribution_reporting::SuitableOrigin reporting_origin,
    const attribution_reporting::RegistrationHeaderError& error,
    const attribution_reporting::SuitableOrigin& context_origin,
    bool is_within_fenced_frame,
    GlobalRenderFrameHostId render_frame_id) {}

}  // namespace content