1// Copyright (c) 2011 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 NET_DISK_CACHE_EVICTION_H_
6#define NET_DISK_CACHE_EVICTION_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/task.h"
11#include "net/disk_cache/disk_format.h"
12#include "net/disk_cache/rankings.h"
13
14namespace disk_cache {
15
16class BackendImpl;
17class EntryImpl;
18
19// This class implements the eviction algorithm for the cache and it is tightly
20// integrated with BackendImpl.
21class Eviction {
22 public:
23  Eviction();
24  ~Eviction();
25
26  void Init(BackendImpl* backend);
27  void Stop();
28
29  // Deletes entries from the cache until the current size is below the limit.
30  // If empty is true, the whole cache will be trimmed, regardless of being in
31  // use.
32  void TrimCache(bool empty);
33
34  // Updates the ranking information for an entry.
35  void UpdateRank(EntryImpl* entry, bool modified);
36
37  // Notifications of interesting events for a given entry.
38  void OnOpenEntry(EntryImpl* entry);
39  void OnCreateEntry(EntryImpl* entry);
40  void OnDoomEntry(EntryImpl* entry);
41  void OnDestroyEntry(EntryImpl* entry);
42
43  // Testing interface.
44  void SetTestMode();
45  void TrimDeletedList(bool empty);
46
47 private:
48  void PostDelayedTrim();
49  void DelayedTrim();
50  bool ShouldTrim();
51  void ReportTrimTimes(EntryImpl* entry);
52  Rankings::List GetListForEntry(EntryImpl* entry);
53  bool EvictEntry(CacheRankingsBlock* node, bool empty, Rankings::List list);
54
55  // We'll just keep for a while a separate set of methods that implement the
56  // new eviction algorithm. This code will replace the original methods when
57  // finished.
58  void TrimCacheV2(bool empty);
59  void UpdateRankV2(EntryImpl* entry, bool modified);
60  void OnOpenEntryV2(EntryImpl* entry);
61  void OnCreateEntryV2(EntryImpl* entry);
62  void OnDoomEntryV2(EntryImpl* entry);
63  void OnDestroyEntryV2(EntryImpl* entry);
64  Rankings::List GetListForEntryV2(EntryImpl* entry);
65  void TrimDeleted(bool empty);
66  bool RemoveDeletedNode(CacheRankingsBlock* node);
67
68  bool NodeIsOldEnough(CacheRankingsBlock* node, int list);
69  int SelectListByLength(Rankings::ScopedRankingsBlock* next);
70  void ReportListStats();
71
72  BackendImpl* backend_;
73  Rankings* rankings_;
74  IndexHeader* header_;
75  int max_size_;
76  int trim_delays_;
77  bool new_eviction_;
78  bool first_trim_;
79  bool trimming_;
80  bool delay_trim_;
81  bool init_;
82  bool test_mode_;
83  bool in_experiment_;
84  ScopedRunnableMethodFactory<Eviction> factory_;
85
86  DISALLOW_COPY_AND_ASSIGN(Eviction);
87};
88
89}  // namespace disk_cache
90
91#endif  // NET_DISK_CACHE_EVICTION_H_
92