Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / disk_cache / simple / simple_backend_impl.h
blob2bd00818b9cfb1f550f432f9f53521b41b66f049
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_
8 #include <string>
9 #include <utility>
10 #include <vector>
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"
26 namespace base {
27 class SingleThreadTaskRunner;
28 class TaskRunner;
31 namespace disk_cache {
33 // SimpleBackendImpl is a new cache backend that stores entries in individual
34 // files.
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
38 // otherwise stated.
40 class SimpleEntryImpl;
41 class SimpleIndex;
43 class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend,
44 public SimpleIndexDelegate,
45 public base::SupportsWeakPtr<SimpleBackendImpl> {
46 public:
47 SimpleBackendImpl(
48 const base::FilePath& path,
49 int max_bytes,
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
78 // doom completed.
79 void OnDoomComplete(uint64 entry_hash);
81 // SimpleIndexDelegate:
82 virtual void DoomEntries(std::vector<uint64>* entry_hashes,
83 const CompletionCallback& callback) OVERRIDE;
85 // Backend:
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,
96 base::Time end_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;
107 private:
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;
119 uint64 max_size;
120 bool detected_magic_number_mismatch;
121 int net_error;
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,
130 base::Time end_time,
131 const CompletionCallback& callback,
132 int result);
134 // Try to create the directory if it doesn't exist. This must run on the IO
135 // thread.
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(
142 uint64 entry_hash,
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
148 // the disk.
149 int OpenEntryFromHash(uint64 entry_hash,
150 Entry** entry,
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,
162 Entry** next_entry,
163 const CompletionCallback& callback,
164 int error_code);
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,
171 Entry** entry,
172 scoped_refptr<SimpleEntryImpl> simple_entry,
173 const CompletionCallback& callback,
174 int error_code);
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,
179 Entry** entry,
180 scoped_refptr<SimpleEntryImpl> simple_entry,
181 const CompletionCallback& callback,
182 int error_code);
184 // Called at the end of the asynchronous operation triggered by
185 // OpenEntryFromHash. Makes sure to continue iterating if the open entry was
186 // not a success.
187 void CheckIterationReturnValue(void** iter,
188 Entry** entry,
189 const CompletionCallback& callback,
190 int error_code);
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,
196 int result);
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_;
204 int orig_max_size_;
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_