[go: nahoru, domu]

blob: 9d2a1fc455128ebc7be0548665cb7752a2045e21 [file] [log] [blame]
Avi Drissman4e1b7bc2022-09-15 14:03:501// Copyright 2021 The Chromium Authors
Nan Lin89244d62021-11-12 21:38:172// 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_AGGREGATION_SERVICE_IMPL_H_
6#define CONTENT_BROWSER_AGGREGATION_SERVICE_AGGREGATION_SERVICE_IMPL_H_
7
Nan Lin89244d62021-11-12 21:38:178#include <memory>
Dan McArdle08ad6112023-11-21 20:39:479#include <optional>
Thomas Quintanilladce5ba9b2023-06-16 18:49:4410#include <set>
Alex Turner48e25302022-07-21 20:25:3511#include <vector>
Nan Lin89244d62021-11-12 21:38:1712
Avi Drissmanadac21992023-01-11 23:46:3913#include "base/functional/callback_forward.h"
Alex Turnerae969102022-11-16 18:22:4214#include "base/memory/scoped_refptr.h"
Nan Lin6a520782022-08-15 19:45:1315#include "base/memory/weak_ptr.h"
Nan Lin55ad2a5a2022-08-15 21:32:5716#include "base/observer_list.h"
Nan Lin89244d62021-11-12 21:38:1717#include "base/threading/sequence_bound.h"
18#include "content/browser/aggregation_service/aggregatable_report_assembler.h"
Alex Turner48e25302022-07-21 20:25:3519#include "content/browser/aggregation_service/aggregatable_report_scheduler.h"
Nan Lin16e55f482021-12-10 15:05:4720#include "content/browser/aggregation_service/aggregatable_report_sender.h"
Nan Lin89244d62021-11-12 21:38:1721#include "content/browser/aggregation_service/aggregation_service.h"
Nan Lin55ad2a5a2022-08-15 21:32:5722#include "content/browser/aggregation_service/aggregation_service_observer.h"
Nan Lin6a520782022-08-15 19:45:1323#include "content/browser/aggregation_service/aggregation_service_storage.h"
Nan Lin89244d62021-11-12 21:38:1724#include "content/browser/aggregation_service/aggregation_service_storage_context.h"
25#include "content/common/content_export.h"
Alex Turnerb835b3a2022-07-21 21:42:1826#include "content/public/browser/storage_partition.h"
Nan Lin89244d62021-11-12 21:38:1727
Nan Lin8de22bc2022-04-02 02:51:1428class GURL;
29
Nan Lin89244d62021-11-12 21:38:1730namespace base {
31class Clock;
Dan McArdle84d73742023-12-05 21:23:2132class ElapsedTimer;
Nan Lin89244d62021-11-12 21:38:1733class FilePath;
Alex Turnerae969102022-11-16 18:22:4234class UpdateableSequencedTaskRunner;
Nan Lin89244d62021-11-12 21:38:1735} // namespace base
36
Thomas Quintanilladce5ba9b2023-06-16 18:49:4437namespace url {
38class Origin;
39} // namespace url
40
Nan Lin89244d62021-11-12 21:38:1741namespace content {
42
Nan Lin8de22bc2022-04-02 02:51:1443struct PublicKeyset;
Alex Turner48e25302022-07-21 20:25:3544class AggregatableReport;
Nan Lin55ad2a5a2022-08-15 21:32:5745class AggregatableReportRequest;
Alex Turner71512772022-06-29 19:20:2746class AggregationServiceStorage;
Alex Turner48e25302022-07-21 20:25:3547class AggregatableReportScheduler;
Nan Lin89244d62021-11-12 21:38:1748class StoragePartitionImpl;
49
50// UI thread class that manages the lifetime of the underlying storage. Owned by
51// the StoragePartitionImpl. Lifetime is bound to lifetime of the
52// StoragePartitionImpl.
53class CONTENT_EXPORT AggregationServiceImpl
54 : public AggregationService,
55 public AggregationServiceStorageContext {
56 public:
57 static std::unique_ptr<AggregationServiceImpl> CreateForTesting(
58 bool run_in_memory,
59 const base::FilePath& user_data_directory,
60 const base::Clock* clock,
Alex Turner48e25302022-07-21 20:25:3561 std::unique_ptr<AggregatableReportScheduler> scheduler,
Nan Lin16e55f482021-12-10 15:05:4762 std::unique_ptr<AggregatableReportAssembler> assembler,
63 std::unique_ptr<AggregatableReportSender> sender);
Nan Lin89244d62021-11-12 21:38:1764
65 AggregationServiceImpl(bool run_in_memory,
66 const base::FilePath& user_data_directory,
67 StoragePartitionImpl* storage_partition);
68 AggregationServiceImpl(const AggregationServiceImpl& other) = delete;
69 AggregationServiceImpl& operator=(const AggregationServiceImpl& other) =
70 delete;
71 AggregationServiceImpl(AggregationServiceImpl&& other) = delete;
72 AggregationServiceImpl& operator=(AggregationServiceImpl&& other) = delete;
73 ~AggregationServiceImpl() override;
74
75 // AggregationService:
76 void AssembleReport(AggregatableReportRequest report_request,
77 AssemblyCallback callback) override;
Nan Lin16e55f482021-12-10 15:05:4778 void SendReport(const GURL& url,
Nan Lina7cffd02022-01-06 15:20:2979 const AggregatableReport& report,
Nan Lin16e55f482021-12-10 15:05:4780 SendCallback callback) override;
81 void SendReport(const GURL& url,
82 const base::Value& contents,
83 SendCallback callback) override;
Nan Linb771003b2022-01-19 21:05:5384 void ClearData(base::Time delete_begin,
85 base::Time delete_end,
Alex Turnerb835b3a2022-07-21 21:42:1886 StoragePartition::StorageKeyMatcherFunction filter,
Nan Linb771003b2022-01-19 21:05:5387 base::OnceClosure done) override;
Alex Turner48e25302022-07-21 20:25:3588 void ScheduleReport(AggregatableReportRequest report_request) override;
Alex Turner4162376f2022-10-20 18:04:1089 void AssembleAndSendReport(AggregatableReportRequest report_request) override;
Nan Lin6a520782022-08-15 19:45:1390 void GetPendingReportRequestsForWebUI(
91 base::OnceCallback<
92 void(std::vector<AggregationServiceStorage::RequestAndId>)> callback)
93 override;
94 void SendReportsForWebUI(
95 const std::vector<AggregationServiceStorage::RequestId>& ids,
96 base::OnceClosure reports_sent_callback) override;
Thomas Quintanilladce5ba9b2023-06-16 18:49:4497 void GetPendingReportReportingOrigins(
98 base::OnceCallback<void(std::set<url::Origin>)> callback) override;
Nan Lin55ad2a5a2022-08-15 21:32:5799 void AddObserver(AggregationServiceObserver* observer) override;
100 void RemoveObserver(AggregationServiceObserver* observer) override;
Nan Lin89244d62021-11-12 21:38:17101
102 // AggregationServiceStorageContext:
Alex Turner71512772022-06-29 19:20:27103 const base::SequenceBound<AggregationServiceStorage>& GetStorage() override;
Nan Lin89244d62021-11-12 21:38:17104
Nan Lin8de22bc2022-04-02 02:51:14105 // Sets the public keys for `url` in storage to allow testing without network.
106 void SetPublicKeysForTesting(const GURL& url, const PublicKeyset& keyset);
107
Nan Lin89244d62021-11-12 21:38:17108 private:
Alex Turner48e25302022-07-21 20:25:35109 // Allows access to `OnScheduledReportTimeReached()`.
110 friend class AggregationServiceImplTest;
111
Nan Lin16e55f482021-12-10 15:05:47112 AggregationServiceImpl(bool run_in_memory,
113 const base::FilePath& user_data_directory,
114 const base::Clock* clock,
Alex Turner48e25302022-07-21 20:25:35115 std::unique_ptr<AggregatableReportScheduler> scheduler,
Nan Lin16e55f482021-12-10 15:05:47116 std::unique_ptr<AggregatableReportAssembler> assembler,
117 std::unique_ptr<AggregatableReportSender> sender);
Nan Lin89244d62021-11-12 21:38:17118
Alex Turner48e25302022-07-21 20:25:35119 void OnScheduledReportTimeReached(
120 std::vector<AggregationServiceStorage::RequestAndId> requests_and_ids);
Alex Turnera022ec62022-07-20 21:10:27121
Alex Turner4162376f2022-10-20 18:04:10122 void AssembleAndSendReports(
123 std::vector<AggregationServiceStorage::RequestAndId> requests_and_ids,
124 base::RepeatingClosure done);
125
Dan McArdle08ad6112023-11-21 20:39:47126 // `request_id` is `std::nullopt` iff `report_request` was not
Alex Turner4162376f2022-10-20 18:04:10127 // stored/scheduled.
128 void AssembleAndSendReportImpl(
129 AggregatableReportRequest report_request,
Dan McArdle08ad6112023-11-21 20:39:47130 std::optional<AggregationServiceStorage::RequestId> request_id,
Alex Turner4162376f2022-10-20 18:04:10131 base::OnceClosure done);
Alex Turner48e25302022-07-21 20:25:35132 void OnReportAssemblyComplete(
Nan Lin6a520782022-08-15 19:45:13133 base::OnceClosure done,
Dan McArdle08ad6112023-11-21 20:39:47134 std::optional<AggregationServiceStorage::RequestId> request_id,
Alex Turner48e25302022-07-21 20:25:35135 GURL reporting_url,
Dan McArdle84d73742023-12-05 21:23:21136 base::ElapsedTimer elapsed_timer,
Nan Lin55ad2a5a2022-08-15 21:32:57137 AggregatableReportRequest report_request,
Dan McArdle08ad6112023-11-21 20:39:47138 std::optional<AggregatableReport> report,
Alex Turner48e25302022-07-21 20:25:35139 AggregatableReportAssembler::AssemblyStatus status);
Nan Lin55ad2a5a2022-08-15 21:32:57140 void OnReportSendingComplete(
141 base::OnceClosure done,
Alex Turner4162376f2022-10-20 18:04:10142 AggregatableReportRequest report_request,
Dan McArdle08ad6112023-11-21 20:39:47143 std::optional<AggregationServiceStorage::RequestId> request_id,
Nan Lin55ad2a5a2022-08-15 21:32:57144 AggregatableReport report,
Dan McArdle84d73742023-12-05 21:23:21145 base::ElapsedTimer elapsed_timer,
Nan Lin55ad2a5a2022-08-15 21:32:57146 AggregatableReportSender::RequestStatus status);
Thomas Quintanilladce5ba9b2023-06-16 18:49:44147 void OnUserVisibleTaskStarted();
148 void OnUserVisibleTaskComplete();
Alex Turnerae969102022-11-16 18:22:42149 void OnClearDataComplete();
Alex Turner48e25302022-07-21 20:25:35150
Nan Lin6a520782022-08-15 19:45:13151 void OnGetRequestsToSendFromWebUI(
152 base::OnceClosure reports_sent_callback,
153 std::vector<AggregationServiceStorage::RequestAndId> requests_and_ids);
154
Nan Lin55ad2a5a2022-08-15 21:32:57155 void NotifyReportHandled(
Alex Turner4162376f2022-10-20 18:04:10156 const AggregatableReportRequest& request,
Dan McArdle08ad6112023-11-21 20:39:47157 std::optional<AggregationServiceStorage::RequestId> request_id,
158 const std::optional<AggregatableReport>& report,
Nan Lin55ad2a5a2022-08-15 21:32:57159 AggregationServiceObserver::ReportStatus status);
160
161 void NotifyRequestStorageModified();
162
Alex Turnerae969102022-11-16 18:22:42163 // The task runner for all aggregation service storage operations. Updateable
164 // to allow for priority to be temporarily increased to `USER_VISIBLE` when a
165 // clear data task is queued or running. Otherwise `BEST_EFFORT` is used.
166 scoped_refptr<base::UpdateableSequencedTaskRunner> storage_task_runner_;
167
Thomas Quintanilladce5ba9b2023-06-16 18:49:44168 // How many user visible storage tasks are queued or running currently, i.e.
Alex Turnerae969102022-11-16 18:22:42169 // have been posted but the reply has not been run.
Thomas Quintanilladce5ba9b2023-06-16 18:49:44170 int num_pending_user_visible_tasks_ = 0;
Alex Turnerae969102022-11-16 18:22:42171
Alex Turner71512772022-06-29 19:20:27172 base::SequenceBound<AggregationServiceStorage> storage_;
Alex Turnerb859b9c2022-08-03 21:47:54173 std::unique_ptr<AggregatableReportScheduler> scheduler_;
Nan Lin89244d62021-11-12 21:38:17174 std::unique_ptr<AggregatableReportAssembler> assembler_;
Nan Lin16e55f482021-12-10 15:05:47175 std::unique_ptr<AggregatableReportSender> sender_;
Nan Lin6a520782022-08-15 19:45:13176
Nan Lin55ad2a5a2022-08-15 21:32:57177 base::ObserverList<AggregationServiceObserver> observers_;
178
Nan Lin6a520782022-08-15 19:45:13179 base::WeakPtrFactory<AggregationServiceImpl> weak_factory_{this};
Nan Lin89244d62021-11-12 21:38:17180};
181
182} // namespace content
183
Sean Mahere672a662023-01-09 21:42:28184#endif // CONTENT_BROWSER_AGGREGATION_SERVICE_AGGREGATION_SERVICE_IMPL_H_