| 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 <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/platform_file.h" | 16 #include "base/platform_file.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "net/base/cache_type.h" | 18 #include "net/base/cache_type.h" |
| 18 #include "net/disk_cache/simple/simple_entry_format.h" | 19 #include "net/disk_cache/simple/simple_entry_format.h" |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 22 class GrowableIOBuffer; |
| 21 class IOBuffer; | 23 class IOBuffer; |
| 22 } | 24 } |
| 23 | 25 |
| 24 namespace disk_cache { | 26 namespace disk_cache { |
| 25 | 27 |
| 26 class SimpleSynchronousEntry; | 28 class SimpleSynchronousEntry; |
| 27 | 29 |
| 28 struct SimpleEntryStat { | 30 struct SimpleEntryStat { |
| 29 SimpleEntryStat(); | 31 SimpleEntryStat(); |
| 30 SimpleEntryStat(base::Time last_used_p, | 32 SimpleEntryStat(base::Time last_used_p, |
| 31 base::Time last_modified_p, | 33 base::Time last_modified_p, |
| 32 const int32 data_size_p[]); | 34 const int32 data_size_p[]); |
| 33 | 35 |
| 34 base::Time last_used; | 36 base::Time last_used; |
| 35 base::Time last_modified; | 37 base::Time last_modified; |
| 36 int32 data_size[kSimpleEntryFileCount]; | 38 int32 data_size[kSimpleEntryStreamCount]; |
| 37 }; | 39 }; |
| 38 | 40 |
| 39 struct SimpleEntryCreationResults { | 41 struct SimpleEntryCreationResults { |
| 40 explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat); | 42 explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat); |
| 41 ~SimpleEntryCreationResults(); | 43 ~SimpleEntryCreationResults(); |
| 42 | 44 |
| 43 SimpleSynchronousEntry* sync_entry; | 45 SimpleSynchronousEntry* sync_entry; |
| 46 scoped_refptr<net::GrowableIOBuffer> stream_0_data; |
| 44 SimpleEntryStat entry_stat; | 47 SimpleEntryStat entry_stat; |
| 45 int result; | 48 int result; |
| 46 }; | 49 }; |
| 47 | 50 |
| 48 // Worker thread interface to the very simple cache. This interface is not | 51 // Worker thread interface to the very simple cache. This interface is not |
| 49 // thread safe, and callers must ensure that it is only ever accessed from | 52 // thread safe, and callers must ensure that it is only ever accessed from |
| 50 // a single thread between synchronization points. | 53 // a single thread between synchronization points. |
| 51 class SimpleSynchronousEntry { | 54 class SimpleSynchronousEntry { |
| 52 public: | 55 public: |
| 53 struct CRCRecord { | 56 struct CRCRecord { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 // Like |DoomEntry()| above. Deletes all entries corresponding to the | 98 // Like |DoomEntry()| above. Deletes all entries corresponding to the |
| 96 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net | 99 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net |
| 97 // error code. | 100 // error code. |
| 98 static int DoomEntrySet(scoped_ptr<std::vector<uint64> > key_hashes, | 101 static int DoomEntrySet(scoped_ptr<std::vector<uint64> > key_hashes, |
| 99 const base::FilePath& path); | 102 const base::FilePath& path); |
| 100 | 103 |
| 101 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO. | 104 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO. |
| 102 void ReadData(const EntryOperationData& in_entry_op, | 105 void ReadData(const EntryOperationData& in_entry_op, |
| 103 net::IOBuffer* out_buf, | 106 net::IOBuffer* out_buf, |
| 104 uint32* out_crc32, | 107 uint32* out_crc32, |
| 105 base::Time* out_last_used, | 108 SimpleEntryStat* out_entry_stat, |
| 106 int* out_result) const; | 109 int* out_result) const; |
| 107 void WriteData(const EntryOperationData& in_entry_op, | 110 void WriteData(const EntryOperationData& in_entry_op, |
| 108 net::IOBuffer* in_buf, | 111 net::IOBuffer* in_buf, |
| 109 SimpleEntryStat* out_entry_stat, | 112 SimpleEntryStat* out_entry_stat, |
| 110 int* out_result) const; | 113 int* out_result) const; |
| 111 void CheckEOFRecord(int index, | 114 void CheckEOFRecord(int index, |
| 112 int data_size, | 115 const int data_size[], |
| 113 uint32 expected_crc32, | 116 uint32 expected_crc32, |
| 114 int* out_result) const; | 117 int* out_result) const; |
| 115 | 118 |
| 116 // Close all streams, and add write EOF records to streams indicated by the | 119 // Close all streams, and add write EOF records to streams indicated by the |
| 117 // CRCRecord entries in |crc32s_to_write|. | 120 // CRCRecord entries in |crc32s_to_write|. |
| 118 void Close(const SimpleEntryStat& entry_stat, | 121 void Close(const SimpleEntryStat& entry_stat, |
| 119 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write); | 122 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write, |
| 123 net::GrowableIOBuffer* stream_0_data); |
| 120 | 124 |
| 121 const base::FilePath& path() const { return path_; } | 125 const base::FilePath& path() const { return path_; } |
| 122 std::string key() const { return key_; } | 126 std::string key() const { return key_; } |
| 123 | 127 |
| 124 private: | 128 private: |
| 125 SimpleSynchronousEntry( | 129 SimpleSynchronousEntry( |
| 126 net::CacheType cache_type, | 130 net::CacheType cache_type, |
| 127 const base::FilePath& path, | 131 const base::FilePath& path, |
| 128 const std::string& key, | 132 const std::string& key, |
| 129 uint64 entry_hash); | 133 uint64 entry_hash); |
| 130 | 134 |
| 131 // Like Entry, the SimpleSynchronousEntry self releases when Close() is | 135 // Like Entry, the SimpleSynchronousEntry self releases when Close() is |
| 132 // called. | 136 // called. |
| 133 ~SimpleSynchronousEntry(); | 137 ~SimpleSynchronousEntry(); |
| 134 | 138 |
| 135 bool OpenOrCreateFiles(bool create, | 139 bool OpenOrCreateFiles(bool create, |
| 136 bool had_index, | 140 bool had_index, |
| 137 SimpleEntryStat* out_entry_stat); | 141 SimpleEntryStat* out_entry_stat); |
| 138 void CloseFiles(); | 142 void CloseFiles(); |
| 139 | 143 |
| 140 // Returns a net error, i.e. net::OK on success. |had_index| is passed | 144 // Returns a net error, i.e. net::OK on success. |had_index| is passed |
| 141 // from the main entry for metrics purposes, and is true if the index was | 145 // from the main entry for metrics purposes, and is true if the index was |
| 142 // initialized when the open operation began. | 146 // initialized when the open operation began. |
| 143 int InitializeForOpen(bool had_index, SimpleEntryStat* out_entry_stat); | 147 int InitializeForOpen(bool had_index, |
| 148 SimpleEntryStat* out_entry_stat, |
| 149 scoped_refptr<net::GrowableIOBuffer>* stream_0_data); |
| 144 | 150 |
| 145 // Returns a net error, including net::OK on success and net::FILE_EXISTS | 151 // Returns a net error, including net::OK on success and net::FILE_EXISTS |
| 146 // when the entry already exists. |had_index| is passed from the main entry | 152 // when the entry already exists. |had_index| is passed from the main entry |
| 147 // for metrics purposes, and is true if the index was initialized when the | 153 // for metrics purposes, and is true if the index was initialized when the |
| 148 // create operation began. | 154 // create operation began. |
| 149 int InitializeForCreate(bool had_index, SimpleEntryStat* out_entry_stat); | 155 int InitializeForCreate(bool had_index, SimpleEntryStat* out_entry_stat); |
| 150 | 156 |
| 157 // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then |
| 158 // checks its crc32. |
| 159 int ReadAndValidateStream0( |
| 160 int data_size[], |
| 161 scoped_refptr<net::GrowableIOBuffer>* stream_0_data) const; |
| 162 |
| 163 // Writes the header for an entry. |
| 164 int WriteHeader(int file_index, int stream_0_size, int stream_1_size) const; |
| 165 |
| 166 int CheckEOFRecordInternal(int index, |
| 167 const int data_size[], |
| 168 uint32 expected_crc32) const; |
| 151 void Doom() const; | 169 void Doom() const; |
| 152 | 170 |
| 153 static bool DeleteFilesForEntryHash(const base::FilePath& path, | 171 static bool DeleteFilesForEntryHash(const base::FilePath& path, |
| 154 uint64 entry_hash); | 172 uint64 entry_hash); |
| 155 | 173 |
| 156 const net::CacheType cache_type_; | 174 const net::CacheType cache_type_; |
| 157 const base::FilePath path_; | 175 const base::FilePath path_; |
| 158 const uint64 entry_hash_; | 176 const uint64 entry_hash_; |
| 159 std::string key_; | 177 std::string key_; |
| 160 | 178 |
| 161 bool have_open_files_; | 179 bool have_open_files_; |
| 162 bool initialized_; | 180 bool initialized_; |
| 163 | 181 |
| 164 base::PlatformFile files_[kSimpleEntryFileCount]; | 182 base::PlatformFile files_[kSimpleEntryFileCount]; |
| 165 }; | 183 }; |
| 166 | 184 |
| 167 } // namespace disk_cache | 185 } // namespace disk_cache |
| 168 | 186 |
| 169 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | 187 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ |
| OLD | NEW |