OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ | |
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ | |
7 | |
8 #include <string> | |
9 #include <utility> | |
10 #include <vector> | |
11 | |
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" | |
25 | |
26 namespace base { | |
27 class SingleThreadTaskRunner; | |
28 class TaskRunner; | |
29 } | |
30 | |
31 namespace disk_cache { | |
32 | |
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-ca
che/very-simple-backend | |
36 // | |
37 // The SimpleBackendImpl provides safe iteration; mutating entries during | |
38 // iteration cannot cause a crash. It is undefined whether entries created or | |
39 // destroyed during the iteration will be included in any pre-existing | |
40 // iterations. | |
41 // | |
42 // The non-static functions below must be called on the IO thread unless | |
43 // otherwise stated. | |
44 | |
45 class SimpleEntryImpl; | |
46 class SimpleIndex; | |
47 | |
48 class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend, | |
49 public SimpleIndexDelegate, | |
50 public base::SupportsWeakPtr<SimpleBackendImpl> { | |
51 public: | |
52 SimpleBackendImpl( | |
53 const base::FilePath& path, | |
54 int max_bytes, | |
55 net::CacheType cache_type, | |
56 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, | |
57 net::NetLog* net_log); | |
58 | |
59 ~SimpleBackendImpl() override; | |
60 | |
61 net::CacheType cache_type() const { return cache_type_; } | |
62 SimpleIndex* index() { return index_.get(); } | |
63 | |
64 base::TaskRunner* worker_pool() { return worker_pool_.get(); } | |
65 | |
66 int Init(const CompletionCallback& completion_callback); | |
67 | |
68 // Sets the maximum size for the total amount of data stored by this instance. | |
69 bool SetMaxSize(int max_bytes); | |
70 | |
71 // Returns the maximum file size permitted in this backend. | |
72 int GetMaxFileSize() const; | |
73 | |
74 // Flush our SequencedWorkerPool. | |
75 static void FlushWorkerPoolForTesting(); | |
76 | |
77 // The entry for |entry_hash| is being doomed; the backend will not attempt | |
78 // run new operations for this |entry_hash| until the Doom is completed. | |
79 void OnDoomStart(uint64 entry_hash); | |
80 | |
81 // The entry for |entry_hash| has been successfully doomed, we can now allow | |
82 // operations on this entry, and we can run any operations enqueued while the | |
83 // doom completed. | |
84 void OnDoomComplete(uint64 entry_hash); | |
85 | |
86 // SimpleIndexDelegate: | |
87 void DoomEntries(std::vector<uint64>* entry_hashes, | |
88 const CompletionCallback& callback) override; | |
89 | |
90 // Backend: | |
91 net::CacheType GetCacheType() const override; | |
92 int32 GetEntryCount() const override; | |
93 int OpenEntry(const std::string& key, | |
94 Entry** entry, | |
95 const CompletionCallback& callback) override; | |
96 int CreateEntry(const std::string& key, | |
97 Entry** entry, | |
98 const CompletionCallback& callback) override; | |
99 int DoomEntry(const std::string& key, | |
100 const CompletionCallback& callback) override; | |
101 int DoomAllEntries(const CompletionCallback& callback) override; | |
102 int DoomEntriesBetween(base::Time initial_time, | |
103 base::Time end_time, | |
104 const CompletionCallback& callback) override; | |
105 int DoomEntriesSince(base::Time initial_time, | |
106 const CompletionCallback& callback) override; | |
107 scoped_ptr<Iterator> CreateIterator() override; | |
108 void GetStats( | |
109 std::vector<std::pair<std::string, std::string>>* stats) override; | |
110 void OnExternalCacheHit(const std::string& key) override; | |
111 | |
112 private: | |
113 class SimpleIterator; | |
114 friend class SimpleIterator; | |
115 | |
116 typedef base::hash_map<uint64, SimpleEntryImpl*> EntryMap; | |
117 | |
118 typedef base::Callback<void(base::Time mtime, uint64 max_size, int result)> | |
119 InitializeIndexCallback; | |
120 | |
121 class ActiveEntryProxy; | |
122 friend class ActiveEntryProxy; | |
123 | |
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; | |
130 }; | |
131 | |
132 void InitializeIndex(const CompletionCallback& callback, | |
133 const DiskStatResult& result); | |
134 | |
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); | |
141 | |
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); | |
146 | |
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); | |
152 | |
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); | |
160 | |
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); | |
165 | |
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); | |
175 | |
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); | |
183 | |
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); | |
189 | |
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_; | |
195 | |
196 int orig_max_size_; | |
197 const SimpleEntryImpl::OperationsMode entry_operations_mode_; | |
198 | |
199 EntryMap active_entries_; | |
200 | |
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_; | |
206 | |
207 net::NetLog* const net_log_; | |
208 }; | |
209 | |
210 } // namespace disk_cache | |
211 | |
212 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ | |
OLD | NEW |