host_cache_persistence_manager.h 2.9 KB

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