OLD | NEW |
(Empty) | |
| 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 |
| 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/macros.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 |
| 19 class PrefService; |
| 20 |
| 21 namespace cronet { |
| 22 // Handles the interaction between HostCache and prefs for persistence. |
| 23 // When notified of a change in the HostCache, starts a timer, or ignores if the |
| 24 // timer is already running. When that timer expires, writes the current state |
| 25 // of the HostCache to prefs. |
| 26 // |
| 27 // Can be used with synchronous or asynchronous prefs loading. Not appropriate |
| 28 // for use outside of Cronet because its network and prefs operations run on |
| 29 // the same sequence. Must be created after and destroyed before the HostCache |
| 30 // and PrefService. |
| 31 class HostCachePersistenceManager : public net::HostCache::PersistenceDelegate { |
| 32 public: |
| 33 // |cache| is the HostCache whose contents will be persisted. It must be |
| 34 // non-null and must outlive the HostCachePersistenceManager. |
| 35 // |pref_service| is the PrefService that will be used to persist the cache |
| 36 // contents. It must outlive the HostCachePersistenceManager. |
| 37 // |pref_name| is the name of the pref to read and write. |
| 38 // |delay| is the maximum time between a change in the cache and writing that |
| 39 // change to prefs. |
| 40 HostCachePersistenceManager(net::HostCache* cache, |
| 41 PrefService* pref_service, |
| 42 std::string pref_name, |
| 43 base::TimeDelta delay); |
| 44 virtual ~HostCachePersistenceManager(); |
| 45 |
| 46 // net::HostCache::PersistenceDelegate implementation |
| 47 void ScheduleWrite() override; |
| 48 |
| 49 private: |
| 50 // Gets the serialized HostCache and writes it to prefs. |
| 51 void WriteToDisk(); |
| 52 // On initial prefs read, passes the serialized entries to the HostCache. |
| 53 void ReadFromDisk(); |
| 54 |
| 55 net::HostCache* const cache_; |
| 56 |
| 57 PrefChangeRegistrar registrar_; |
| 58 PrefService* const pref_service_; |
| 59 const std::string pref_name_; |
| 60 bool writing_pref_; |
| 61 |
| 62 const base::TimeDelta delay_; |
| 63 base::OneShotTimer timer_; |
| 64 |
| 65 SEQUENCE_CHECKER(sequence_checker_); |
| 66 base::WeakPtrFactory<HostCachePersistenceManager> weak_factory_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(HostCachePersistenceManager); |
| 69 }; |
| 70 |
| 71 } // namespace cronet |
| 72 |
| 73 #endif // COMPONENTS_CRONET_HOST_CACHE_PERSISTENCE_MANAGER_H_ |
OLD | NEW |