diff options
Diffstat (limited to 'chromium/net/disk_cache/simple/simple_synchronous_entry.h')
-rw-r--r-- | chromium/net/disk_cache/simple/simple_synchronous_entry.h | 149 |
1 files changed, 120 insertions, 29 deletions
diff --git a/chromium/net/disk_cache/simple/simple_synchronous_entry.h b/chromium/net/disk_cache/simple/simple_synchronous_entry.h index f591c9a0f93..470e8e20bd8 100644 --- a/chromium/net/disk_cache/simple/simple_synchronous_entry.h +++ b/chromium/net/disk_cache/simple/simple_synchronous_entry.h @@ -11,12 +11,16 @@ #include <vector> #include "base/files/file_path.h" +#include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "base/platform_file.h" #include "base/time/time.h" +#include "net/base/cache_type.h" +#include "net/base/net_export.h" #include "net/disk_cache/simple/simple_entry_format.h" namespace net { +class GrowableIOBuffer; class IOBuffer; } @@ -24,23 +28,48 @@ namespace disk_cache { class SimpleSynchronousEntry; -struct SimpleEntryStat { - SimpleEntryStat(); - SimpleEntryStat(base::Time last_used_p, - base::Time last_modified_p, - const int32 data_size_p[]); +// This class handles the passing of data about the entry between +// SimpleEntryImplementation and SimpleSynchronousEntry and the computation of +// file offsets based on the data size for all streams. +class NET_EXPORT_PRIVATE SimpleEntryStat { + public: + SimpleEntryStat(base::Time last_used, + base::Time last_modified, + const int32 data_size[]); + + int GetOffsetInFile(const std::string& key, + int offset, + int stream_index) const; + int GetEOFOffsetInFile(const std::string& key, int stream_index) const; + int GetLastEOFOffsetInFile(const std::string& key, int file_index) const; + int GetFileSize(const std::string& key, int file_index) const; + + base::Time last_used() const { return last_used_; } + base::Time last_modified() const { return last_modified_; } + void set_last_used(base::Time last_used) { last_used_ = last_used; } + void set_last_modified(base::Time last_modified) { + last_modified_ = last_modified; + } + + int32 data_size(int stream_index) const { return data_size_[stream_index]; } + void set_data_size(int stream_index, int data_size) { + data_size_[stream_index] = data_size; + } - base::Time last_used; - base::Time last_modified; - int32 data_size[kSimpleEntryFileCount]; + private: + base::Time last_used_; + base::Time last_modified_; + int32 data_size_[kSimpleEntryStreamCount]; }; struct SimpleEntryCreationResults { - SimpleEntryCreationResults(SimpleEntryStat entry_stat); + explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat); ~SimpleEntryCreationResults(); SimpleSynchronousEntry* sync_entry; + scoped_refptr<net::GrowableIOBuffer> stream_0_data; SimpleEntryStat entry_stat; + uint32 stream_0_crc32; int result; }; @@ -63,65 +92,81 @@ class SimpleSynchronousEntry { EntryOperationData(int index_p, int offset_p, int buf_len_p, - bool truncate_p); + bool truncate_p, + bool doomed_p); int index; int offset; int buf_len; bool truncate; + bool doomed; }; - static void OpenEntry(const base::FilePath& path, + static void OpenEntry(net::CacheType cache_type, + const base::FilePath& path, uint64 entry_hash, bool had_index, SimpleEntryCreationResults* out_results); - static void CreateEntry(const base::FilePath& path, + static void CreateEntry(net::CacheType cache_type, + const base::FilePath& path, const std::string& key, uint64 entry_hash, bool had_index, SimpleEntryCreationResults* out_results); - // Deletes an entry without first Opening it. Does not check if there is - // already an Entry object in memory holding the open files. Be careful! This - // is meant to be used by the Backend::DoomEntry() call. |callback| will be - // run by |callback_runner|. - static void DoomEntry(const base::FilePath& path, - const std::string& key, - uint64 entry_hash, - int* out_result); + // Deletes an entry from the file system without affecting the state of the + // corresponding instance, if any (allowing operations to continue to be + // executed through that instance). Returns a net error code. + static int DoomEntry(const base::FilePath& path, + uint64 entry_hash); // Like |DoomEntry()| above. Deletes all entries corresponding to the // |key_hashes|. Succeeds only when all entries are deleted. Returns a net // error code. - static int DoomEntrySet(scoped_ptr<std::vector<uint64> > key_hashes, + static int DoomEntrySet(const std::vector<uint64>* key_hashes, const base::FilePath& path); // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO. void ReadData(const EntryOperationData& in_entry_op, net::IOBuffer* out_buf, uint32* out_crc32, - base::Time* out_last_used, + SimpleEntryStat* entry_stat, int* out_result) const; void WriteData(const EntryOperationData& in_entry_op, net::IOBuffer* in_buf, SimpleEntryStat* out_entry_stat, - int* out_result) const; + int* out_result); void CheckEOFRecord(int index, - int data_size, + const SimpleEntryStat& entry_stat, uint32 expected_crc32, int* out_result) const; // Close all streams, and add write EOF records to streams indicated by the // CRCRecord entries in |crc32s_to_write|. void Close(const SimpleEntryStat& entry_stat, - scoped_ptr<std::vector<CRCRecord> > crc32s_to_write); + scoped_ptr<std::vector<CRCRecord> > crc32s_to_write, + net::GrowableIOBuffer* stream_0_data); const base::FilePath& path() const { return path_; } std::string key() const { return key_; } private: + enum CreateEntryResult { + CREATE_ENTRY_SUCCESS = 0, + CREATE_ENTRY_PLATFORM_FILE_ERROR = 1, + CREATE_ENTRY_CANT_WRITE_HEADER = 2, + CREATE_ENTRY_CANT_WRITE_KEY = 3, + CREATE_ENTRY_MAX = 4, + }; + + enum FileRequired { + FILE_NOT_REQUIRED, + FILE_REQUIRED + }; + SimpleSynchronousEntry( + net::CacheType cache_type, const base::FilePath& path, const std::string& key, uint64 entry_hash); @@ -130,15 +175,36 @@ class SimpleSynchronousEntry { // called. ~SimpleSynchronousEntry(); - bool OpenOrCreateFiles(bool create, - bool had_index, - SimpleEntryStat* out_entry_stat); + // Tries to open one of the cache entry files. Succeeds if the open succeeds + // or if the file was not found and is allowed to be omitted if the + // corresponding stream is empty. + bool MaybeOpenFile(int file_index, + base::PlatformFileError* out_error); + // Creates one of the cache entry files if necessary. If the file is allowed + // to be omitted if the corresponding stream is empty, and if |file_required| + // is FILE_NOT_REQUIRED, then the file is not created; otherwise, it is. + bool MaybeCreateFile(int file_index, + FileRequired file_required, + base::PlatformFileError* out_error); + bool OpenFiles(bool had_index, + SimpleEntryStat* out_entry_stat); + bool CreateFiles(bool had_index, + SimpleEntryStat* out_entry_stat); + void CloseFile(int index); void CloseFiles(); // Returns a net error, i.e. net::OK on success. |had_index| is passed // from the main entry for metrics purposes, and is true if the index was // initialized when the open operation began. - int InitializeForOpen(bool had_index, SimpleEntryStat* out_entry_stat); + int InitializeForOpen(bool had_index, + SimpleEntryStat* out_entry_stat, + scoped_refptr<net::GrowableIOBuffer>* stream_0_data, + uint32* out_stream_0_crc32); + + // Writes the header and key to a newly-created stream file. |index| is the + // index of the stream. Returns true on success; returns false and sets + // |*out_result| on failure. + bool InitializeCreatedFile(int index, CreateEntryResult* out_result); // Returns a net error, including net::OK on success and net::FILE_EXISTS // when the entry already exists. |had_index| is passed from the main entry @@ -146,11 +212,32 @@ class SimpleSynchronousEntry { // create operation began. int InitializeForCreate(bool had_index, SimpleEntryStat* out_entry_stat); + // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then + // checks its crc32. + int ReadAndValidateStream0( + int total_data_size, + SimpleEntryStat* out_entry_stat, + scoped_refptr<net::GrowableIOBuffer>* stream_0_data, + uint32* out_stream_0_crc32) const; + + int GetEOFRecordData(int index, + const SimpleEntryStat& entry_stat, + bool* out_has_crc32, + uint32* out_crc32, + int* out_data_size) const; void Doom() const; + static bool DeleteFileForEntryHash(const base::FilePath& path, + uint64 entry_hash, + int file_index); static bool DeleteFilesForEntryHash(const base::FilePath& path, uint64 entry_hash); + void RecordSyncCreateResult(CreateEntryResult result, bool had_index); + + base::FilePath GetFilenameFromFileIndex(int file_index); + + const net::CacheType cache_type_; const base::FilePath path_; const uint64 entry_hash_; std::string key_; @@ -159,6 +246,10 @@ class SimpleSynchronousEntry { bool initialized_; base::PlatformFile files_[kSimpleEntryFileCount]; + + // True if the corresponding stream is empty and therefore no on-disk file + // was created to store it. + bool empty_file_omitted_[kSimpleEntryFileCount]; }; } // namespace disk_cache |