xref: /aosp_15_r20/external/cronet/components/cronet/cronet_prefs_manager.h (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 #ifndef COMPONENTS_CRONET_CRONET_PREFS_MANAGER_H_
6 #define COMPONENTS_CRONET_CRONET_PREFS_MANAGER_H_
7 
8 #include <string>
9 
10 #include "base/memory/ref_counted.h"
11 #include "base/threading/thread_checker.h"
12 
13 class JsonPrefStore;
14 class PrefService;
15 
16 namespace base {
17 class SingleThreadTaskRunner;
18 class SequencedTaskRunner;
19 }  // namespace base
20 
21 namespace net {
22 class HostCache;
23 class NetLog;
24 class NetworkQualitiesPrefsManager;
25 class NetworkQualityEstimator;
26 class URLRequestContextBuilder;
27 }  // namespace net
28 
29 namespace cronet {
30 class HostCachePersistenceManager;
31 
32 // Manages the PrefService, JsonPrefStore and all associated persistence
33 // managers used by Cronet such as NetworkQualityPrefsManager,
34 // HostCachePersistenceManager, etc. The constructor, destructor and all
35 // other methods of this class should be called on the network thread.
36 class CronetPrefsManager {
37  public:
38   CronetPrefsManager(
39       const std::string& storage_path,
40       scoped_refptr<base::SingleThreadTaskRunner> network_task_runner,
41       scoped_refptr<base::SequencedTaskRunner> file_task_runner,
42       bool enable_network_quality_estimator,
43       bool enable_host_cache_persistence,
44       net::NetLog* net_log,
45       net::URLRequestContextBuilder* context_builder);
46 
47   CronetPrefsManager(const CronetPrefsManager&) = delete;
48   CronetPrefsManager& operator=(const CronetPrefsManager&) = delete;
49 
50   virtual ~CronetPrefsManager();
51 
52   void SetupNqePersistence(net::NetworkQualityEstimator* nqe);
53 
54   void SetupHostCachePersistence(net::HostCache* host_cache,
55                                  int host_cache_persistence_delay_ms,
56                                  net::NetLog* net_log);
57 
58   // Prepares |this| for shutdown.
59   void PrepareForShutdown();
60 
61  private:
62   // |pref_service_| should outlive the HttpServerPropertiesManager owned by
63   // |host_cache_persistence_manager_|.
64   std::unique_ptr<PrefService> pref_service_;
65   scoped_refptr<JsonPrefStore> json_pref_store_;
66 
67   // Manages the writing and reading of the network quality prefs.
68   std::unique_ptr<net::NetworkQualitiesPrefsManager>
69       network_qualities_prefs_manager_;
70 
71   // Manages reading and writing the HostCache pref when persistence is enabled.
72   // Must be destroyed before |context_| owned by CronetUrlContextAdapter
73   // (because it owns the HostResolverImpl,
74   // which owns the HostCache) and |pref_service_|.
75   std::unique_ptr<HostCachePersistenceManager> host_cache_persistence_manager_;
76 
77   // Checks that all methods are called on the network thread.
78   THREAD_CHECKER(thread_checker_);
79 
80 };  // class CronetPrefsManager
81 
82 }  // namespace cronet
83 
84 #endif  // COMPONENTS_CRONET_CRONET_PREFS_MANAGER_H_
85