1 // Copyright (c) 2013 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.
5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_
12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/task_runner.h"
20 #include "base/time/time.h"
21 #include "net/base/cache_type.h"
22 #include "net/disk_cache/disk_cache.h"
23 #include "net/disk_cache/simple/simple_entry_impl.h"
24 #include "net/disk_cache/simple/simple_index_delegate.h"
27 class SingleThreadTaskRunner
;
31 namespace disk_cache
{
33 // SimpleBackendImpl is a new cache backend that stores entries in individual
35 // See http://www.chromium.org/developers/design-documents/network-stack/disk-cache/very-simple-backend
37 // The non-static functions below must be called on the IO thread unless
40 class SimpleEntryImpl
;
43 class NET_EXPORT_PRIVATE SimpleBackendImpl
: public Backend
,
44 public SimpleIndexDelegate
,
45 public base::SupportsWeakPtr
<SimpleBackendImpl
> {
48 const base::FilePath
& path
,
50 net::CacheType cache_type
,
51 const scoped_refptr
<base::SingleThreadTaskRunner
>& cache_thread
,
52 net::NetLog
* net_log
);
54 virtual ~SimpleBackendImpl();
56 net::CacheType
cache_type() const { return cache_type_
; }
57 SimpleIndex
* index() { return index_
.get(); }
59 base::TaskRunner
* worker_pool() { return worker_pool_
.get(); }
61 int Init(const CompletionCallback
& completion_callback
);
63 // Sets the maximum size for the total amount of data stored by this instance.
64 bool SetMaxSize(int max_bytes
);
66 // Returns the maximum file size permitted in this backend.
67 int GetMaxFileSize() const;
69 // Flush our SequencedWorkerPool.
70 static void FlushWorkerPoolForTesting();
72 // The entry for |entry_hash| is being doomed; the backend will not attempt
73 // run new operations for this |entry_hash| until the Doom is completed.
74 void OnDoomStart(uint64 entry_hash
);
76 // The entry for |entry_hash| has been successfully doomed, we can now allow
77 // operations on this entry, and we can run any operations enqueued while the
79 void OnDoomComplete(uint64 entry_hash
);
81 // SimpleIndexDelegate:
82 virtual void DoomEntries(std::vector
<uint64
>* entry_hashes
,
83 const CompletionCallback
& callback
) OVERRIDE
;
86 virtual net::CacheType
GetCacheType() const OVERRIDE
;
87 virtual int32
GetEntryCount() const OVERRIDE
;
88 virtual int OpenEntry(const std::string
& key
, Entry
** entry
,
89 const CompletionCallback
& callback
) OVERRIDE
;
90 virtual int CreateEntry(const std::string
& key
, Entry
** entry
,
91 const CompletionCallback
& callback
) OVERRIDE
;
92 virtual int DoomEntry(const std::string
& key
,
93 const CompletionCallback
& callback
) OVERRIDE
;
94 virtual int DoomAllEntries(const CompletionCallback
& callback
) OVERRIDE
;
95 virtual int DoomEntriesBetween(base::Time initial_time
,
97 const CompletionCallback
& callback
) OVERRIDE
;
98 virtual int DoomEntriesSince(base::Time initial_time
,
99 const CompletionCallback
& callback
) OVERRIDE
;
100 virtual int OpenNextEntry(void** iter
, Entry
** next_entry
,
101 const CompletionCallback
& callback
) OVERRIDE
;
102 virtual void EndEnumeration(void** iter
) OVERRIDE
;
103 virtual void GetStats(
104 std::vector
<std::pair
<std::string
, std::string
> >* stats
) OVERRIDE
;
105 virtual void OnExternalCacheHit(const std::string
& key
) OVERRIDE
;
108 typedef base::hash_map
<uint64
, SimpleEntryImpl
*> EntryMap
;
110 typedef base::Callback
<void(base::Time mtime
, uint64 max_size
, int result
)>
111 InitializeIndexCallback
;
113 class ActiveEntryProxy
;
114 friend class ActiveEntryProxy
;
116 // Return value of InitCacheStructureOnDisk().
117 struct DiskStatResult
{
118 base::Time cache_dir_mtime
;
120 bool detected_magic_number_mismatch
;
124 void InitializeIndex(const CompletionCallback
& callback
,
125 const DiskStatResult
& result
);
127 // Dooms all entries previously accessed between |initial_time| and
128 // |end_time|. Invoked when the index is ready.
129 void IndexReadyForDoom(base::Time initial_time
,
131 const CompletionCallback
& callback
,
134 // Try to create the directory if it doesn't exist. This must run on the IO
136 static DiskStatResult
InitCacheStructureOnDisk(const base::FilePath
& path
,
137 uint64 suggested_max_size
);
139 // Searches |active_entries_| for the entry corresponding to |key|. If found,
140 // returns the found entry. Otherwise, creates a new entry and returns that.
141 scoped_refptr
<SimpleEntryImpl
> CreateOrFindActiveEntry(
143 const std::string
& key
);
145 // Given a hash, will try to open the corresponding Entry. If we have an Entry
146 // corresponding to |hash| in the map of active entries, opens it. Otherwise,
147 // a new empty Entry will be created, opened and filled with information from
149 int OpenEntryFromHash(uint64 entry_hash
,
151 const CompletionCallback
& callback
);
153 // Doom the entry corresponding to |entry_hash|, if it's active or currently
154 // pending doom. This function does not block if there is an active entry,
155 // which is very important to prevent races in DoomEntries() above.
156 int DoomEntryFromHash(uint64 entry_hash
, const CompletionCallback
& callback
);
158 // Called when the index is initilized to find the next entry in the iterator
159 // |iter|. If there are no more hashes in the iterator list, net::ERR_FAILED
160 // is returned. Otherwise, calls OpenEntryFromHash.
161 void GetNextEntryInIterator(void** iter
,
163 const CompletionCallback
& callback
,
166 // Called when we tried to open an entry with hash alone. When a blank entry
167 // has been created and filled in with information from the disk - based on a
168 // hash alone - this checks that a duplicate active entry was not created
169 // using a key in the meantime.
170 void OnEntryOpenedFromHash(uint64 hash
,
172 scoped_refptr
<SimpleEntryImpl
> simple_entry
,
173 const CompletionCallback
& callback
,
176 // Called when we tried to open an entry from key. When the entry has been
177 // opened, a check for key mismatch is performed.
178 void OnEntryOpenedFromKey(const std::string key
,
180 scoped_refptr
<SimpleEntryImpl
> simple_entry
,
181 const CompletionCallback
& callback
,
184 // Called at the end of the asynchronous operation triggered by
185 // OpenEntryFromHash. Makes sure to continue iterating if the open entry was
187 void CheckIterationReturnValue(void** iter
,
189 const CompletionCallback
& callback
,
192 // A callback thunk used by DoomEntries to clear the |entries_pending_doom_|
193 // after a mass doom.
194 void DoomEntriesComplete(scoped_ptr
<std::vector
<uint64
> > entry_hashes
,
195 const CompletionCallback
& callback
,
198 const base::FilePath path_
;
199 const net::CacheType cache_type_
;
200 scoped_ptr
<SimpleIndex
> index_
;
201 const scoped_refptr
<base::SingleThreadTaskRunner
> cache_thread_
;
202 scoped_refptr
<base::TaskRunner
> worker_pool_
;
205 const SimpleEntryImpl::OperationsMode entry_operations_mode_
;
207 EntryMap active_entries_
;
209 // The set of all entries which are currently being doomed. To avoid races,
210 // these entries cannot have Doom/Create/Open operations run until the doom
211 // is complete. The base::Closure map target is used to store deferred
212 // operations to be run at the completion of the Doom.
213 base::hash_map
<uint64
, std::vector
<base::Closure
> > entries_pending_doom_
;
215 net::NetLog
* const net_log_
;
218 } // namespace disk_cache
220 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_