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_ENTRY_IMPL_H_ | 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
| 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ | 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "net/base/cache_type.h" | 16 #include "net/base/cache_type.h" |
| 17 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
| 18 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
| 19 #include "net/disk_cache/disk_cache.h" | 19 #include "net/disk_cache/disk_cache.h" |
| 20 #include "net/disk_cache/simple/simple_entry_format.h" | 20 #include "net/disk_cache/simple/simple_entry_format.h" |
| 21 #include "net/disk_cache/simple/simple_entry_operation.h" | 21 #include "net/disk_cache/simple/simple_entry_operation.h" |
| 22 | 22 |
| 23 namespace base { | 23 namespace base { |
| 24 class TaskRunner; | 24 class TaskRunner; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace net { | 27 namespace net { |
| 28 class GrowableIOBuffer; | |
| 28 class IOBuffer; | 29 class IOBuffer; |
| 29 } | 30 } |
| 30 | 31 |
| 31 namespace disk_cache { | 32 namespace disk_cache { |
| 32 | 33 |
| 33 class SimpleBackendImpl; | 34 class SimpleBackendImpl; |
| 34 class SimpleSynchronousEntry; | 35 class SimpleSynchronousEntry; |
| 35 struct SimpleEntryStat; | 36 struct SimpleEntryStat; |
|
pasko
2013/09/18 16:53:56
the compiler on Windows is complaining that you sh
clamy
2013/09/18 17:20:46
Done. Actually most bots complain. I don't underst
| |
| 36 struct SimpleEntryCreationResults; | 37 struct SimpleEntryCreationResults; |
| 37 | 38 |
| 38 // SimpleEntryImpl is the IO thread interface to an entry in the very simple | 39 // SimpleEntryImpl is the IO thread interface to an entry in the very simple |
| 39 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO | 40 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO |
| 40 // on the worker thread. | 41 // on the worker thread. |
| 41 class NET_EXPORT_PRIVATE SimpleEntryImpl : public Entry, | 42 class NET_EXPORT_PRIVATE SimpleEntryImpl : public Entry, |
| 42 public base::RefCounted<SimpleEntryImpl>, | 43 public base::RefCounted<SimpleEntryImpl>, |
| 43 public base::SupportsWeakPtr<SimpleEntryImpl> { | 44 public base::SupportsWeakPtr<SimpleEntryImpl> { |
| 44 friend class base::RefCounted<SimpleEntryImpl>; | 45 friend class base::RefCounted<SimpleEntryImpl>; |
| 45 public: | 46 public: |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 void EntryOperationComplete(int stream_index, | 211 void EntryOperationComplete(int stream_index, |
| 211 const CompletionCallback& completion_callback, | 212 const CompletionCallback& completion_callback, |
| 212 const SimpleEntryStat& entry_stat, | 213 const SimpleEntryStat& entry_stat, |
| 213 scoped_ptr<int> result); | 214 scoped_ptr<int> result); |
| 214 | 215 |
| 215 // Called after an asynchronous read. Updates |crc32s_| if possible. | 216 // Called after an asynchronous read. Updates |crc32s_| if possible. |
| 216 void ReadOperationComplete(int stream_index, | 217 void ReadOperationComplete(int stream_index, |
| 217 int offset, | 218 int offset, |
| 218 const CompletionCallback& completion_callback, | 219 const CompletionCallback& completion_callback, |
| 219 scoped_ptr<uint32> read_crc32, | 220 scoped_ptr<uint32> read_crc32, |
| 220 scoped_ptr<base::Time> last_used, | 221 scoped_ptr<SimpleEntryStat> entry_stat, |
| 221 scoped_ptr<int> result); | 222 scoped_ptr<int> result); |
| 222 | 223 |
| 223 // Called after an asynchronous write completes. | 224 // Called after an asynchronous write completes. |
| 224 void WriteOperationComplete(int stream_index, | 225 void WriteOperationComplete(int stream_index, |
| 225 const CompletionCallback& completion_callback, | 226 const CompletionCallback& completion_callback, |
| 226 scoped_ptr<SimpleEntryStat> entry_stat, | 227 scoped_ptr<SimpleEntryStat> entry_stat, |
| 227 scoped_ptr<int> result); | 228 scoped_ptr<int> result); |
| 228 | 229 |
| 229 // Called after an asynchronous doom completes. | 230 // Called after an asynchronous doom completes. |
| 230 void DoomOperationComplete(const CompletionCallback& callback, | 231 void DoomOperationComplete(const CompletionCallback& callback, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 244 // the entry in |entry_stat|. Updates the metadata in the entry and in the | 245 // the entry in |entry_stat|. Updates the metadata in the entry and in the |
| 245 // index to make them available on next IO operations. | 246 // index to make them available on next IO operations. |
| 246 void UpdateDataFromEntryStat(const SimpleEntryStat& entry_stat); | 247 void UpdateDataFromEntryStat(const SimpleEntryStat& entry_stat); |
| 247 | 248 |
| 248 int64 GetDiskUsage() const; | 249 int64 GetDiskUsage() const; |
| 249 | 250 |
| 250 // Used to report histograms. | 251 // Used to report histograms. |
| 251 void RecordReadIsParallelizable(const SimpleEntryOperation& operation) const; | 252 void RecordReadIsParallelizable(const SimpleEntryOperation& operation) const; |
| 252 void RecordWriteDependencyType(const SimpleEntryOperation& operation) const; | 253 void RecordWriteDependencyType(const SimpleEntryOperation& operation) const; |
| 253 | 254 |
| 255 // Reads from the stream 0 data kept in memory. | |
| 256 int ReadStream0Data(net::IOBuffer* buf, int offset, int buf_len); | |
| 257 | |
| 258 // Copies data from |buf| to the internal in-memory buffer for stream 0. If | |
| 259 // |truncate| is set to true, the target buffer will be truncated at |offset| | |
| 260 // + |buf_len| before being written. | |
| 261 int SetStream0Data(net::IOBuffer* buf, | |
| 262 int offset, int buf_len, | |
| 263 bool truncate); | |
| 264 | |
| 265 // Updates |crc32s_| and |crc32s_end_offset_| for a write of the data in | |
| 266 // |buffer| on |stream_index|, starting at |offset| and of length |length|. | |
| 267 void AdvanceCrc(net::IOBuffer* buffer, | |
|
pasko
2013/09/18 16:53:56
we are not updating the buffer here, should be a c
clamy
2013/09/18 17:20:46
No we use net::IOBuffer::data() which is not const
| |
| 268 int offset, | |
| 269 int length, | |
| 270 int stream_index); | |
| 271 | |
| 254 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 272 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
| 255 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 273 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
| 256 base::ThreadChecker io_thread_checker_; | 274 base::ThreadChecker io_thread_checker_; |
| 257 | 275 |
| 258 const base::WeakPtr<SimpleBackendImpl> backend_; | 276 const base::WeakPtr<SimpleBackendImpl> backend_; |
| 259 const net::CacheType cache_type_; | 277 const net::CacheType cache_type_; |
| 260 const scoped_refptr<base::TaskRunner> worker_pool_; | 278 const scoped_refptr<base::TaskRunner> worker_pool_; |
| 261 const base::FilePath path_; | 279 const base::FilePath path_; |
| 262 const uint64 entry_hash_; | 280 const uint64 entry_hash_; |
| 263 const bool use_optimistic_operations_; | 281 const bool use_optimistic_operations_; |
| 264 std::string key_; | 282 std::string key_; |
| 265 | 283 |
| 266 // |last_used_|, |last_modified_| and |data_size_| are copied from the | 284 // |last_used_|, |last_modified_| and |data_size_| are copied from the |
| 267 // synchronous entry at the completion of each item of asynchronous IO. | 285 // synchronous entry at the completion of each item of asynchronous IO. |
| 268 // TODO(clamy): Unify last_used_ with data in the index. | 286 // TODO(clamy): Unify last_used_ with data in the index. |
| 269 base::Time last_used_; | 287 base::Time last_used_; |
| 270 base::Time last_modified_; | 288 base::Time last_modified_; |
| 271 int32 data_size_[kSimpleEntryFileCount]; | 289 int32 data_size_[kSimpleEntryStreamCount]; |
| 272 | 290 |
| 273 // Number of times this object has been returned from Backend::OpenEntry() and | 291 // Number of times this object has been returned from Backend::OpenEntry() and |
| 274 // Backend::CreateEntry() without subsequent Entry::Close() calls. Used to | 292 // Backend::CreateEntry() without subsequent Entry::Close() calls. Used to |
| 275 // notify the backend when this entry not used by any callers. | 293 // notify the backend when this entry not used by any callers. |
| 276 int open_count_; | 294 int open_count_; |
| 277 | 295 |
| 278 bool doomed_; | 296 bool doomed_; |
| 279 | 297 |
| 280 State state_; | 298 State state_; |
| 281 | 299 |
| 282 // When possible, we compute a crc32, for the data in each entry as we read or | 300 // When possible, we compute a crc32, for the data in each entry as we read or |
| 283 // write. For each stream, |crc32s_[index]| is the crc32 of that stream from | 301 // write. For each stream, |crc32s_[index]| is the crc32 of that stream from |
| 284 // [0 .. |crc32s_end_offset_|). If |crc32s_end_offset_[index] == 0| then the | 302 // [0 .. |crc32s_end_offset_|). If |crc32s_end_offset_[index] == 0| then the |
| 285 // value of |crc32s_[index]| is undefined. | 303 // value of |crc32s_[index]| is undefined. |
| 286 int32 crc32s_end_offset_[kSimpleEntryFileCount]; | 304 int32 crc32s_end_offset_[kSimpleEntryStreamCount]; |
| 287 uint32 crc32s_[kSimpleEntryFileCount]; | 305 uint32 crc32s_[kSimpleEntryStreamCount]; |
| 288 | 306 |
| 289 // If |have_written_[index]| is true, we have written to the stream |index|. | 307 // If |have_written_[index]| is true, we have written to the file that |
| 290 bool have_written_[kSimpleEntryFileCount]; | 308 // contains stream |index|. |
| 309 bool have_written_[kSimpleEntryStreamCount]; | |
| 291 | 310 |
| 292 // Reflects how much CRC checking has been done with the entry. This state is | 311 // Reflects how much CRC checking has been done with the entry. This state is |
| 293 // reported on closing each entry stream. | 312 // reported on closing each entry stream. |
| 294 CheckCrcResult crc_check_state_[kSimpleEntryFileCount]; | 313 CheckCrcResult crc_check_state_[kSimpleEntryStreamCount]; |
| 295 | 314 |
| 296 // The |synchronous_entry_| is the worker thread object that performs IO on | 315 // The |synchronous_entry_| is the worker thread object that performs IO on |
| 297 // entries. It's owned by this SimpleEntryImpl whenever |executing_operation_| | 316 // entries. It's owned by this SimpleEntryImpl whenever |executing_operation_| |
| 298 // is false (i.e. when an operation is not pending on the worker pool). When | 317 // is false (i.e. when an operation is not pending on the worker pool). When |
| 299 // an operation is being executed no one owns the synchronous entry. Therefore | 318 // an operation is being executed no one owns the synchronous entry. Therefore |
| 300 // SimpleEntryImpl should not be deleted while an operation is running as that | 319 // SimpleEntryImpl should not be deleted while an operation is running as that |
| 301 // would leak the SimpleSynchronousEntry. | 320 // would leak the SimpleSynchronousEntry. |
| 302 SimpleSynchronousEntry* synchronous_entry_; | 321 SimpleSynchronousEntry* synchronous_entry_; |
| 303 | 322 |
| 304 std::queue<SimpleEntryOperation> pending_operations_; | 323 std::queue<SimpleEntryOperation> pending_operations_; |
| 305 | 324 |
| 306 net::BoundNetLog net_log_; | 325 net::BoundNetLog net_log_; |
| 307 | 326 |
| 308 scoped_ptr<SimpleEntryOperation> executing_operation_; | 327 scoped_ptr<SimpleEntryOperation> executing_operation_; |
| 328 | |
| 329 // Unlike other streams, stream 0 data is read from the disk when the entry is | |
| 330 // opened, and then kept in memory. All read/write operations on stream 0 | |
| 331 // affect the |stream_0_data_| buffer. When the entry is closed, | |
| 332 // |stream_0_data_| is written to the disk. | |
| 333 // Stream 0 is kept in memory because it is stored in the same file as stream | |
| 334 // 1 on disk, to reduce the number of file descriptors and save disk space. | |
| 335 // This strategy allows stream 1 to change size easily. Since stream 0 is only | |
| 336 // used to write HTTP headers, the memory consumption of keeping it in memory | |
| 337 // is acceptable. | |
| 338 scoped_refptr<net::GrowableIOBuffer> stream_0_data_; | |
| 309 }; | 339 }; |
| 310 | 340 |
| 311 } // namespace disk_cache | 341 } // namespace disk_cache |
| 312 | 342 |
| 313 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ | 343 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
| OLD | NEW |