xref: /aosp_15_r20/external/cronet/net/extras/sqlite/sqlite_persistent_reporting_and_nel_store.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 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 #ifndef NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_REPORTING_AND_NEL_STORE_H_
6 #define NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_REPORTING_AND_NEL_STORE_H_
7 
8 #include <vector>
9 
10 #include "base/component_export.h"
11 #include "base/memory/scoped_refptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/task/task_traits.h"
14 #include "net/network_error_logging/network_error_logging_service.h"
15 #include "net/network_error_logging/persistent_reporting_and_nel_store.h"
16 #include "net/reporting/reporting_cache.h"
17 
18 namespace base {
19 class FilePath;
20 class SequencedTaskRunner;
21 }  // namespace base
22 
23 namespace net {
24 
25 // Returns recommended task priority for |background_task_runner|.
26 base::TaskPriority COMPONENT_EXPORT(NET_EXTRAS)
27     GetReportingAndNelStoreBackgroundSequencePriority();
28 
COMPONENT_EXPORT(NET_EXTRAS)29 class COMPONENT_EXPORT(NET_EXTRAS) SQLitePersistentReportingAndNelStore
30     : public PersistentReportingAndNelStore {
31  public:
32   SQLitePersistentReportingAndNelStore(
33       const base::FilePath& path,
34       const scoped_refptr<base::SequencedTaskRunner>& client_task_runner,
35       const scoped_refptr<base::SequencedTaskRunner>& background_task_runner);
36 
37   SQLitePersistentReportingAndNelStore(
38       const SQLitePersistentReportingAndNelStore&) = delete;
39   SQLitePersistentReportingAndNelStore& operator=(
40       const SQLitePersistentReportingAndNelStore&) = delete;
41 
42   ~SQLitePersistentReportingAndNelStore() override;
43 
44   // NetworkErrorLoggingService::PersistentNelStore implementation
45   void LoadNelPolicies(NelPoliciesLoadedCallback loaded_callback) override;
46   void AddNelPolicy(
47       const NetworkErrorLoggingService::NelPolicy& policy) override;
48   void UpdateNelPolicyAccessTime(
49       const NetworkErrorLoggingService::NelPolicy& policy) override;
50   void DeleteNelPolicy(
51       const NetworkErrorLoggingService::NelPolicy& policy) override;
52 
53   // ReportingCache::PersistentReportingStore implementation
54   void LoadReportingClients(
55       ReportingClientsLoadedCallback loaded_callback) override;
56   void AddReportingEndpoint(const ReportingEndpoint& endpoint) override;
57   void AddReportingEndpointGroup(
58       const CachedReportingEndpointGroup& group) override;
59   void UpdateReportingEndpointGroupAccessTime(
60       const CachedReportingEndpointGroup& group) override;
61   void UpdateReportingEndpointDetails(
62       const ReportingEndpoint& endpoint) override;
63   void UpdateReportingEndpointGroupDetails(
64       const CachedReportingEndpointGroup& group) override;
65   void DeleteReportingEndpoint(const ReportingEndpoint& endpoint) override;
66   void DeleteReportingEndpointGroup(
67       const CachedReportingEndpointGroup& group) override;
68 
69   void Flush() override;
70 
71   size_t GetQueueLengthForTesting() const;
72 
73  private:
74   class Backend;
75 
76   // Calls |callback| with the loaded |policies|.
77   void CompleteLoadNelPolicies(
78       NelPoliciesLoadedCallback callback,
79       std::vector<NetworkErrorLoggingService::NelPolicy> policies);
80 
81   // Calls |callback| with the loaded |endpoints| and |endpoint_groups|.
82   void CompleteLoadReportingClients(
83       ReportingClientsLoadedCallback callback,
84       std::vector<ReportingEndpoint> endpoints,
85       std::vector<CachedReportingEndpointGroup> endpoint_groups);
86 
87   const scoped_refptr<Backend> backend_;
88 
89   base::WeakPtrFactory<SQLitePersistentReportingAndNelStore> weak_factory_{
90       this};
91 };
92 
93 }  // namespace net
94 
95 #endif  // NET_EXTRAS_SQLITE_SQLITE_PERSISTENT_REPORTING_AND_NEL_STORE_H_
96