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> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/feature_list.h" | |
| 17 #include "base/files/file.h" | 18 #include "base/files/file.h" |
| 18 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
| 19 #include "base/gtest_prod_util.h" | 20 #include "base/gtest_prod_util.h" |
| 20 #include "base/memory/ref_counted.h" | 21 #include "base/memory/ref_counted.h" |
| 22 #include "base/strings/string_piece_forward.h" | |
| 21 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 22 #include "net/base/cache_type.h" | 24 #include "net/base/cache_type.h" |
| 23 #include "net/base/net_export.h" | 25 #include "net/base/net_export.h" |
| 24 #include "net/disk_cache/simple/simple_entry_format.h" | 26 #include "net/disk_cache/simple/simple_entry_format.h" |
| 25 | 27 |
| 26 namespace net { | 28 namespace net { |
| 27 class GrowableIOBuffer; | 29 class GrowableIOBuffer; |
| 28 class IOBuffer; | 30 class IOBuffer; |
| 29 } | 31 } |
| 30 | 32 |
| 31 FORWARD_DECLARE_TEST(DiskCacheBackendTest, SimpleCacheEnumerationLongKeys); | 33 FORWARD_DECLARE_TEST(DiskCacheBackendTest, SimpleCacheEnumerationLongKeys); |
| 32 | 34 |
| 33 namespace disk_cache { | 35 namespace disk_cache { |
| 34 | 36 |
| 37 NET_EXPORT_PRIVATE extern const base::Feature kSimpleCachePrefetchExperiment; | |
| 38 NET_EXPORT_PRIVATE extern const char kSimplePrefetchBytesParam[]; | |
| 39 | |
| 40 // Returns how large a file would get prefetched on reading the entry. | |
| 41 // If the experiment is disabled, returns 0. | |
| 42 NET_EXPORT_PRIVATE int GetSimpleCachePrefetchSize(); | |
| 43 | |
| 35 class SimpleSynchronousEntry; | 44 class SimpleSynchronousEntry; |
| 36 | 45 |
| 37 // This class handles the passing of data about the entry between | 46 // This class handles the passing of data about the entry between |
| 38 // SimpleEntryImplementation and SimpleSynchronousEntry and the computation of | 47 // SimpleEntryImplementation and SimpleSynchronousEntry and the computation of |
| 39 // file offsets based on the data size for all streams. | 48 // file offsets based on the data size for all streams. |
| 40 class NET_EXPORT_PRIVATE SimpleEntryStat { | 49 class NET_EXPORT_PRIVATE SimpleEntryStat { |
| 41 public: | 50 public: |
| 42 SimpleEntryStat(base::Time last_used, | 51 SimpleEntryStat(base::Time last_used, |
| 43 base::Time last_modified, | 52 base::Time last_modified, |
| 44 const int32_t data_size[], | 53 const int32_t data_size[], |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 66 sparse_data_size_ = sparse_data_size; | 75 sparse_data_size_ = sparse_data_size; |
| 67 } | 76 } |
| 68 | 77 |
| 69 private: | 78 private: |
| 70 base::Time last_used_; | 79 base::Time last_used_; |
| 71 base::Time last_modified_; | 80 base::Time last_modified_; |
| 72 int32_t data_size_[kSimpleEntryStreamCount]; | 81 int32_t data_size_[kSimpleEntryStreamCount]; |
| 73 int32_t sparse_data_size_; | 82 int32_t sparse_data_size_; |
| 74 }; | 83 }; |
| 75 | 84 |
| 85 struct SimpleStreamPrefetchData { | |
|
pasko
2017/08/04 01:28:35
Arguably we should use "a struct only for passive
Maks Orlovich
2017/08/07 13:09:01
Is it that different from having an std::string me
pasko
2017/08/09 12:28:21
I think std::string and scoped_refptr equally make
| |
| 86 SimpleStreamPrefetchData(); | |
| 87 ~SimpleStreamPrefetchData(); | |
| 88 | |
| 89 scoped_refptr<net::GrowableIOBuffer> data; | |
| 90 uint32_t stream_crc32; | |
| 91 }; | |
| 92 | |
| 76 struct SimpleEntryCreationResults { | 93 struct SimpleEntryCreationResults { |
|
pasko
2017/08/04 01:28:34
then this should also be a class?
| |
| 77 explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat); | 94 explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat); |
| 78 ~SimpleEntryCreationResults(); | 95 ~SimpleEntryCreationResults(); |
| 79 | 96 |
| 80 SimpleSynchronousEntry* sync_entry; | 97 SimpleSynchronousEntry* sync_entry; |
| 81 scoped_refptr<net::GrowableIOBuffer> stream_0_data; | 98 |
| 99 // Expectation is that [0] will always be filled in, but [1] might not be. | |
| 100 SimpleStreamPrefetchData stream_prefetch_data[2]; | |
| 101 | |
| 82 SimpleEntryStat entry_stat; | 102 SimpleEntryStat entry_stat; |
| 83 uint32_t stream_0_crc32; | |
| 84 int result; | 103 int result; |
| 85 }; | 104 }; |
| 86 | 105 |
| 87 // Worker thread interface to the very simple cache. This interface is not | 106 // Worker thread interface to the very simple cache. This interface is not |
| 88 // thread safe, and callers must ensure that it is only ever accessed from | 107 // thread safe, and callers must ensure that it is only ever accessed from |
| 89 // a single thread between synchronization points. | 108 // a single thread between synchronization points. |
| 90 class SimpleSynchronousEntry { | 109 class SimpleSynchronousEntry { |
| 91 public: | 110 public: |
| 92 struct CRCRecord { | 111 struct CRCRecord { |
| 93 CRCRecord(); | 112 CRCRecord(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 // requested. | 200 // requested. |
| 182 void ReadData(const EntryOperationData& in_entry_op, | 201 void ReadData(const EntryOperationData& in_entry_op, |
| 183 CRCRequest* crc_request, | 202 CRCRequest* crc_request, |
| 184 SimpleEntryStat* entry_stat, | 203 SimpleEntryStat* entry_stat, |
| 185 net::IOBuffer* out_buf, | 204 net::IOBuffer* out_buf, |
| 186 int* out_result); | 205 int* out_result); |
| 187 void WriteData(const EntryOperationData& in_entry_op, | 206 void WriteData(const EntryOperationData& in_entry_op, |
| 188 net::IOBuffer* in_buf, | 207 net::IOBuffer* in_buf, |
| 189 SimpleEntryStat* out_entry_stat, | 208 SimpleEntryStat* out_entry_stat, |
| 190 int* out_result); | 209 int* out_result); |
| 191 int CheckEOFRecord(int index, | 210 int CheckEOFRecord(int stream_index, |
| 192 const SimpleEntryStat& entry_stat, | 211 const SimpleEntryStat& entry_stat, |
| 193 uint32_t expected_crc32) const; | 212 uint32_t expected_crc32); |
| 194 | 213 |
| 195 void ReadSparseData(const EntryOperationData& in_entry_op, | 214 void ReadSparseData(const EntryOperationData& in_entry_op, |
| 196 net::IOBuffer* out_buf, | 215 net::IOBuffer* out_buf, |
| 197 base::Time* out_last_used, | 216 base::Time* out_last_used, |
| 198 int* out_result); | 217 int* out_result); |
| 199 void WriteSparseData(const EntryOperationData& in_entry_op, | 218 void WriteSparseData(const EntryOperationData& in_entry_op, |
| 200 net::IOBuffer* in_buf, | 219 net::IOBuffer* in_buf, |
| 201 uint64_t max_sparse_data_size, | 220 uint64_t max_sparse_data_size, |
| 202 SimpleEntryStat* out_entry_stat, | 221 SimpleEntryStat* out_entry_stat, |
| 203 int* out_result); | 222 int* out_result); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 void CloseFiles(); | 293 void CloseFiles(); |
| 275 | 294 |
| 276 // Read the header and key at the beginning of the file, and validate that | 295 // Read the header and key at the beginning of the file, and validate that |
| 277 // they are correct. If this entry was opened with a key, the key is checked | 296 // they are correct. If this entry was opened with a key, the key is checked |
| 278 // for a match. If not, then the |key_| member is set based on the value in | 297 // for a match. If not, then the |key_| member is set based on the value in |
| 279 // this header. Records histograms if any check is failed. | 298 // this header. Records histograms if any check is failed. |
| 280 bool CheckHeaderAndKey(int file_index); | 299 bool CheckHeaderAndKey(int file_index); |
| 281 | 300 |
| 282 // Returns a net error, i.e. net::OK on success. | 301 // Returns a net error, i.e. net::OK on success. |
| 283 int InitializeForOpen(SimpleEntryStat* out_entry_stat, | 302 int InitializeForOpen(SimpleEntryStat* out_entry_stat, |
| 284 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, | 303 SimpleStreamPrefetchData* stream_prefetch_data); |
| 285 uint32_t* out_stream_0_crc32); | |
| 286 | 304 |
| 287 // Writes the header and key to a newly-created stream file. |index| is the | 305 // Writes the header and key to a newly-created stream file. |index| is the |
| 288 // index of the stream. Returns true on success; returns false and sets | 306 // index of the stream. Returns true on success; returns false and sets |
| 289 // |*out_result| on failure. | 307 // |*out_result| on failure. |
| 290 bool InitializeCreatedFile(int index, CreateEntryResult* out_result); | 308 bool InitializeCreatedFile(int index, CreateEntryResult* out_result); |
| 291 | 309 |
| 292 // Returns a net error, including net::OK on success and net::FILE_EXISTS | 310 // Returns a net error, including net::OK on success and net::FILE_EXISTS |
| 293 // when the entry already exists. | 311 // when the entry already exists. |
| 294 int InitializeForCreate(SimpleEntryStat* out_entry_stat); | 312 int InitializeForCreate(SimpleEntryStat* out_entry_stat); |
| 295 | 313 |
| 296 // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then | 314 // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then |
| 297 // checks its crc32. | 315 // checks its crc32. May also optionally read in |stream_1_data| and its |
| 298 int ReadAndValidateStream0( | 316 // crc, but might decide not to. |
| 317 int ReadAndValidateStream0AndMaybe1( | |
| 299 int file_size, | 318 int file_size, |
| 300 SimpleEntryStat* out_entry_stat, | 319 SimpleEntryStat* out_entry_stat, |
| 301 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, | 320 SimpleStreamPrefetchData* stream_prefetch_data); |
|
pasko
2017/08/04 01:28:35
We should explain that |stream_prefetch_data| is a
Maks Orlovich
2017/08/04 18:35:44
Done (and elsewhere)
| |
| 302 uint32_t* out_stream_0_crc32); | |
| 303 | 321 |
| 304 int GetEOFRecordData(int index, | 322 // Reads the EOF record for stream |stream_index| from the appropriate file, |
| 323 // or in memory prefetched version in |file_0_prefetch| | |
| 324 // using |entry_stat| for layout info, and putting result into | |
| 325 // |*eof_record|. Sanity-checks the result. | |
| 326 // Returns net status, and records any failures to UMA. | |
| 327 int GetEOFRecordData(base::StringPiece file_0_prefetch, | |
| 328 int stream_index, | |
| 305 const SimpleEntryStat& entry_stat, | 329 const SimpleEntryStat& entry_stat, |
| 306 bool* out_has_crc32, | 330 SimpleFileEOF* eof_record); |
| 307 bool* out_has_key_sha256, | 331 |
| 308 uint32_t* out_crc32, | 332 // Reads either from |file_0_prefetch| or files_[file_index]. |
| 309 int32_t* out_data_size) const; | 333 // Range-checks all the in-memory reads. |
| 334 bool ReadFromFileOrPrefetched(base::StringPiece file_0_prefetch, | |
| 335 int file_index, | |
| 336 int offset, | |
| 337 int size, | |
| 338 char* dest); | |
| 339 | |
| 340 // Extracts out the payload of stream |stream_index|, reading either from | |
| 341 // |file_0_prefetch|, if available, or the file. |entry_stat| will be used to | |
| 342 // determine file layout, though |extra_size| additional bytes will be read | |
| 343 // past the stream payload end. | |
| 344 // | |
| 345 // |*stream_data| will be pointed to a fresh buffer with the results, | |
| 346 // and |*out_crc32| will get the checksum, which will be verified against | |
| 347 // |eof_record|. | |
| 348 int PreReadStreamPayload(base::StringPiece file_0_prefetch, | |
| 349 int stream_index, | |
| 350 int extra_size, | |
| 351 const SimpleEntryStat& entry_stat, | |
| 352 const SimpleFileEOF& eof_record, | |
| 353 scoped_refptr<net::GrowableIOBuffer>* stream_data, | |
|
pasko
2017/08/04 01:28:34
why not producing SimpleStreamPrefetchData instead
Maks Orlovich
2017/08/04 18:35:44
Done.
| |
| 354 uint32_t* out_crc32); | |
| 355 | |
| 310 void Doom() const; | 356 void Doom() const; |
| 311 | 357 |
| 312 // Opens the sparse data file and scans it if it exists. | 358 // Opens the sparse data file and scans it if it exists. |
| 313 bool OpenSparseFileIfExists(int32_t* out_sparse_data_size); | 359 bool OpenSparseFileIfExists(int32_t* out_sparse_data_size); |
| 314 | 360 |
| 315 // Creates and initializes the sparse data file. | 361 // Creates and initializes the sparse data file. |
| 316 bool CreateSparseFile(); | 362 bool CreateSparseFile(); |
| 317 | 363 |
| 318 // Closes the sparse data file. | 364 // Closes the sparse data file. |
| 319 void CloseSparseFile(); | 365 void CloseSparseFile(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 int64_t sparse_tail_offset_; | 435 int64_t sparse_tail_offset_; |
| 390 | 436 |
| 391 // True if the entry was created, or false if it was opened. Used to log | 437 // True if the entry was created, or false if it was opened. Used to log |
| 392 // SimpleCache.*.EntryCreatedWithStream2Omitted only for created entries. | 438 // SimpleCache.*.EntryCreatedWithStream2Omitted only for created entries. |
| 393 bool files_created_; | 439 bool files_created_; |
| 394 }; | 440 }; |
| 395 | 441 |
| 396 } // namespace disk_cache | 442 } // namespace disk_cache |
| 397 | 443 |
| 398 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | 444 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ |
| OLD | NEW |