xref: /aosp_15_r20/external/cronet/net/reporting/reporting_browsing_data_remover.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/reporting/reporting_browsing_data_remover.h"
6 
7 #include <vector>
8 
9 #include "base/memory/raw_ptr.h"
10 #include "net/reporting/reporting_cache.h"
11 #include "net/reporting/reporting_context.h"
12 #include "net/reporting/reporting_report.h"
13 
14 namespace net {
15 
16 // static
RemoveBrowsingData(ReportingCache * cache,uint64_t data_type_mask,const base::RepeatingCallback<bool (const url::Origin &)> & origin_filter)17 void ReportingBrowsingDataRemover::RemoveBrowsingData(
18     ReportingCache* cache,
19     uint64_t data_type_mask,
20     const base::RepeatingCallback<bool(const url::Origin&)>& origin_filter) {
21   if ((data_type_mask & DATA_TYPE_REPORTS) != 0) {
22     std::vector<raw_ptr<const ReportingReport, VectorExperimental>> all_reports;
23     cache->GetReports(&all_reports);
24 
25     std::vector<raw_ptr<const ReportingReport, VectorExperimental>>
26         reports_to_remove;
27     for (const ReportingReport* report : all_reports) {
28       if (origin_filter.Run(url::Origin::Create(report->url)))
29         reports_to_remove.push_back(report);
30     }
31 
32     cache->RemoveReports(reports_to_remove);
33   }
34 
35   if ((data_type_mask & DATA_TYPE_CLIENTS) != 0) {
36     for (const url::Origin& origin : cache->GetAllOrigins()) {
37       if (origin_filter.Run(origin))
38         cache->RemoveClientsForOrigin(origin);
39     }
40   }
41   cache->Flush();
42 }
43 
44 // static
RemoveAllBrowsingData(ReportingCache * cache,uint64_t data_type_mask)45 void ReportingBrowsingDataRemover::RemoveAllBrowsingData(
46     ReportingCache* cache,
47     uint64_t data_type_mask) {
48   if ((data_type_mask & DATA_TYPE_REPORTS) != 0) {
49     cache->RemoveAllReports();
50   }
51   if ((data_type_mask & DATA_TYPE_CLIENTS) != 0) {
52     cache->RemoveAllClients();
53   }
54   cache->Flush();
55 }
56 
57 }  // namespace net
58