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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_PRIVATE_AGGREGATION_PRIVATE_AGGREGATION_HOST_H_
#define CONTENT_BROWSER_PRIVATE_AGGREGATION_PRIVATE_AGGREGATION_HOST_H_
#include <stddef.h>
#include <stdint.h>
#include <optional>
#include <string>
#include <vector>
#include "base/functional/callback.h"
#include "base/memory/raw_ref.h"
#include "base/numerics/safe_conversions.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "content/browser/aggregation_service/aggregatable_report.h"
#include "content/browser/private_aggregation/private_aggregation_budget_key.h"
#include "content/browser/private_aggregation/private_aggregation_budgeter.h"
#include "content/browser/private_aggregation/private_aggregation_caller_api.h"
#include "content/browser/private_aggregation/private_aggregation_pending_contributions.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "third_party/blink/public/common/shared_storage/shared_storage_utils.h"
#include "third_party/blink/public/mojom/private_aggregation/private_aggregation_host.mojom.h"
namespace base {
class ElapsedTimer;
class Uuid;
} // namespace base
namespace url {
class Origin;
} // namespace url
namespace content {
class AggregatableReportRequest;
class BrowserContext;
// UI thread class responsible for implementing the mojo interface used by
// worklets and renderers to request reports be sent and maintaining the
// receiver set for this interface. It is responsible for validating the
// messages received and then passing them on for budget approval.
class CONTENT_EXPORT PrivateAggregationHost
: public blink::mojom::PrivateAggregationHost {
public:
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class PipeResult {
kReportSuccess = 0,
kReportSuccessButTruncatedDueToTooManyContributions = 1,
kNoReportButNoError = 2,
kApiDisabledInSettings = 3,
kEnableDebugModeCalledMultipleTimes = 4,
kNegativeValue = 5,
kFilteringIdInvalid = 6,
kNecessaryFeatureNotEnabled = 7,
kMaxValue = kNecessaryFeatureNotEnabled,
};
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class TimeoutResult {
kOccurredBeforeRemoteDisconnection = 0,
kOccurredAfterRemoteDisconnection = 1,
kCanceledDueToError = 2,
kStillScheduledOnShutdown = 3,
kMaxValue = kStillScheduledOnShutdown,
};
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class FilteringIdStatus {
kNoFilteringIdWithDefaultMaxBytes = 0,
kFilteringIdProvidedWithDefaultMaxBytes = 1,
kNoFilteringIdWithCustomMaxBytes = 2,
kFilteringIdProvidedWithCustomMaxBytes = 3,
kMaxValue = kFilteringIdProvidedWithCustomMaxBytes,
};
// Indicates the desired behavior when a report has no contributions, such as
// when budget is denied or `contributeToHistogram()` was not called.
enum class NullReportBehavior {
// Still send a report, but without any contributions.
kSendNullReport,
// Drop the report.
kDontSendReport,
};
using ReportRequestGenerator = base::OnceCallback<AggregatableReportRequest(
std::vector<blink::mojom::AggregatableReportHistogramContribution>)>;
// Version string for the reports generated by this API.
static constexpr char kApiReportVersion[] = "1.0";
static constexpr size_t kDefaultFilteringIdMaxBytes = 1;
// The maximum allowed context_id string length.
static constexpr int kMaxContextIdLength = 64;
static_assert(kMaxContextIdLength ==
blink::kPrivateAggregationApiContextIdMaxLength,
"Maximum length of context_id should be aligned between Shared "
"Storage and Private Aggregation.");
// The duration of time that `SendReportOnTimeoutOrDisconnect()`
// unconditionally adds to the scheduled report time. Marked public for
// testing.
static constexpr base::TimeDelta kTimeForLocalProcessing =
base::Milliseconds(100);
// Returns the effective maximum number of contributions that can go in an
// `AggregatableReport` after merging. The `requested_max_contributions`
// parameter comes from the web-visible `maxContributions` field.
//
// This method is marked public for testing; this enables golden report
// unittests to match the browser's actual behavior.
static base::StrictNumeric<size_t> GetEffectiveMaxContributions(
PrivateAggregationCallerApi caller_api,
std::optional<size_t> requested_max_contributions);
// `on_report_request_details_received` and `browser_context` must be
// non-null.
PrivateAggregationHost(
base::RepeatingCallback<
void(ReportRequestGenerator,
PrivateAggregationPendingContributions::Wrapper,
PrivateAggregationBudgetKey,
NullReportBehavior)> on_report_request_details_received,
BrowserContext* browser_context);
PrivateAggregationHost(const PrivateAggregationHost&) = delete;
PrivateAggregationHost& operator=(const PrivateAggregationHost&) = delete;
~PrivateAggregationHost() override;
// See `PrivateAggregationManager::BindNewReceiver()`.
[[nodiscard]] virtual bool BindNewReceiver(
url::Origin worklet_origin,
url::Origin top_frame_origin,
PrivateAggregationCallerApi caller_api,
std::optional<std::string> context_id,
std::optional<base::TimeDelta> timeout,
std::optional<url::Origin> aggregation_coordinator_origin,
size_t filtering_id_max_bytes,
std::optional<size_t> max_contributions,
mojo::PendingReceiver<blink::mojom::PrivateAggregationHost>
pending_receiver);
bool IsDebugModeAllowed(const url::Origin& top_frame_origin,
const url::Origin& reporting_origin);
// blink::mojom::PrivateAggregationHost:
void ContributeToHistogram(
std::vector<blink::mojom::AggregatableReportHistogramContributionPtr>
contribution_ptrs) override;
void ContributeToHistogramOnEvent(
blink::mojom::PrivateAggregationErrorEvent error_event,
std::vector<blink::mojom::AggregatableReportHistogramContributionPtr>
contribution_ptrs) override;
void EnableDebugMode(blink::mojom::DebugKeyPtr debug_key) override;
void FlushReceiverSetForTesting() { receiver_set_.FlushForTesting(); }
private:
friend class PrivateAggregationReportGoldenLatestVersionTest;
struct ReceiverContext;
static AggregatableReportRequest GenerateReportRequest(
base::ElapsedTimer timeout_or_disconnect_timer,
blink::mojom::DebugModeDetailsPtr debug_mode_details,
base::Time scheduled_report_time,
AggregatableReportRequest::DelayType delay_type,
base::Uuid report_id,
const url::Origin& reporting_origin,
PrivateAggregationCallerApi caller_api,
std::optional<std::string> context_id,
std::optional<url::Origin> aggregation_coordinator_origin,
size_t specified_filtering_id_max_bytes,
size_t max_contributions,
std::vector<blink::mojom::AggregatableReportHistogramContribution>
contributions);
void CloseCurrentPipe(PipeResult pipe_result);
void OnTimeoutBeforeDisconnect(mojo::ReceiverId id);
void OnReceiverDisconnected();
void SendReportOnTimeoutOrDisconnect(
ReceiverContext& current_context,
base::TimeDelta remaining_timeout,
PrivateAggregationPendingContributions::TimeoutOrDisconnect
timeout_or_disconnect);
// Performs shared validation for `ContributeToHistogram()` and
// `ContributeToHistogramOnEvent()` calls.
bool ValidateContributeCall(
const std::vector<
blink::mojom::AggregatableReportHistogramContributionPtr>&
contribution_ptrs);
// Set iff the private aggregation developer mode is set.
bool should_not_delay_reports_;
base::RepeatingCallback<void(ReportRequestGenerator,
PrivateAggregationPendingContributions::Wrapper,
PrivateAggregationBudgetKey,
NullReportBehavior)>
on_report_request_details_received_;
mojo::ReceiverSet<blink::mojom::PrivateAggregationHost, ReceiverContext>
receiver_set_;
// `this` is indirectly owned by the StoragePartitionImpl, which itself is
// owned by `browser_context_`.
raw_ref<BrowserContext> browser_context_;
};
} // namespace content
#endif // CONTENT_BROWSER_PRIVATE_AGGREGATION_PRIVATE_AGGREGATION_HOST_H_
|