1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef NET_DISK_CACHE_BLOCKFILE_EVICTION_H_ 6*6777b538SAndroid Build Coastguard Worker #define NET_DISK_CACHE_BLOCKFILE_EVICTION_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h" 9*6777b538SAndroid Build Coastguard Worker #include "base/memory/weak_ptr.h" 10*6777b538SAndroid Build Coastguard Worker #include "net/disk_cache/blockfile/rankings.h" 11*6777b538SAndroid Build Coastguard Worker 12*6777b538SAndroid Build Coastguard Worker namespace disk_cache { 13*6777b538SAndroid Build Coastguard Worker 14*6777b538SAndroid Build Coastguard Worker class BackendImpl; 15*6777b538SAndroid Build Coastguard Worker class EntryImpl; 16*6777b538SAndroid Build Coastguard Worker struct IndexHeader; 17*6777b538SAndroid Build Coastguard Worker 18*6777b538SAndroid Build Coastguard Worker // This class implements the eviction algorithm for the cache and it is tightly 19*6777b538SAndroid Build Coastguard Worker // integrated with BackendImpl. 20*6777b538SAndroid Build Coastguard Worker class Eviction { 21*6777b538SAndroid Build Coastguard Worker public: 22*6777b538SAndroid Build Coastguard Worker Eviction(); 23*6777b538SAndroid Build Coastguard Worker 24*6777b538SAndroid Build Coastguard Worker Eviction(const Eviction&) = delete; 25*6777b538SAndroid Build Coastguard Worker Eviction& operator=(const Eviction&) = delete; 26*6777b538SAndroid Build Coastguard Worker 27*6777b538SAndroid Build Coastguard Worker ~Eviction(); 28*6777b538SAndroid Build Coastguard Worker 29*6777b538SAndroid Build Coastguard Worker void Init(BackendImpl* backend); 30*6777b538SAndroid Build Coastguard Worker void Stop(); 31*6777b538SAndroid Build Coastguard Worker 32*6777b538SAndroid Build Coastguard Worker // Deletes entries from the cache until the current size is below the limit. 33*6777b538SAndroid Build Coastguard Worker // If empty is true, the whole cache will be trimmed, regardless of being in 34*6777b538SAndroid Build Coastguard Worker // use. 35*6777b538SAndroid Build Coastguard Worker void TrimCache(bool empty); 36*6777b538SAndroid Build Coastguard Worker 37*6777b538SAndroid Build Coastguard Worker // Updates the ranking information for an entry. 38*6777b538SAndroid Build Coastguard Worker void UpdateRank(EntryImpl* entry, bool modified); 39*6777b538SAndroid Build Coastguard Worker 40*6777b538SAndroid Build Coastguard Worker // Notifications of interesting events for a given entry. 41*6777b538SAndroid Build Coastguard Worker void OnOpenEntry(EntryImpl* entry); 42*6777b538SAndroid Build Coastguard Worker void OnCreateEntry(EntryImpl* entry); 43*6777b538SAndroid Build Coastguard Worker void OnDoomEntry(EntryImpl* entry); 44*6777b538SAndroid Build Coastguard Worker void OnDestroyEntry(EntryImpl* entry); 45*6777b538SAndroid Build Coastguard Worker 46*6777b538SAndroid Build Coastguard Worker // Testing interface. 47*6777b538SAndroid Build Coastguard Worker void SetTestMode(); 48*6777b538SAndroid Build Coastguard Worker void TrimDeletedList(bool empty); 49*6777b538SAndroid Build Coastguard Worker 50*6777b538SAndroid Build Coastguard Worker private: 51*6777b538SAndroid Build Coastguard Worker void PostDelayedTrim(); 52*6777b538SAndroid Build Coastguard Worker void DelayedTrim(); 53*6777b538SAndroid Build Coastguard Worker bool ShouldTrim(); 54*6777b538SAndroid Build Coastguard Worker bool ShouldTrimDeleted(); 55*6777b538SAndroid Build Coastguard Worker void ReportTrimTimes(EntryImpl* entry); 56*6777b538SAndroid Build Coastguard Worker Rankings::List GetListForEntry(EntryImpl* entry); 57*6777b538SAndroid Build Coastguard Worker bool EvictEntry(CacheRankingsBlock* node, bool empty, Rankings::List list); 58*6777b538SAndroid Build Coastguard Worker 59*6777b538SAndroid Build Coastguard Worker // We'll just keep for a while a separate set of methods that implement the 60*6777b538SAndroid Build Coastguard Worker // new eviction algorithm. This code will replace the original methods when 61*6777b538SAndroid Build Coastguard Worker // finished. 62*6777b538SAndroid Build Coastguard Worker void TrimCacheV2(bool empty); 63*6777b538SAndroid Build Coastguard Worker void UpdateRankV2(EntryImpl* entry, bool modified); 64*6777b538SAndroid Build Coastguard Worker void OnOpenEntryV2(EntryImpl* entry); 65*6777b538SAndroid Build Coastguard Worker void OnCreateEntryV2(EntryImpl* entry); 66*6777b538SAndroid Build Coastguard Worker void OnDoomEntryV2(EntryImpl* entry); 67*6777b538SAndroid Build Coastguard Worker void OnDestroyEntryV2(EntryImpl* entry); 68*6777b538SAndroid Build Coastguard Worker Rankings::List GetListForEntryV2(EntryImpl* entry); 69*6777b538SAndroid Build Coastguard Worker void TrimDeleted(bool empty); 70*6777b538SAndroid Build Coastguard Worker bool RemoveDeletedNode(CacheRankingsBlock* node); 71*6777b538SAndroid Build Coastguard Worker 72*6777b538SAndroid Build Coastguard Worker bool NodeIsOldEnough(CacheRankingsBlock* node, int list); 73*6777b538SAndroid Build Coastguard Worker int SelectListByLength(Rankings::ScopedRankingsBlock* next); 74*6777b538SAndroid Build Coastguard Worker 75*6777b538SAndroid Build Coastguard Worker raw_ptr<BackendImpl> backend_ = nullptr; 76*6777b538SAndroid Build Coastguard Worker raw_ptr<Rankings> rankings_; 77*6777b538SAndroid Build Coastguard Worker 78*6777b538SAndroid Build Coastguard Worker // May point to a mapped file's unmapped memory at destruction time. 79*6777b538SAndroid Build Coastguard Worker raw_ptr<IndexHeader, DisableDanglingPtrDetection> header_; 80*6777b538SAndroid Build Coastguard Worker 81*6777b538SAndroid Build Coastguard Worker int max_size_; 82*6777b538SAndroid Build Coastguard Worker int trim_delays_; 83*6777b538SAndroid Build Coastguard Worker int index_size_; 84*6777b538SAndroid Build Coastguard Worker bool new_eviction_; 85*6777b538SAndroid Build Coastguard Worker bool first_trim_; 86*6777b538SAndroid Build Coastguard Worker bool trimming_; 87*6777b538SAndroid Build Coastguard Worker bool delay_trim_; 88*6777b538SAndroid Build Coastguard Worker bool init_ = false; 89*6777b538SAndroid Build Coastguard Worker bool test_mode_; 90*6777b538SAndroid Build Coastguard Worker base::WeakPtrFactory<Eviction> ptr_factory_{this}; 91*6777b538SAndroid Build Coastguard Worker }; 92*6777b538SAndroid Build Coastguard Worker 93*6777b538SAndroid Build Coastguard Worker } // namespace disk_cache 94*6777b538SAndroid Build Coastguard Worker 95*6777b538SAndroid Build Coastguard Worker #endif // NET_DISK_CACHE_BLOCKFILE_EVICTION_H_ 96