[go: nahoru, domu]

blob: b80d294699d840f4a07fef0df1513750831fa065 [file] [log] [blame]
Avi Drissman4e1b7bc2022-09-15 14:03:501// Copyright 2021 The Chromium Authors
Alex Turner7b0c6b62021-10-05 18:34:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_AGGREGATION_SERVICE_AGGREGATABLE_REPORT_ASSEMBLER_H_
6#define CONTENT_BROWSER_AGGREGATION_SERVICE_AGGREGATABLE_REPORT_ASSEMBLER_H_
7
8#include <stddef.h>
9#include <stdint.h>
10
Alex Turner7b0c6b62021-10-05 18:34:3911#include <memory>
Dan McArdle08ad6112023-11-21 20:39:4712#include <optional>
Alex Turner7b0c6b62021-10-05 18:34:3913#include <vector>
14
Alex Turner7b0c6b62021-10-05 18:34:3915#include "base/containers/flat_map.h"
Avi Drissmanadac21992023-01-11 23:46:3916#include "base/functional/callback.h"
Alex Turner7b0c6b62021-10-05 18:34:3917#include "content/browser/aggregation_service/aggregatable_report.h"
18#include "content/browser/aggregation_service/aggregation_service_key_fetcher.h"
19#include "content/browser/aggregation_service/public_key.h"
20#include "content/common/content_export.h"
Alex Turner7b0c6b62021-10-05 18:34:3921
Nan Lin66d388d2021-10-20 16:03:0922template <class T>
23class scoped_refptr;
24
25namespace network {
26class SharedURLLoaderFactory;
27} // namespace network
28
Alex Turner7b0c6b62021-10-05 18:34:3929namespace content {
30
Nan Lin89244d62021-11-12 21:38:1731class AggregationServiceStorageContext;
Alex Turner7b0c6b62021-10-05 18:34:3932class StoragePartition;
33
34// This class provides an interface for assembling an aggregatable report. It is
35// therefore responsible for taking a request, identifying and requesting the
36// appropriate public keys, and generating and returning the AggregatableReport.
37class CONTENT_EXPORT AggregatableReportAssembler {
38 public:
Nan Lind637a9c92022-02-24 02:48:0239 // These values are persisted to logs. Entries should not be renumbered and
40 // numeric values should never be reused.
Alex Turner7b0c6b62021-10-05 18:34:3941 enum class AssemblyStatus {
Nan Lind637a9c92022-02-24 02:48:0242 kOk = 0,
Alex Turner7b0c6b62021-10-05 18:34:3943
44 // The attempt to fetch a public key failed.
Nan Lind637a9c92022-02-24 02:48:0245 kPublicKeyFetchFailed = 1,
Alex Turner7b0c6b62021-10-05 18:34:3946
47 // An internal error occurred while attempting to construct the report.
Nan Lind637a9c92022-02-24 02:48:0248 kAssemblyFailed = 2,
Alex Turner7b0c6b62021-10-05 18:34:3949
50 // The limit on the number of simultenous requests has been reached.
Nan Lind637a9c92022-02-24 02:48:0251 kTooManySimultaneousRequests = 3,
Alex Turner7b0c6b62021-10-05 18:34:3952 kMaxValue = kTooManySimultaneousRequests,
53 };
54
55 using AssemblyCallback =
Nan Lin55ad2a5a2022-08-15 21:32:5756 base::OnceCallback<void(AggregatableReportRequest,
Dan McArdle08ad6112023-11-21 20:39:4757 std::optional<AggregatableReport>,
Alex Turner7b0c6b62021-10-05 18:34:3958 AssemblyStatus)>;
59
60 // While we shouldn't hit these limits in typical usage, we protect against
61 // the possibility of unbounded memory growth
62 static constexpr size_t kMaxSimultaneousRequests = 1000;
63
Nan Lin89244d62021-11-12 21:38:1764 AggregatableReportAssembler(AggregationServiceStorageContext* storage_context,
Nan Lin66d388d2021-10-20 16:03:0965 StoragePartition* storage_partition);
Alex Turner7b0c6b62021-10-05 18:34:3966 // Not copyable or movable.
67 AggregatableReportAssembler(const AggregatableReportAssembler& other) =
68 delete;
69 AggregatableReportAssembler& operator=(
70 const AggregatableReportAssembler& other) = delete;
71 virtual ~AggregatableReportAssembler();
72
73 static std::unique_ptr<AggregatableReportAssembler> CreateForTesting(
74 std::unique_ptr<AggregationServiceKeyFetcher> fetcher,
75 std::unique_ptr<AggregatableReport::Provider> report_provider);
76
Nan Lin66d388d2021-10-20 16:03:0977 // Used by the aggregation service tool to inject a `url_loader_factory` to
78 // AggregationServiceNetworkFetcherImpl if one is provided.
79 static std::unique_ptr<AggregatableReportAssembler> CreateForTesting(
Nan Lin89244d62021-11-12 21:38:1780 AggregationServiceStorageContext* storage_context,
Alex Turner8868723f2022-03-08 00:55:2581 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
82 bool enable_debug_logging);
Nan Lin66d388d2021-10-20 16:03:0983
Alex Turner7b0c6b62021-10-05 18:34:3984 // Fetches the necessary public keys and uses it to construct an
85 // AggregatableReport from the information in `report_request`. See the
86 // AggregatableReport documentation for more detail on the returned report.
Nan Lin89244d62021-11-12 21:38:1787 virtual void AssembleReport(AggregatableReportRequest report_request,
88 AssemblyCallback callback);
89
90 protected:
91 // For testing only.
92 AggregatableReportAssembler(
93 AggregationServiceStorageContext* storage_context,
Alex Turner8868723f2022-03-08 00:55:2594 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
95 bool enable_debug_logging = false);
Alex Turner7b0c6b62021-10-05 18:34:3996
97 private:
98 // Represents a request to assemble a report that has not completed.
99 struct PendingRequest {
100 PendingRequest(AggregatableReportRequest report_request,
101 AssemblyCallback callback,
Nan Lin4c75cd82022-02-18 23:39:34102 size_t num_processing_urls);
Alex Turner7b0c6b62021-10-05 18:34:39103 // Move-only.
104 PendingRequest(PendingRequest&& other);
105 PendingRequest& operator=(PendingRequest&& other);
106 ~PendingRequest();
107
108 AggregatableReportRequest report_request;
109 AssemblyCallback callback;
110
111 // How many key fetches for this request have returned, including errors.
112 size_t num_returned_key_fetches = 0;
113
114 // The PublicKey returned for each key fetch request. Indices correspond to
Nan Lin4c75cd82022-02-18 23:39:34115 // the ordering of `report_request.processing_urls`. Each element is
Dan McArdle08ad6112023-11-21 20:39:47116 // `std::nullopt` if that key fetch either has not yet returned or has
Alex Turner7b0c6b62021-10-05 18:34:39117 // returned an error.
Dan McArdle08ad6112023-11-21 20:39:47118 std::vector<std::optional<PublicKey>> processing_url_keys;
Alex Turner7b0c6b62021-10-05 18:34:39119 };
120
121 AggregatableReportAssembler(
122 std::unique_ptr<AggregationServiceKeyFetcher> fetcher,
123 std::unique_ptr<AggregatableReport::Provider> report_provider);
124
Alex Turner7b0c6b62021-10-05 18:34:39125 // Called when a result is returned from the key fetcher. Handles throwing
126 // errors on a failed fetch, waiting for both results to return and calling
Alex Turner81c59162022-03-15 21:50:12127 // into `OnAllPublicKeysFetched()` when appropriate. `processing_url_index` is
128 // an index into the corresponding AggregatableReportRequest's
129 // `processing_urls` vector, indicating which URL this fetch is for.
Alex Turner7b0c6b62021-10-05 18:34:39130 void OnPublicKeyFetched(
131 int64_t report_id,
Nan Lin4c75cd82022-02-18 23:39:34132 size_t processing_url_index,
Dan McArdle08ad6112023-11-21 20:39:47133 std::optional<PublicKey> key,
Alex Turner7b0c6b62021-10-05 18:34:39134 AggregationServiceKeyFetcher::PublicKeyFetchStatus status);
135
136 // Call when all results have been returned from the key fetcher. Handles
137 // calling into `AssembleReportUsingKeys()` when appropriate and returning
138 // any assembled report or throwing an error if assembly fails.
139 void OnAllPublicKeysFetched(int64_t report_id,
140 PendingRequest& pending_request);
141
142 // Keyed by a token for easier lookup.
143 base::flat_map<int64_t, PendingRequest> pending_requests_;
144
145 // Used to generate unique ids for PendingRequests. These need to be unique
146 // per Assembler for tracking pending requests.
147 int64_t unique_id_counter_ = 0;
148
149 std::unique_ptr<AggregationServiceKeyFetcher> fetcher_;
150 std::unique_ptr<AggregatableReport::Provider> report_provider_;
151};
152
153} // namespace content
154
155#endif // CONTENT_BROWSER_AGGREGATION_SERVICE_AGGREGATABLE_REPORT_ASSEMBLER_H_