xref: /aosp_15_r20/external/cronet/components/cronet/host_cache_persistence_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_HOST_CACHE_PERSISTENCE_MANAGER_H_
6 #define COMPONENTS_CRONET_HOST_CACHE_PERSISTENCE_MANAGER_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/sequence_checker.h"
14 #include "base/time/time.h"
15 #include "base/timer/timer.h"
16 #include "components/prefs/pref_change_registrar.h"
17 #include "net/dns/host_cache.h"
18 #include "net/log/net_log_with_source.h"
19 
20 class PrefService;
21 
22 namespace net {
23 class NetLog;
24 }
25 
26 namespace cronet {
27 // Handles the interaction between HostCache and prefs for persistence.
28 // When notified of a change in the HostCache, starts a timer, or ignores if the
29 // timer is already running. When that timer expires, writes the current state
30 // of the HostCache to prefs.
31 //
32 // Can be used with synchronous or asynchronous prefs loading. Not appropriate
33 // for use outside of Cronet because its network and prefs operations run on
34 // the same sequence. Must be created after and destroyed before the HostCache
35 // and PrefService.
36 class HostCachePersistenceManager : public net::HostCache::PersistenceDelegate {
37  public:
38   // |cache| is the HostCache whose contents will be persisted. It must be
39   // non-null and must outlive the HostCachePersistenceManager.
40   // |pref_service| is the PrefService that will be used to persist the cache
41   // contents. It must outlive the HostCachePersistenceManager.
42   // |pref_name| is the name of the pref to read and write.
43   // |delay| is the maximum time between a change in the cache and writing that
44   // change to prefs.
45   HostCachePersistenceManager(net::HostCache* cache,
46                               PrefService* pref_service,
47                               std::string pref_name,
48                               base::TimeDelta delay,
49                               net::NetLog* net_log);
50 
51   HostCachePersistenceManager(const HostCachePersistenceManager&) = delete;
52   HostCachePersistenceManager& operator=(const HostCachePersistenceManager&) =
53       delete;
54 
55   virtual ~HostCachePersistenceManager();
56 
57   // net::HostCache::PersistenceDelegate implementation
58   void ScheduleWrite() override;
59 
60  private:
61   // Gets the serialized HostCache and writes it to prefs.
62   void WriteToDisk();
63   // On initial prefs read, passes the serialized entries to the HostCache.
64   void ReadFromDisk();
65 
66   const raw_ptr<net::HostCache> cache_;
67 
68   PrefChangeRegistrar registrar_;
69   const raw_ptr<PrefService> pref_service_;
70   const std::string pref_name_;
71   bool writing_pref_;
72 
73   const base::TimeDelta delay_;
74   base::OneShotTimer timer_;
75 
76   const net::NetLogWithSource net_log_;
77 
78   SEQUENCE_CHECKER(sequence_checker_);
79   base::WeakPtrFactory<HostCachePersistenceManager> weak_factory_{this};
80 };
81 
82 }  // namespace cronet
83 
84 #endif  // COMPONENTS_CRONET_HOST_CACHE_PERSISTENCE_MANAGER_H_
85