blob: 684f2863012c0c1630b4b6d6302531e0122f8cb1 [file] [log] [blame]
Avi Drissman4e1b7bc32022-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 Linb771003b362022-01-19 21:05:5378 void ClearData(base::Time delete_begin,
79 base::Time delete_end,
Alex Turnerb835b3a2022-07-21 21:42:1880 StoragePartition::StorageKeyMatcherFunction filter,
Nan Linb771003b362022-01-19 21:05:5381 base::OnceClosure done) override;
Alex Turner48e25302022-07-21 20:25:3582 void ScheduleReport(AggregatableReportRequest report_request) override;
Alex Turner4162376f2022-10-20 18:04:1083 void AssembleAndSendReport(AggregatableReportRequest report_request) override;
Nan Lin6a520782022-08-15 19:45:1384 void GetPendingReportRequestsForWebUI(
85 base::OnceCallback<
86 void(std::vector<AggregationServiceStorage::RequestAndId>)> callback)
87 override;
88 void SendReportsForWebUI(
89 const std::vector<AggregationServiceStorage::RequestId>& ids,
90 base::OnceClosure reports_sent_callback) override;
Thomas Quintanilladce5ba9b2023-06-16 18:49:4491 void GetPendingReportReportingOrigins(
92 base::OnceCallback<void(std::set<url::Origin>)> callback) override;
Nan Lin55ad2a5a2022-08-15 21:32:5793 void AddObserver(AggregationServiceObserver* observer) override;
94 void RemoveObserver(AggregationServiceObserver* observer) override;
Nan Lin89244d62021-11-12 21:38:1795
96 // AggregationServiceStorageContext:
Alex Turner71512772022-06-29 19:20:2797 const base::SequenceBound<AggregationServiceStorage>& GetStorage() override;
Nan Lin89244d62021-11-12 21:38:1798
Nan Lin8de22bc2022-04-02 02:51:1499 // Sets the public keys for `url` in storage to allow testing without network.
100 void SetPublicKeysForTesting(const GURL& url, const PublicKeyset& keyset);
101
Nan Lin89244d62021-11-12 21:38:17102 private:
Alex Turner48e25302022-07-21 20:25:35103 // Allows access to `OnScheduledReportTimeReached()`.
104 friend class AggregationServiceImplTest;
105
Nan Lin16e55f482021-12-10 15:05:47106 AggregationServiceImpl(bool run_in_memory,
107 const base::FilePath& user_data_directory,
108 const base::Clock* clock,
Alex Turner48e25302022-07-21 20:25:35109 std::unique_ptr<AggregatableReportScheduler> scheduler,
Nan Lin16e55f482021-12-10 15:05:47110 std::unique_ptr<AggregatableReportAssembler> assembler,
111 std::unique_ptr<AggregatableReportSender> sender);
Nan Lin89244d62021-11-12 21:38:17112
Alex Turner48e25302022-07-21 20:25:35113 void OnScheduledReportTimeReached(
114 std::vector<AggregationServiceStorage::RequestAndId> requests_and_ids);
Alex Turnera022ec62022-07-20 21:10:27115
Alex Turner4162376f2022-10-20 18:04:10116 void AssembleAndSendReports(
117 std::vector<AggregationServiceStorage::RequestAndId> requests_and_ids,
118 base::RepeatingClosure done);
119
Dan McArdle08ad6112023-11-21 20:39:47120 // `request_id` is `std::nullopt` iff `report_request` was not
Alex Turner4162376f2022-10-20 18:04:10121 // stored/scheduled.
122 void AssembleAndSendReportImpl(
123 AggregatableReportRequest report_request,
Dan McArdle08ad6112023-11-21 20:39:47124 std::optional<AggregationServiceStorage::RequestId> request_id,
Alex Turner4162376f2022-10-20 18:04:10125 base::OnceClosure done);
Alex Turner48e25302022-07-21 20:25:35126 void OnReportAssemblyComplete(
Nan Lin6a520782022-08-15 19:45:13127 base::OnceClosure done,
Dan McArdle08ad6112023-11-21 20:39:47128 std::optional<AggregationServiceStorage::RequestId> request_id,
Alex Turner48e25302022-07-21 20:25:35129 GURL reporting_url,
Dan McArdle84d73742023-12-05 21:23:21130 base::ElapsedTimer elapsed_timer,
Nan Lin55ad2a5a2022-08-15 21:32:57131 AggregatableReportRequest report_request,
Dan McArdle08ad6112023-11-21 20:39:47132 std::optional<AggregatableReport> report,
Alex Turner48e25302022-07-21 20:25:35133 AggregatableReportAssembler::AssemblyStatus status);
Nan Lin55ad2a5a2022-08-15 21:32:57134 void OnReportSendingComplete(
135 base::OnceClosure done,
Alex Turner4162376f2022-10-20 18:04:10136 AggregatableReportRequest report_request,
Dan McArdle08ad6112023-11-21 20:39:47137 std::optional<AggregationServiceStorage::RequestId> request_id,
Nan Lin55ad2a5a2022-08-15 21:32:57138 AggregatableReport report,
Dan McArdle84d73742023-12-05 21:23:21139 base::ElapsedTimer elapsed_timer,
Nan Lin55ad2a5a2022-08-15 21:32:57140 AggregatableReportSender::RequestStatus status);
Thomas Quintanilladce5ba9b2023-06-16 18:49:44141 void OnUserVisibleTaskStarted();
142 void OnUserVisibleTaskComplete();
Alex Turnerae969102022-11-16 18:22:42143 void OnClearDataComplete();
Alex Turner48e25302022-07-21 20:25:35144
Nan Lin6a520782022-08-15 19:45:13145 void OnGetRequestsToSendFromWebUI(
146 base::OnceClosure reports_sent_callback,
147 std::vector<AggregationServiceStorage::RequestAndId> requests_and_ids);
148
Nan Lin55ad2a5a2022-08-15 21:32:57149 void NotifyReportHandled(
Alex Turner4162376f2022-10-20 18:04:10150 const AggregatableReportRequest& request,
Dan McArdle08ad6112023-11-21 20:39:47151 std::optional<AggregationServiceStorage::RequestId> request_id,
152 const std::optional<AggregatableReport>& report,
Nan Lin55ad2a5a2022-08-15 21:32:57153 AggregationServiceObserver::ReportStatus status);
154
155 void NotifyRequestStorageModified();
156
Alex Turnerae969102022-11-16 18:22:42157 // The task runner for all aggregation service storage operations. Updateable
158 // to allow for priority to be temporarily increased to `USER_VISIBLE` when a
159 // clear data task is queued or running. Otherwise `BEST_EFFORT` is used.
160 scoped_refptr<base::UpdateableSequencedTaskRunner> storage_task_runner_;
161
Thomas Quintanilladce5ba9b2023-06-16 18:49:44162 // How many user visible storage tasks are queued or running currently, i.e.
Alex Turnerae969102022-11-16 18:22:42163 // have been posted but the reply has not been run.
Thomas Quintanilladce5ba9b2023-06-16 18:49:44164 int num_pending_user_visible_tasks_ = 0;
Alex Turnerae969102022-11-16 18:22:42165
Alex Turner71512772022-06-29 19:20:27166 base::SequenceBound<AggregationServiceStorage> storage_;
Alex Turnerb859b9c2022-08-03 21:47:54167 std::unique_ptr<AggregatableReportScheduler> scheduler_;
Nan Lin89244d62021-11-12 21:38:17168 std::unique_ptr<AggregatableReportAssembler> assembler_;
Nan Lin16e55f482021-12-10 15:05:47169 std::unique_ptr<AggregatableReportSender> sender_;
Nan Lin6a520782022-08-15 19:45:13170
Nan Lin55ad2a5a2022-08-15 21:32:57171 base::ObserverList<AggregationServiceObserver> observers_;
172
Nan Lin6a520782022-08-15 19:45:13173 base::WeakPtrFactory<AggregationServiceImpl> weak_factory_{this};
Nan Lin89244d62021-11-12 21:38:17174};
175
176} // namespace content
177
Sean Mahere672a662023-01-09 21:42:28178#endif // CONTENT_BROWSER_AGGREGATION_SERVICE_AGGREGATION_SERVICE_IMPL_H_