Update V8 to version 4.7.53.
[chromium-blink-merge.git] / net / disk_cache / simple / simple_synchronous_entry.h
blob7f98b21ceaa42c26bcc8128a49fe9fea5234d3a7
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_SYNCHRONOUS_ENTRY_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_
8 #include <algorithm>
9 #include <map>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "base/files/file.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/time/time.h"
19 #include "net/base/cache_type.h"
20 #include "net/base/net_export.h"
21 #include "net/disk_cache/simple/simple_entry_format.h"
23 namespace net {
24 class GrowableIOBuffer;
25 class IOBuffer;
28 namespace disk_cache {
30 class SimpleSynchronousEntry;
32 // This class handles the passing of data about the entry between
33 // SimpleEntryImplementation and SimpleSynchronousEntry and the computation of
34 // file offsets based on the data size for all streams.
35 class NET_EXPORT_PRIVATE SimpleEntryStat {
36 public:
37 SimpleEntryStat(base::Time last_used,
38 base::Time last_modified,
39 const int32 data_size[],
40 const int32 sparse_data_size);
42 int GetOffsetInFile(const std::string& key,
43 int offset,
44 int stream_index) const;
45 int GetEOFOffsetInFile(const std::string& key, int stream_index) const;
46 int GetLastEOFOffsetInFile(const std::string& key, int file_index) const;
47 int64 GetFileSize(const std::string& key, int file_index) const;
49 base::Time last_used() const { return last_used_; }
50 base::Time last_modified() const { return last_modified_; }
51 void set_last_used(base::Time last_used) { last_used_ = last_used; }
52 void set_last_modified(base::Time last_modified) {
53 last_modified_ = last_modified;
56 int32 data_size(int stream_index) const { return data_size_[stream_index]; }
57 void set_data_size(int stream_index, int data_size) {
58 data_size_[stream_index] = data_size;
61 int32 sparse_data_size() const { return sparse_data_size_; }
62 void set_sparse_data_size(int32 sparse_data_size) {
63 sparse_data_size_ = sparse_data_size;
66 private:
67 base::Time last_used_;
68 base::Time last_modified_;
69 int32 data_size_[kSimpleEntryStreamCount];
70 int32 sparse_data_size_;
73 struct SimpleEntryCreationResults {
74 explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat);
75 ~SimpleEntryCreationResults();
77 SimpleSynchronousEntry* sync_entry;
78 scoped_refptr<net::GrowableIOBuffer> stream_0_data;
79 SimpleEntryStat entry_stat;
80 uint32 stream_0_crc32;
81 int result;
84 // Worker thread interface to the very simple cache. This interface is not
85 // thread safe, and callers must ensure that it is only ever accessed from
86 // a single thread between synchronization points.
87 class SimpleSynchronousEntry {
88 public:
89 struct CRCRecord {
90 CRCRecord();
91 CRCRecord(int index_p, bool has_crc32_p, uint32 data_crc32_p);
93 int index;
94 bool has_crc32;
95 uint32 data_crc32;
98 struct EntryOperationData {
99 EntryOperationData(int index_p, int offset_p, int buf_len_p);
100 EntryOperationData(int index_p,
101 int offset_p,
102 int buf_len_p,
103 bool truncate_p,
104 bool doomed_p);
105 EntryOperationData(int64 sparse_offset_p, int buf_len_p);
107 int index;
108 int offset;
109 int64 sparse_offset;
110 int buf_len;
111 bool truncate;
112 bool doomed;
115 static void OpenEntry(net::CacheType cache_type,
116 const base::FilePath& path,
117 uint64 entry_hash,
118 bool had_index,
119 SimpleEntryCreationResults* out_results);
121 static void CreateEntry(net::CacheType cache_type,
122 const base::FilePath& path,
123 const std::string& key,
124 uint64 entry_hash,
125 bool had_index,
126 SimpleEntryCreationResults* out_results);
128 // Deletes an entry from the file system without affecting the state of the
129 // corresponding instance, if any (allowing operations to continue to be
130 // executed through that instance). Returns a net error code.
131 static int DoomEntry(const base::FilePath& path,
132 uint64 entry_hash);
134 // Like |DoomEntry()| above. Deletes all entries corresponding to the
135 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net
136 // error code.
137 static int DoomEntrySet(const std::vector<uint64>* key_hashes,
138 const base::FilePath& path);
140 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO.
141 void ReadData(const EntryOperationData& in_entry_op,
142 net::IOBuffer* out_buf,
143 uint32* out_crc32,
144 SimpleEntryStat* entry_stat,
145 int* out_result) const;
146 void WriteData(const EntryOperationData& in_entry_op,
147 net::IOBuffer* in_buf,
148 SimpleEntryStat* out_entry_stat,
149 int* out_result);
150 void CheckEOFRecord(int index,
151 const SimpleEntryStat& entry_stat,
152 uint32 expected_crc32,
153 int* out_result) const;
155 void ReadSparseData(const EntryOperationData& in_entry_op,
156 net::IOBuffer* out_buf,
157 base::Time* out_last_used,
158 int* out_result);
159 void WriteSparseData(const EntryOperationData& in_entry_op,
160 net::IOBuffer* in_buf,
161 uint64 max_sparse_data_size,
162 SimpleEntryStat* out_entry_stat,
163 int* out_result);
164 void GetAvailableRange(const EntryOperationData& in_entry_op,
165 int64* out_start,
166 int* out_result);
168 // Close all streams, and add write EOF records to streams indicated by the
169 // CRCRecord entries in |crc32s_to_write|.
170 void Close(const SimpleEntryStat& entry_stat,
171 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write,
172 net::GrowableIOBuffer* stream_0_data);
174 const base::FilePath& path() const { return path_; }
175 std::string key() const { return key_; }
177 private:
178 enum CreateEntryResult {
179 CREATE_ENTRY_SUCCESS = 0,
180 CREATE_ENTRY_PLATFORM_FILE_ERROR = 1,
181 CREATE_ENTRY_CANT_WRITE_HEADER = 2,
182 CREATE_ENTRY_CANT_WRITE_KEY = 3,
183 CREATE_ENTRY_MAX = 4,
186 enum FileRequired {
187 FILE_NOT_REQUIRED,
188 FILE_REQUIRED
191 struct SparseRange {
192 int64 offset;
193 int64 length;
194 uint32 data_crc32;
195 int64 file_offset;
197 bool operator<(const SparseRange& other) const {
198 return offset < other.offset;
202 SimpleSynchronousEntry(
203 net::CacheType cache_type,
204 const base::FilePath& path,
205 const std::string& key,
206 uint64 entry_hash);
208 // Like Entry, the SimpleSynchronousEntry self releases when Close() is
209 // called.
210 ~SimpleSynchronousEntry();
212 // Tries to open one of the cache entry files. Succeeds if the open succeeds
213 // or if the file was not found and is allowed to be omitted if the
214 // corresponding stream is empty.
215 bool MaybeOpenFile(int file_index,
216 base::File::Error* out_error);
217 // Creates one of the cache entry files if necessary. If the file is allowed
218 // to be omitted if the corresponding stream is empty, and if |file_required|
219 // is FILE_NOT_REQUIRED, then the file is not created; otherwise, it is.
220 bool MaybeCreateFile(int file_index,
221 FileRequired file_required,
222 base::File::Error* out_error);
223 bool OpenFiles(bool had_index,
224 SimpleEntryStat* out_entry_stat);
225 bool CreateFiles(bool had_index,
226 SimpleEntryStat* out_entry_stat);
227 void CloseFile(int index);
228 void CloseFiles();
230 // Returns a net error, i.e. net::OK on success. |had_index| is passed
231 // from the main entry for metrics purposes, and is true if the index was
232 // initialized when the open operation began.
233 int InitializeForOpen(bool had_index,
234 SimpleEntryStat* out_entry_stat,
235 scoped_refptr<net::GrowableIOBuffer>* stream_0_data,
236 uint32* out_stream_0_crc32);
238 // Writes the header and key to a newly-created stream file. |index| is the
239 // index of the stream. Returns true on success; returns false and sets
240 // |*out_result| on failure.
241 bool InitializeCreatedFile(int index, CreateEntryResult* out_result);
243 // Returns a net error, including net::OK on success and net::FILE_EXISTS
244 // when the entry already exists. |had_index| is passed from the main entry
245 // for metrics purposes, and is true if the index was initialized when the
246 // create operation began.
247 int InitializeForCreate(bool had_index, SimpleEntryStat* out_entry_stat);
249 // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then
250 // checks its crc32.
251 int ReadAndValidateStream0(
252 int total_data_size,
253 SimpleEntryStat* out_entry_stat,
254 scoped_refptr<net::GrowableIOBuffer>* stream_0_data,
255 uint32* out_stream_0_crc32) const;
257 int GetEOFRecordData(int index,
258 const SimpleEntryStat& entry_stat,
259 bool* out_has_crc32,
260 uint32* out_crc32,
261 int* out_data_size) const;
262 void Doom() const;
264 // Opens the sparse data file and scans it if it exists.
265 bool OpenSparseFileIfExists(int32* out_sparse_data_size);
267 // Creates and initializes the sparse data file.
268 bool CreateSparseFile();
270 // Closes the sparse data file.
271 void CloseSparseFile();
273 // Writes the header to the (newly-created) sparse file.
274 bool InitializeSparseFile();
276 // Removes all but the header of the sparse file.
277 bool TruncateSparseFile();
279 // Scans the existing ranges in the sparse file. Populates |sparse_ranges_|
280 // and sets |*out_sparse_data_size| to the total size of all the ranges (not
281 // including headers).
282 bool ScanSparseFile(int32* out_sparse_data_size);
284 // Reads from a single sparse range. If asked to read the entire range, also
285 // verifies the CRC32.
286 bool ReadSparseRange(const SparseRange* range,
287 int offset, int len, char* buf);
289 // Writes to a single (existing) sparse range. If asked to write the entire
290 // range, also updates the CRC32; otherwise, invalidates it.
291 bool WriteSparseRange(SparseRange* range,
292 int offset, int len, const char* buf);
294 // Appends a new sparse range to the sparse data file.
295 bool AppendSparseRange(int64 offset, int len, const char* buf);
297 static bool DeleteFileForEntryHash(const base::FilePath& path,
298 uint64 entry_hash,
299 int file_index);
300 static bool DeleteFilesForEntryHash(const base::FilePath& path,
301 uint64 entry_hash);
303 void RecordSyncCreateResult(CreateEntryResult result, bool had_index);
305 base::FilePath GetFilenameFromFileIndex(int file_index);
307 bool sparse_file_open() const {
308 return sparse_file_.IsValid();
311 const net::CacheType cache_type_;
312 const base::FilePath path_;
313 const uint64 entry_hash_;
314 std::string key_;
316 bool have_open_files_;
317 bool initialized_;
319 base::File files_[kSimpleEntryFileCount];
321 // True if the corresponding stream is empty and therefore no on-disk file
322 // was created to store it.
323 bool empty_file_omitted_[kSimpleEntryFileCount];
325 typedef std::map<int64, SparseRange> SparseRangeOffsetMap;
326 typedef SparseRangeOffsetMap::iterator SparseRangeIterator;
327 SparseRangeOffsetMap sparse_ranges_;
328 base::File sparse_file_;
329 // Offset of the end of the sparse file (where the next sparse range will be
330 // written).
331 int64 sparse_tail_offset_;
333 // True if the entry was created, or false if it was opened. Used to log
334 // SimpleCache.*.EntryCreatedWithStream2Omitted only for created entries.
335 bool files_created_;
338 } // namespace disk_cache
340 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_