| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ |
| 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 static void CreateEntry(net::CacheType cache_type, | 121 static void CreateEntry(net::CacheType cache_type, |
| 122 const base::FilePath& path, | 122 const base::FilePath& path, |
| 123 const std::string& key, | 123 const std::string& key, |
| 124 uint64 entry_hash, | 124 uint64 entry_hash, |
| 125 bool had_index, | 125 bool had_index, |
| 126 SimpleEntryCreationResults* out_results); | 126 SimpleEntryCreationResults* out_results); |
| 127 | 127 |
| 128 // Deletes an entry from the file system without affecting the state of the | 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 | 129 // corresponding instance, if any (allowing operations to continue to be |
| 130 // executed through that instance). Returns a net error code. | 130 // executed through that instance). Returns a net error code. |
| 131 static int DoomEntry(const base::FilePath& path, | 131 static int DoomEntry(const base::FilePath& path, uint64 entry_hash); |
| 132 uint64 entry_hash); | |
| 133 | 132 |
| 134 // Like |DoomEntry()| above. Deletes all entries corresponding to the | 133 // Like |DoomEntry()| above. Deletes all entries corresponding to the |
| 135 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net | 134 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net |
| 136 // error code. | 135 // error code. |
| 137 static int DoomEntrySet(const std::vector<uint64>* key_hashes, | 136 static int DoomEntrySet(const std::vector<uint64>* key_hashes, |
| 138 const base::FilePath& path); | 137 const base::FilePath& path); |
| 139 | 138 |
| 140 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO. | 139 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO. |
| 141 void ReadData(const EntryOperationData& in_entry_op, | 140 void ReadData(const EntryOperationData& in_entry_op, |
| 142 net::IOBuffer* out_buf, | 141 net::IOBuffer* out_buf, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 | 175 |
| 177 private: | 176 private: |
| 178 enum CreateEntryResult { | 177 enum CreateEntryResult { |
| 179 CREATE_ENTRY_SUCCESS = 0, | 178 CREATE_ENTRY_SUCCESS = 0, |
| 180 CREATE_ENTRY_PLATFORM_FILE_ERROR = 1, | 179 CREATE_ENTRY_PLATFORM_FILE_ERROR = 1, |
| 181 CREATE_ENTRY_CANT_WRITE_HEADER = 2, | 180 CREATE_ENTRY_CANT_WRITE_HEADER = 2, |
| 182 CREATE_ENTRY_CANT_WRITE_KEY = 3, | 181 CREATE_ENTRY_CANT_WRITE_KEY = 3, |
| 183 CREATE_ENTRY_MAX = 4, | 182 CREATE_ENTRY_MAX = 4, |
| 184 }; | 183 }; |
| 185 | 184 |
| 186 enum FileRequired { | 185 enum FileRequired { FILE_NOT_REQUIRED, FILE_REQUIRED }; |
| 187 FILE_NOT_REQUIRED, | |
| 188 FILE_REQUIRED | |
| 189 }; | |
| 190 | 186 |
| 191 struct SparseRange { | 187 struct SparseRange { |
| 192 int64 offset; | 188 int64 offset; |
| 193 int64 length; | 189 int64 length; |
| 194 uint32 data_crc32; | 190 uint32 data_crc32; |
| 195 int64 file_offset; | 191 int64 file_offset; |
| 196 | 192 |
| 197 bool operator<(const SparseRange& other) const { | 193 bool operator<(const SparseRange& other) const { |
| 198 return offset < other.offset; | 194 return offset < other.offset; |
| 199 } | 195 } |
| 200 }; | 196 }; |
| 201 | 197 |
| 202 SimpleSynchronousEntry( | 198 SimpleSynchronousEntry(net::CacheType cache_type, |
| 203 net::CacheType cache_type, | 199 const base::FilePath& path, |
| 204 const base::FilePath& path, | 200 const std::string& key, |
| 205 const std::string& key, | 201 uint64 entry_hash); |
| 206 uint64 entry_hash); | |
| 207 | 202 |
| 208 // Like Entry, the SimpleSynchronousEntry self releases when Close() is | 203 // Like Entry, the SimpleSynchronousEntry self releases when Close() is |
| 209 // called. | 204 // called. |
| 210 ~SimpleSynchronousEntry(); | 205 ~SimpleSynchronousEntry(); |
| 211 | 206 |
| 212 // Tries to open one of the cache entry files. Succeeds if the open succeeds | 207 // 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 | 208 // or if the file was not found and is allowed to be omitted if the |
| 214 // corresponding stream is empty. | 209 // corresponding stream is empty. |
| 215 bool MaybeOpenFile(int file_index, | 210 bool MaybeOpenFile(int file_index, base::File::Error* out_error); |
| 216 base::File::Error* out_error); | |
| 217 // Creates one of the cache entry files if necessary. If the file is allowed | 211 // 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| | 212 // 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. | 213 // is FILE_NOT_REQUIRED, then the file is not created; otherwise, it is. |
| 220 bool MaybeCreateFile(int file_index, | 214 bool MaybeCreateFile(int file_index, |
| 221 FileRequired file_required, | 215 FileRequired file_required, |
| 222 base::File::Error* out_error); | 216 base::File::Error* out_error); |
| 223 bool OpenFiles(bool had_index, | 217 bool OpenFiles(bool had_index, SimpleEntryStat* out_entry_stat); |
| 224 SimpleEntryStat* out_entry_stat); | 218 bool CreateFiles(bool had_index, SimpleEntryStat* out_entry_stat); |
| 225 bool CreateFiles(bool had_index, | |
| 226 SimpleEntryStat* out_entry_stat); | |
| 227 void CloseFile(int index); | 219 void CloseFile(int index); |
| 228 void CloseFiles(); | 220 void CloseFiles(); |
| 229 | 221 |
| 230 // Returns a net error, i.e. net::OK on success. |had_index| is passed | 222 // 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 | 223 // from the main entry for metrics purposes, and is true if the index was |
| 232 // initialized when the open operation began. | 224 // initialized when the open operation began. |
| 233 int InitializeForOpen(bool had_index, | 225 int InitializeForOpen(bool had_index, |
| 234 SimpleEntryStat* out_entry_stat, | 226 SimpleEntryStat* out_entry_stat, |
| 235 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, | 227 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, |
| 236 uint32* out_stream_0_crc32); | 228 uint32* out_stream_0_crc32); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 bool TruncateSparseFile(); | 269 bool TruncateSparseFile(); |
| 278 | 270 |
| 279 // Scans the existing ranges in the sparse file. Populates |sparse_ranges_| | 271 // 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 | 272 // and sets |*out_sparse_data_size| to the total size of all the ranges (not |
| 281 // including headers). | 273 // including headers). |
| 282 bool ScanSparseFile(int32* out_sparse_data_size); | 274 bool ScanSparseFile(int32* out_sparse_data_size); |
| 283 | 275 |
| 284 // Reads from a single sparse range. If asked to read the entire range, also | 276 // Reads from a single sparse range. If asked to read the entire range, also |
| 285 // verifies the CRC32. | 277 // verifies the CRC32. |
| 286 bool ReadSparseRange(const SparseRange* range, | 278 bool ReadSparseRange(const SparseRange* range, |
| 287 int offset, int len, char* buf); | 279 int offset, |
| 280 int len, |
| 281 char* buf); |
| 288 | 282 |
| 289 // Writes to a single (existing) sparse range. If asked to write the entire | 283 // Writes to a single (existing) sparse range. If asked to write the entire |
| 290 // range, also updates the CRC32; otherwise, invalidates it. | 284 // range, also updates the CRC32; otherwise, invalidates it. |
| 291 bool WriteSparseRange(SparseRange* range, | 285 bool WriteSparseRange(SparseRange* range, |
| 292 int offset, int len, const char* buf); | 286 int offset, |
| 287 int len, |
| 288 const char* buf); |
| 293 | 289 |
| 294 // Appends a new sparse range to the sparse data file. | 290 // Appends a new sparse range to the sparse data file. |
| 295 bool AppendSparseRange(int64 offset, int len, const char* buf); | 291 bool AppendSparseRange(int64 offset, int len, const char* buf); |
| 296 | 292 |
| 297 static bool DeleteFileForEntryHash(const base::FilePath& path, | 293 static bool DeleteFileForEntryHash(const base::FilePath& path, |
| 298 uint64 entry_hash, | 294 uint64 entry_hash, |
| 299 int file_index); | 295 int file_index); |
| 300 static bool DeleteFilesForEntryHash(const base::FilePath& path, | 296 static bool DeleteFilesForEntryHash(const base::FilePath& path, |
| 301 uint64 entry_hash); | 297 uint64 entry_hash); |
| 302 | 298 |
| 303 void RecordSyncCreateResult(CreateEntryResult result, bool had_index); | 299 void RecordSyncCreateResult(CreateEntryResult result, bool had_index); |
| 304 | 300 |
| 305 base::FilePath GetFilenameFromFileIndex(int file_index); | 301 base::FilePath GetFilenameFromFileIndex(int file_index); |
| 306 | 302 |
| 307 bool sparse_file_open() const { | 303 bool sparse_file_open() const { return sparse_file_.IsValid(); } |
| 308 return sparse_file_.IsValid(); | |
| 309 } | |
| 310 | 304 |
| 311 const net::CacheType cache_type_; | 305 const net::CacheType cache_type_; |
| 312 const base::FilePath path_; | 306 const base::FilePath path_; |
| 313 const uint64 entry_hash_; | 307 const uint64 entry_hash_; |
| 314 std::string key_; | 308 std::string key_; |
| 315 | 309 |
| 316 bool have_open_files_; | 310 bool have_open_files_; |
| 317 bool initialized_; | 311 bool initialized_; |
| 318 | 312 |
| 319 base::File files_[kSimpleEntryFileCount]; | 313 base::File files_[kSimpleEntryFileCount]; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 331 int64 sparse_tail_offset_; | 325 int64 sparse_tail_offset_; |
| 332 | 326 |
| 333 // True if the entry was created, or false if it was opened. Used to log | 327 // True if the entry was created, or false if it was opened. Used to log |
| 334 // SimpleCache.*.EntryCreatedWithStream2Omitted only for created entries. | 328 // SimpleCache.*.EntryCreatedWithStream2Omitted only for created entries. |
| 335 bool files_created_; | 329 bool files_created_; |
| 336 }; | 330 }; |
| 337 | 331 |
| 338 } // namespace disk_cache | 332 } // namespace disk_cache |
| 339 | 333 |
| 340 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | 334 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ |
| OLD | NEW |