Chromium Code Reviews| 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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 // This class handles the passing of data about the entry between | 34 // This class handles the passing of data about the entry between |
| 35 // SimpleEntryImplementation and SimpleSynchronousEntry and the computation of | 35 // SimpleEntryImplementation and SimpleSynchronousEntry and the computation of |
| 36 // file offsets based on the data size for all streams. | 36 // file offsets based on the data size for all streams. |
| 37 class NET_EXPORT_PRIVATE SimpleEntryStat { | 37 class NET_EXPORT_PRIVATE SimpleEntryStat { |
| 38 public: | 38 public: |
| 39 SimpleEntryStat(base::Time last_used, | 39 SimpleEntryStat(base::Time last_used, |
| 40 base::Time last_modified, | 40 base::Time last_modified, |
| 41 const int32_t data_size[], | 41 const int32_t data_size[], |
| 42 const int32_t sparse_data_size); | 42 const int32_t sparse_data_size); |
| 43 | 43 |
| 44 int GetOffsetInFile(const std::string& key, | 44 int GetOffsetInFile(size_t key_length, int offset, int stream_index) const; |
| 45 int offset, | 45 int GetEOFOffsetInFile(size_t key_length, int stream_index) const; |
| 46 int stream_index) const; | 46 int GetLastEOFOffsetInFile(size_t key_length, int file_index) const; |
| 47 int GetEOFOffsetInFile(const std::string& key, int stream_index) const; | 47 int64_t GetFileSize(size_t key_length, int file_index) const; |
| 48 int GetLastEOFOffsetInFile(const std::string& key, int file_index) const; | |
| 49 int64_t GetFileSize(const std::string& key, int file_index) const; | |
| 50 | 48 |
| 51 base::Time last_used() const { return last_used_; } | 49 base::Time last_used() const { return last_used_; } |
| 52 base::Time last_modified() const { return last_modified_; } | 50 base::Time last_modified() const { return last_modified_; } |
| 53 void set_last_used(base::Time last_used) { last_used_ = last_used; } | 51 void set_last_used(base::Time last_used) { last_used_ = last_used; } |
| 54 void set_last_modified(base::Time last_modified) { | 52 void set_last_modified(base::Time last_modified) { |
| 55 last_modified_ = last_modified; | 53 last_modified_ = last_modified; |
| 56 } | 54 } |
| 57 | 55 |
| 58 int32_t data_size(int stream_index) const { return data_size_[stream_index]; } | 56 int32_t data_size(int stream_index) const { return data_size_[stream_index]; } |
| 59 void set_data_size(int stream_index, int data_size) { | 57 void set_data_size(int stream_index, int data_size) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 EntryOperationData(int64_t sparse_offset_p, int buf_len_p); | 105 EntryOperationData(int64_t sparse_offset_p, int buf_len_p); |
| 108 | 106 |
| 109 int index; | 107 int index; |
| 110 int offset; | 108 int offset; |
| 111 int64_t sparse_offset; | 109 int64_t sparse_offset; |
| 112 int buf_len; | 110 int buf_len; |
| 113 bool truncate; | 111 bool truncate; |
| 114 bool doomed; | 112 bool doomed; |
| 115 }; | 113 }; |
| 116 | 114 |
| 115 // Opens a disk cache entry on disk. The |key| parameter is optional, if empty | |
| 116 // the operation may be slower. The |entry_hash| parameter is required. | |
| 117 // |had_index| is provided only for histograms. | |
|
Randy Smith (Not in Mondays)
2016/05/17 20:04:07
I'm torn here, because for someone reading the cod
gavinp
2016/05/18 15:46:00
Done. Documented it at SetKey in SimpleEntryImpl.
| |
| 117 static void OpenEntry(net::CacheType cache_type, | 118 static void OpenEntry(net::CacheType cache_type, |
| 118 const base::FilePath& path, | 119 const base::FilePath& path, |
| 120 const std::string& key, | |
| 119 uint64_t entry_hash, | 121 uint64_t entry_hash, |
| 120 bool had_index, | 122 bool had_index, |
| 121 SimpleEntryCreationResults* out_results); | 123 SimpleEntryCreationResults* out_results); |
| 122 | 124 |
| 123 static void CreateEntry(net::CacheType cache_type, | 125 static void CreateEntry(net::CacheType cache_type, |
| 124 const base::FilePath& path, | 126 const base::FilePath& path, |
| 125 const std::string& key, | 127 const std::string& key, |
| 126 uint64_t entry_hash, | 128 uint64_t entry_hash, |
| 127 bool had_index, | 129 bool had_index, |
| 128 SimpleEntryCreationResults* out_results); | 130 SimpleEntryCreationResults* out_results); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 143 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net | 145 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net |
| 144 // error code. | 146 // error code. |
| 145 static int DoomEntrySet(const std::vector<uint64_t>* key_hashes, | 147 static int DoomEntrySet(const std::vector<uint64_t>* key_hashes, |
| 146 const base::FilePath& path); | 148 const base::FilePath& path); |
| 147 | 149 |
| 148 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO. | 150 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO. |
| 149 void ReadData(const EntryOperationData& in_entry_op, | 151 void ReadData(const EntryOperationData& in_entry_op, |
| 150 net::IOBuffer* out_buf, | 152 net::IOBuffer* out_buf, |
| 151 uint32_t* out_crc32, | 153 uint32_t* out_crc32, |
| 152 SimpleEntryStat* entry_stat, | 154 SimpleEntryStat* entry_stat, |
| 153 int* out_result) const; | 155 int* out_result); |
| 154 void WriteData(const EntryOperationData& in_entry_op, | 156 void WriteData(const EntryOperationData& in_entry_op, |
| 155 net::IOBuffer* in_buf, | 157 net::IOBuffer* in_buf, |
| 156 SimpleEntryStat* out_entry_stat, | 158 SimpleEntryStat* out_entry_stat, |
| 157 int* out_result); | 159 int* out_result); |
| 158 void CheckEOFRecord(int index, | 160 void CheckEOFRecord(int index, |
| 159 const SimpleEntryStat& entry_stat, | 161 const SimpleEntryStat& entry_stat, |
| 160 uint32_t expected_crc32, | 162 uint32_t expected_crc32, |
| 161 int* out_result) const; | 163 int* out_result) const; |
| 162 | 164 |
| 163 void ReadSparseData(const EntryOperationData& in_entry_op, | 165 void ReadSparseData(const EntryOperationData& in_entry_op, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 203 int64_t file_offset; | 205 int64_t file_offset; |
| 204 | 206 |
| 205 bool operator<(const SparseRange& other) const { | 207 bool operator<(const SparseRange& other) const { |
| 206 return offset < other.offset; | 208 return offset < other.offset; |
| 207 } | 209 } |
| 208 }; | 210 }; |
| 209 | 211 |
| 210 SimpleSynchronousEntry(net::CacheType cache_type, | 212 SimpleSynchronousEntry(net::CacheType cache_type, |
| 211 const base::FilePath& path, | 213 const base::FilePath& path, |
| 212 const std::string& key, | 214 const std::string& key, |
| 213 uint64_t entry_hash); | 215 uint64_t entry_hash, |
| 216 bool had_index); | |
| 214 | 217 |
| 215 // Like Entry, the SimpleSynchronousEntry self releases when Close() is | 218 // Like Entry, the SimpleSynchronousEntry self releases when Close() is |
| 216 // called. | 219 // called. |
| 217 ~SimpleSynchronousEntry(); | 220 ~SimpleSynchronousEntry(); |
| 218 | 221 |
| 219 // Tries to open one of the cache entry files. Succeeds if the open succeeds | 222 // Tries to open one of the cache entry files. Succeeds if the open succeeds |
| 220 // or if the file was not found and is allowed to be omitted if the | 223 // or if the file was not found and is allowed to be omitted if the |
| 221 // corresponding stream is empty. | 224 // corresponding stream is empty. |
| 222 bool MaybeOpenFile(int file_index, | 225 bool MaybeOpenFile(int file_index, |
| 223 base::File::Error* out_error); | 226 base::File::Error* out_error); |
| 224 // Creates one of the cache entry files if necessary. If the file is allowed | 227 // Creates one of the cache entry files if necessary. If the file is allowed |
| 225 // to be omitted if the corresponding stream is empty, and if |file_required| | 228 // to be omitted if the corresponding stream is empty, and if |file_required| |
| 226 // is FILE_NOT_REQUIRED, then the file is not created; otherwise, it is. | 229 // is FILE_NOT_REQUIRED, then the file is not created; otherwise, it is. |
| 227 bool MaybeCreateFile(int file_index, | 230 bool MaybeCreateFile(int file_index, |
| 228 FileRequired file_required, | 231 FileRequired file_required, |
| 229 base::File::Error* out_error); | 232 base::File::Error* out_error); |
| 230 bool OpenFiles(bool had_index, | 233 bool OpenFiles(SimpleEntryStat* out_entry_stat); |
| 231 SimpleEntryStat* out_entry_stat); | 234 bool CreateFiles(SimpleEntryStat* out_entry_stat); |
| 232 bool CreateFiles(bool had_index, | |
| 233 SimpleEntryStat* out_entry_stat); | |
| 234 void CloseFile(int index); | 235 void CloseFile(int index); |
| 235 void CloseFiles(); | 236 void CloseFiles(); |
| 236 | 237 |
| 237 // Returns a net error, i.e. net::OK on success. |had_index| is passed | 238 // Read the header and key at the beginning of the file, and validate that |
| 238 // from the main entry for metrics purposes, and is true if the index was | 239 // they are correct. If this entry was opened with a key, the key is checked |
| 239 // initialized when the open operation began. | 240 // for a match. If not, then the |key_| member is set based on the value in |
| 240 int InitializeForOpen(bool had_index, | 241 // this header. Records histograms if any check is failed. |
| 241 SimpleEntryStat* out_entry_stat, | 242 bool CheckHeaderAndKeyForOpen(int file_index); |
| 243 | |
| 244 // Returns a net error, i.e. net::OK on success. | |
| 245 int InitializeForOpen(SimpleEntryStat* out_entry_stat, | |
| 242 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, | 246 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, |
| 243 uint32_t* out_stream_0_crc32); | 247 uint32_t* out_stream_0_crc32); |
| 244 | 248 |
| 245 // Writes the header and key to a newly-created stream file. |index| is the | 249 // Writes the header and key to a newly-created stream file. |index| is the |
| 246 // index of the stream. Returns true on success; returns false and sets | 250 // index of the stream. Returns true on success; returns false and sets |
| 247 // |*out_result| on failure. | 251 // |*out_result| on failure. |
| 248 bool InitializeCreatedFile(int index, CreateEntryResult* out_result); | 252 bool InitializeCreatedFile(int index, CreateEntryResult* out_result); |
| 249 | 253 |
| 250 // Returns a net error, including net::OK on success and net::FILE_EXISTS | 254 // Returns a net error, including net::OK on success and net::FILE_EXISTS |
| 251 // when the entry already exists. |had_index| is passed from the main entry | 255 // when the entry already exists. |
| 252 // for metrics purposes, and is true if the index was initialized when the | 256 int InitializeForCreate(SimpleEntryStat* out_entry_stat); |
| 253 // create operation began. | |
| 254 int InitializeForCreate(bool had_index, SimpleEntryStat* out_entry_stat); | |
| 255 | 257 |
| 256 // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then | 258 // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then |
| 257 // checks its crc32. | 259 // checks its crc32. |
| 258 int ReadAndValidateStream0( | 260 int ReadAndValidateStream0( |
| 259 int total_data_size, | 261 int total_data_size, |
| 260 SimpleEntryStat* out_entry_stat, | 262 SimpleEntryStat* out_entry_stat, |
| 261 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, | 263 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, |
| 262 uint32_t* out_stream_0_crc32) const; | 264 uint32_t* out_stream_0_crc32) const; |
| 263 | 265 |
| 264 int GetEOFRecordData(int index, | 266 int GetEOFRecordData(int index, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 | 315 |
| 314 base::FilePath GetFilenameFromFileIndex(int file_index); | 316 base::FilePath GetFilenameFromFileIndex(int file_index); |
| 315 | 317 |
| 316 bool sparse_file_open() const { | 318 bool sparse_file_open() const { |
| 317 return sparse_file_.IsValid(); | 319 return sparse_file_.IsValid(); |
| 318 } | 320 } |
| 319 | 321 |
| 320 const net::CacheType cache_type_; | 322 const net::CacheType cache_type_; |
| 321 const base::FilePath path_; | 323 const base::FilePath path_; |
| 322 const uint64_t entry_hash_; | 324 const uint64_t entry_hash_; |
| 325 const bool had_index_; | |
| 323 std::string key_; | 326 std::string key_; |
| 324 | 327 |
| 325 bool have_open_files_; | 328 bool have_open_files_; |
| 326 bool initialized_; | 329 bool initialized_; |
| 327 | 330 |
| 331 // Initially false, this is set to true when an entry is opened and the | |
| 332 // headers have not yet been checked. Any subsequent Read of data should | |
| 333 // validate them before completing. | |
|
Randy Smith (Not in Mondays)
2016/05/17 20:04:08
Suggestion: I'd find this comment more understanda
gavinp
2016/05/18 15:46:01
Done.
| |
| 334 bool header_and_key_check_needed_; | |
| 335 | |
| 328 base::File files_[kSimpleEntryFileCount]; | 336 base::File files_[kSimpleEntryFileCount]; |
| 329 | 337 |
| 330 // True if the corresponding stream is empty and therefore no on-disk file | 338 // True if the corresponding stream is empty and therefore no on-disk file |
| 331 // was created to store it. | 339 // was created to store it. |
| 332 bool empty_file_omitted_[kSimpleEntryFileCount]; | 340 bool empty_file_omitted_[kSimpleEntryFileCount]; |
| 333 | 341 |
| 334 typedef std::map<int64_t, SparseRange> SparseRangeOffsetMap; | 342 typedef std::map<int64_t, SparseRange> SparseRangeOffsetMap; |
| 335 typedef SparseRangeOffsetMap::iterator SparseRangeIterator; | 343 typedef SparseRangeOffsetMap::iterator SparseRangeIterator; |
| 336 SparseRangeOffsetMap sparse_ranges_; | 344 SparseRangeOffsetMap sparse_ranges_; |
| 337 base::File sparse_file_; | 345 base::File sparse_file_; |
| 338 // Offset of the end of the sparse file (where the next sparse range will be | 346 // Offset of the end of the sparse file (where the next sparse range will be |
| 339 // written). | 347 // written). |
| 340 int64_t sparse_tail_offset_; | 348 int64_t sparse_tail_offset_; |
| 341 | 349 |
| 342 // True if the entry was created, or false if it was opened. Used to log | 350 // True if the entry was created, or false if it was opened. Used to log |
| 343 // SimpleCache.*.EntryCreatedWithStream2Omitted only for created entries. | 351 // SimpleCache.*.EntryCreatedWithStream2Omitted only for created entries. |
| 344 bool files_created_; | 352 bool files_created_; |
| 345 }; | 353 }; |
| 346 | 354 |
| 347 } // namespace disk_cache | 355 } // namespace disk_cache |
| 348 | 356 |
| 349 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | 357 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ |
| OLD | NEW |