Update V8 to version 4.7.53.
[chromium-blink-merge.git] / net / disk_cache / simple / simple_backend_impl.h
blob7993fdcd85343c02c3eea9baf2e63d50009744a6
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/strings/string_split.h"
20 #include "base/task_runner.h"
21 #include "base/time/time.h"
22 #include "net/base/cache_type.h"
23 #include "net/disk_cache/disk_cache.h"
24 #include "net/disk_cache/simple/simple_entry_impl.h"
25 #include "net/disk_cache/simple/simple_index_delegate.h"
27 namespace base {
28 class SingleThreadTaskRunner;
29 class TaskRunner;
32 namespace disk_cache {
34 // SimpleBackendImpl is a new cache backend that stores entries in individual
35 // files.
36 // See http://www.chromium.org/developers/design-documents/network-stack/disk-cache/very-simple-backend
38 // The SimpleBackendImpl provides safe iteration; mutating entries during
39 // iteration cannot cause a crash. It is undefined whether entries created or
40 // destroyed during the iteration will be included in any pre-existing
41 // iterations.
43 // The non-static functions below must be called on the IO thread unless
44 // otherwise stated.
46 class SimpleEntryImpl;
47 class SimpleIndex;
49 class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend,
50 public SimpleIndexDelegate,
51 public base::SupportsWeakPtr<SimpleBackendImpl> {
52 public:
53 SimpleBackendImpl(
54 const base::FilePath& path,
55 int max_bytes,
56 net::CacheType cache_type,
57 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread,
58 net::NetLog* net_log);
60 ~SimpleBackendImpl() override;
62 net::CacheType cache_type() const { return cache_type_; }
63 SimpleIndex* index() { return index_.get(); }
65 base::TaskRunner* worker_pool() { return worker_pool_.get(); }
67 int Init(const CompletionCallback& completion_callback);
69 // Sets the maximum size for the total amount of data stored by this instance.
70 bool SetMaxSize(int max_bytes);
72 // Returns the maximum file size permitted in this backend.
73 int GetMaxFileSize() const;
75 // Flush our SequencedWorkerPool.
76 static void FlushWorkerPoolForTesting();
78 // The entry for |entry_hash| is being doomed; the backend will not attempt
79 // run new operations for this |entry_hash| until the Doom is completed.
80 void OnDoomStart(uint64 entry_hash);
82 // The entry for |entry_hash| has been successfully doomed, we can now allow
83 // operations on this entry, and we can run any operations enqueued while the
84 // doom completed.
85 void OnDoomComplete(uint64 entry_hash);
87 // SimpleIndexDelegate:
88 void DoomEntries(std::vector<uint64>* entry_hashes,
89 const CompletionCallback& callback) override;
91 // Backend:
92 net::CacheType GetCacheType() const override;
93 int32 GetEntryCount() const override;
94 int OpenEntry(const std::string& key,
95 Entry** entry,
96 const CompletionCallback& callback) override;
97 int CreateEntry(const std::string& key,
98 Entry** entry,
99 const CompletionCallback& callback) override;
100 int DoomEntry(const std::string& key,
101 const CompletionCallback& callback) override;
102 int DoomAllEntries(const CompletionCallback& callback) override;
103 int DoomEntriesBetween(base::Time initial_time,
104 base::Time end_time,
105 const CompletionCallback& callback) override;
106 int DoomEntriesSince(base::Time initial_time,
107 const CompletionCallback& callback) override;
108 scoped_ptr<Iterator> CreateIterator() override;
109 void GetStats(base::StringPairs* stats) override;
110 void OnExternalCacheHit(const std::string& key) override;
112 private:
113 class SimpleIterator;
114 friend class SimpleIterator;
116 typedef base::hash_map<uint64, SimpleEntryImpl*> EntryMap;
118 typedef base::Callback<void(base::Time mtime, uint64 max_size, int result)>
119 InitializeIndexCallback;
121 class ActiveEntryProxy;
122 friend class ActiveEntryProxy;
124 // Return value of InitCacheStructureOnDisk().
125 struct DiskStatResult {
126 base::Time cache_dir_mtime;
127 uint64 max_size;
128 bool detected_magic_number_mismatch;
129 int net_error;
132 void InitializeIndex(const CompletionCallback& callback,
133 const DiskStatResult& result);
135 // Dooms all entries previously accessed between |initial_time| and
136 // |end_time|. Invoked when the index is ready.
137 void IndexReadyForDoom(base::Time initial_time,
138 base::Time end_time,
139 const CompletionCallback& callback,
140 int result);
142 // Try to create the directory if it doesn't exist. This must run on the IO
143 // thread.
144 static DiskStatResult InitCacheStructureOnDisk(const base::FilePath& path,
145 uint64 suggested_max_size);
147 // Searches |active_entries_| for the entry corresponding to |key|. If found,
148 // returns the found entry. Otherwise, creates a new entry and returns that.
149 scoped_refptr<SimpleEntryImpl> CreateOrFindActiveEntry(
150 uint64 entry_hash,
151 const std::string& key);
153 // Given a hash, will try to open the corresponding Entry. If we have an Entry
154 // corresponding to |hash| in the map of active entries, opens it. Otherwise,
155 // a new empty Entry will be created, opened and filled with information from
156 // the disk.
157 int OpenEntryFromHash(uint64 entry_hash,
158 Entry** entry,
159 const CompletionCallback& callback);
161 // Doom the entry corresponding to |entry_hash|, if it's active or currently
162 // pending doom. This function does not block if there is an active entry,
163 // which is very important to prevent races in DoomEntries() above.
164 int DoomEntryFromHash(uint64 entry_hash, 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,
171 Entry** entry,
172 const 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 const scoped_refptr<SimpleEntryImpl>& simple_entry,
181 const CompletionCallback& callback,
182 int error_code);
184 // A callback thunk used by DoomEntries to clear the |entries_pending_doom_|
185 // after a mass doom.
186 void DoomEntriesComplete(scoped_ptr<std::vector<uint64> > entry_hashes,
187 const CompletionCallback& callback,
188 int result);
190 const base::FilePath path_;
191 const net::CacheType cache_type_;
192 scoped_ptr<SimpleIndex> index_;
193 const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_;
194 scoped_refptr<base::TaskRunner> worker_pool_;
196 int orig_max_size_;
197 const SimpleEntryImpl::OperationsMode entry_operations_mode_;
199 EntryMap active_entries_;
201 // The set of all entries which are currently being doomed. To avoid races,
202 // these entries cannot have Doom/Create/Open operations run until the doom
203 // is complete. The base::Closure map target is used to store deferred
204 // operations to be run at the completion of the Doom.
205 base::hash_map<uint64, std::vector<base::Closure> > entries_pending_doom_;
207 net::NetLog* const net_log_;
210 } // namespace disk_cache
212 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_