1// Copyright (c) 2012 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_BLOCKFILE_EVICTION_V3_H_
6#define NET_DISK_CACHE_BLOCKFILE_EVICTION_V3_H_
7
8#include "base/basictypes.h"
9#include "base/memory/weak_ptr.h"
10#include "net/disk_cache/blockfile/disk_format_v3.h"
11#include "net/disk_cache/blockfile/index_table_v3.h"
12
13namespace disk_cache {
14
15class BackendImplV3;
16class CacheRankingsBlock;
17class EntryImplV3;
18
19namespace Rankings {
20typedef int List;
21}
22
23// This class implements the eviction algorithm for the cache and it is tightly
24// integrated with BackendImpl.
25class EvictionV3 {
26 public:
27  EvictionV3();
28  ~EvictionV3();
29
30  void Init(BackendImplV3* backend);
31  void Stop();
32
33  // Deletes entries from the cache until the current size is below the limit.
34  // If empty is true, the whole cache will be trimmed, regardless of being in
35  // use.
36  void TrimCache(bool empty);
37
38  // Notifications of interesting events for a given entry.
39  void OnOpenEntry(EntryImplV3* entry);
40  void OnCreateEntry(EntryImplV3* entry);
41
42  // Testing interface.
43  void SetTestMode();
44  void TrimDeletedList(bool empty);
45
46 private:
47  void PostDelayedTrim();
48  void DelayedTrim();
49  bool ShouldTrim();
50  bool ShouldTrimDeleted();
51  bool EvictEntry(CacheRankingsBlock* node, bool empty, Rankings::List list);
52
53  void TrimCacheV2(bool empty);
54  void TrimDeleted(bool empty);
55
56  bool NodeIsOldEnough(CacheRankingsBlock* node, int list);
57  int SelectListByLength();
58  void ReportListStats();
59
60  BackendImplV3* backend_;
61  IndexTable* index_;
62  IndexHeaderV3* header_;
63  int max_size_;
64  int trim_delays_;
65  bool lru_;
66  bool first_trim_;
67  bool trimming_;
68  bool delay_trim_;
69  bool init_;
70  bool test_mode_;
71  base::WeakPtrFactory<EvictionV3> ptr_factory_;
72
73  DISALLOW_COPY_AND_ASSIGN(EvictionV3);
74};
75
76}  // namespace disk_cache
77
78#endif  // NET_DISK_CACHE_BLOCKFILE_EVICTION_V3_H_
79