| 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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> |
| 10 #include <queue> | 11 #include <queue> |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 17 #include "net/base/cache_type.h" | 17 #include "net/base/cache_type.h" |
| 18 #include "net/base/net_export.h" | 18 #include "net/base/net_export.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 #include "net/log/net_log.h" | 22 #include "net/log/net_log.h" |
| 23 | 23 |
| 24 namespace base { | 24 namespace base { |
| 25 class TaskRunner; | 25 class TaskRunner; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 SimpleEntryImpl(net::CacheType cache_type, | 60 SimpleEntryImpl(net::CacheType cache_type, |
| 61 const base::FilePath& path, | 61 const base::FilePath& path, |
| 62 uint64_t entry_hash, | 62 uint64_t entry_hash, |
| 63 OperationsMode operations_mode, | 63 OperationsMode operations_mode, |
| 64 SimpleBackendImpl* backend, | 64 SimpleBackendImpl* backend, |
| 65 net::NetLog* net_log); | 65 net::NetLog* net_log); |
| 66 | 66 |
| 67 void SetActiveEntryProxy( | 67 void SetActiveEntryProxy( |
| 68 scoped_ptr<ActiveEntryProxy> active_entry_proxy); | 68 std::unique_ptr<ActiveEntryProxy> active_entry_proxy); |
| 69 | 69 |
| 70 // Adds another reader/writer to this entry, if possible, returning |this| to | 70 // Adds another reader/writer to this entry, if possible, returning |this| to |
| 71 // |entry|. | 71 // |entry|. |
| 72 int OpenEntry(Entry** entry, const CompletionCallback& callback); | 72 int OpenEntry(Entry** entry, const CompletionCallback& callback); |
| 73 | 73 |
| 74 // Creates this entry, if possible. Returns |this| to |entry|. | 74 // Creates this entry, if possible. Returns |this| to |entry|. |
| 75 int CreateEntry(Entry** entry, const CompletionCallback& callback); | 75 int CreateEntry(Entry** entry, const CompletionCallback& callback); |
| 76 | 76 |
| 77 // Identical to Backend::Doom() except that it accepts a CompletionCallback. | 77 // Identical to Backend::Doom() except that it accepts a CompletionCallback. |
| 78 int DoomEntry(const CompletionCallback& callback); | 78 int DoomEntry(const CompletionCallback& callback); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 | 212 |
| 213 void DoomEntryInternal(const CompletionCallback& callback); | 213 void DoomEntryInternal(const CompletionCallback& callback); |
| 214 | 214 |
| 215 // Called after a SimpleSynchronousEntry has completed CreateEntry() or | 215 // Called after a SimpleSynchronousEntry has completed CreateEntry() or |
| 216 // OpenEntry(). If |in_sync_entry| is non-NULL, creation is successful and we | 216 // OpenEntry(). If |in_sync_entry| is non-NULL, creation is successful and we |
| 217 // can return |this| SimpleEntryImpl to |*out_entry|. Runs | 217 // can return |this| SimpleEntryImpl to |*out_entry|. Runs |
| 218 // |completion_callback|. | 218 // |completion_callback|. |
| 219 void CreationOperationComplete( | 219 void CreationOperationComplete( |
| 220 const CompletionCallback& completion_callback, | 220 const CompletionCallback& completion_callback, |
| 221 const base::TimeTicks& start_time, | 221 const base::TimeTicks& start_time, |
| 222 scoped_ptr<SimpleEntryCreationResults> in_results, | 222 std::unique_ptr<SimpleEntryCreationResults> in_results, |
| 223 Entry** out_entry, | 223 Entry** out_entry, |
| 224 net::NetLog::EventType end_event_type); | 224 net::NetLog::EventType end_event_type); |
| 225 | 225 |
| 226 // Called after we've closed and written the EOF record to our entry. Until | 226 // Called after we've closed and written the EOF record to our entry. Until |
| 227 // this point it hasn't been safe to OpenEntry() the same entry, but from this | 227 // this point it hasn't been safe to OpenEntry() the same entry, but from this |
| 228 // point it is. | 228 // point it is. |
| 229 void CloseOperationComplete(); | 229 void CloseOperationComplete(); |
| 230 | 230 |
| 231 // Internal utility method used by other completion methods. Calls | 231 // Internal utility method used by other completion methods. Calls |
| 232 // |completion_callback| after updating state and dooming on errors. | 232 // |completion_callback| after updating state and dooming on errors. |
| 233 void EntryOperationComplete(const CompletionCallback& completion_callback, | 233 void EntryOperationComplete(const CompletionCallback& completion_callback, |
| 234 const SimpleEntryStat& entry_stat, | 234 const SimpleEntryStat& entry_stat, |
| 235 scoped_ptr<int> result); | 235 std::unique_ptr<int> result); |
| 236 | 236 |
| 237 // Called after an asynchronous read. Updates |crc32s_| if possible. | 237 // Called after an asynchronous read. Updates |crc32s_| if possible. |
| 238 void ReadOperationComplete(int stream_index, | 238 void ReadOperationComplete(int stream_index, |
| 239 int offset, | 239 int offset, |
| 240 const CompletionCallback& completion_callback, | 240 const CompletionCallback& completion_callback, |
| 241 scoped_ptr<uint32_t> read_crc32, | 241 std::unique_ptr<uint32_t> read_crc32, |
| 242 scoped_ptr<SimpleEntryStat> entry_stat, | 242 std::unique_ptr<SimpleEntryStat> entry_stat, |
| 243 scoped_ptr<int> result); | 243 std::unique_ptr<int> result); |
| 244 | 244 |
| 245 // Called after an asynchronous write completes. | 245 // Called after an asynchronous write completes. |
| 246 void WriteOperationComplete(int stream_index, | 246 void WriteOperationComplete(int stream_index, |
| 247 const CompletionCallback& completion_callback, | 247 const CompletionCallback& completion_callback, |
| 248 scoped_ptr<SimpleEntryStat> entry_stat, | 248 std::unique_ptr<SimpleEntryStat> entry_stat, |
| 249 scoped_ptr<int> result); | 249 std::unique_ptr<int> result); |
| 250 | 250 |
| 251 void ReadSparseOperationComplete( | 251 void ReadSparseOperationComplete( |
| 252 const CompletionCallback& completion_callback, | 252 const CompletionCallback& completion_callback, |
| 253 scoped_ptr<base::Time> last_used, | 253 std::unique_ptr<base::Time> last_used, |
| 254 scoped_ptr<int> result); | 254 std::unique_ptr<int> result); |
| 255 | 255 |
| 256 void WriteSparseOperationComplete( | 256 void WriteSparseOperationComplete( |
| 257 const CompletionCallback& completion_callback, | 257 const CompletionCallback& completion_callback, |
| 258 scoped_ptr<SimpleEntryStat> entry_stat, | 258 std::unique_ptr<SimpleEntryStat> entry_stat, |
| 259 scoped_ptr<int> result); | 259 std::unique_ptr<int> result); |
| 260 | 260 |
| 261 void GetAvailableRangeOperationComplete( | 261 void GetAvailableRangeOperationComplete( |
| 262 const CompletionCallback& completion_callback, | 262 const CompletionCallback& completion_callback, |
| 263 scoped_ptr<int> result); | 263 std::unique_ptr<int> result); |
| 264 | 264 |
| 265 // Called after an asynchronous doom completes. | 265 // Called after an asynchronous doom completes. |
| 266 void DoomOperationComplete(const CompletionCallback& callback, | 266 void DoomOperationComplete(const CompletionCallback& callback, |
| 267 State state_to_restore, | 267 State state_to_restore, |
| 268 int result); | 268 int result); |
| 269 | 269 |
| 270 // Called after validating the checksums on an entry. Passes through the | 270 // Called after validating the checksums on an entry. Passes through the |
| 271 // original result if successful, propagates the error if the checksum does | 271 // original result if successful, propagates the error if the checksum does |
| 272 // not validate. | 272 // not validate. |
| 273 void ChecksumOperationComplete( | 273 void ChecksumOperationComplete(int stream_index, |
| 274 int stream_index, | 274 int orig_result, |
| 275 int orig_result, | 275 const CompletionCallback& completion_callback, |
| 276 const CompletionCallback& completion_callback, | 276 std::unique_ptr<int> result); |
| 277 scoped_ptr<int> result); | |
| 278 | 277 |
| 279 // Called after completion of asynchronous IO and receiving file metadata for | 278 // Called after completion of asynchronous IO and receiving file metadata for |
| 280 // the entry in |entry_stat|. Updates the metadata in the entry and in the | 279 // the entry in |entry_stat|. Updates the metadata in the entry and in the |
| 281 // index to make them available on next IO operations. | 280 // index to make them available on next IO operations. |
| 282 void UpdateDataFromEntryStat(const SimpleEntryStat& entry_stat); | 281 void UpdateDataFromEntryStat(const SimpleEntryStat& entry_stat); |
| 283 | 282 |
| 284 int64_t GetDiskUsage() const; | 283 int64_t GetDiskUsage() const; |
| 285 | 284 |
| 286 // Used to report histograms. | 285 // Used to report histograms. |
| 287 void RecordReadIsParallelizable(const SimpleEntryOperation& operation) const; | 286 void RecordReadIsParallelizable(const SimpleEntryOperation& operation) const; |
| 288 void RecordWriteDependencyType(const SimpleEntryOperation& operation) const; | 287 void RecordWriteDependencyType(const SimpleEntryOperation& operation) const; |
| 289 | 288 |
| 290 // Reads from the stream 0 data kept in memory. | 289 // Reads from the stream 0 data kept in memory. |
| 291 int ReadStream0Data(net::IOBuffer* buf, int offset, int buf_len); | 290 int ReadStream0Data(net::IOBuffer* buf, int offset, int buf_len); |
| 292 | 291 |
| 293 // Copies data from |buf| to the internal in-memory buffer for stream 0. If | 292 // Copies data from |buf| to the internal in-memory buffer for stream 0. If |
| 294 // |truncate| is set to true, the target buffer will be truncated at |offset| | 293 // |truncate| is set to true, the target buffer will be truncated at |offset| |
| 295 // + |buf_len| before being written. | 294 // + |buf_len| before being written. |
| 296 int SetStream0Data(net::IOBuffer* buf, | 295 int SetStream0Data(net::IOBuffer* buf, |
| 297 int offset, int buf_len, | 296 int offset, int buf_len, |
| 298 bool truncate); | 297 bool truncate); |
| 299 | 298 |
| 300 // Updates |crc32s_| and |crc32s_end_offset_| for a write of the data in | 299 // Updates |crc32s_| and |crc32s_end_offset_| for a write of the data in |
| 301 // |buffer| on |stream_index|, starting at |offset| and of length |length|. | 300 // |buffer| on |stream_index|, starting at |offset| and of length |length|. |
| 302 void AdvanceCrc(net::IOBuffer* buffer, | 301 void AdvanceCrc(net::IOBuffer* buffer, |
| 303 int offset, | 302 int offset, |
| 304 int length, | 303 int length, |
| 305 int stream_index); | 304 int stream_index); |
| 306 | 305 |
| 307 scoped_ptr<ActiveEntryProxy> active_entry_proxy_; | 306 std::unique_ptr<ActiveEntryProxy> active_entry_proxy_; |
| 308 | 307 |
| 309 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 308 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
| 310 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 309 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
| 311 base::ThreadChecker io_thread_checker_; | 310 base::ThreadChecker io_thread_checker_; |
| 312 | 311 |
| 313 const base::WeakPtr<SimpleBackendImpl> backend_; | 312 const base::WeakPtr<SimpleBackendImpl> backend_; |
| 314 const net::CacheType cache_type_; | 313 const net::CacheType cache_type_; |
| 315 const scoped_refptr<base::TaskRunner> worker_pool_; | 314 const scoped_refptr<base::TaskRunner> worker_pool_; |
| 316 const base::FilePath path_; | 315 const base::FilePath path_; |
| 317 const uint64_t entry_hash_; | 316 const uint64_t entry_hash_; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 // is false (i.e. when an operation is not pending on the worker pool). When | 358 // is false (i.e. when an operation is not pending on the worker pool). When |
| 360 // an operation is being executed no one owns the synchronous entry. Therefore | 359 // an operation is being executed no one owns the synchronous entry. Therefore |
| 361 // SimpleEntryImpl should not be deleted while an operation is running as that | 360 // SimpleEntryImpl should not be deleted while an operation is running as that |
| 362 // would leak the SimpleSynchronousEntry. | 361 // would leak the SimpleSynchronousEntry. |
| 363 SimpleSynchronousEntry* synchronous_entry_; | 362 SimpleSynchronousEntry* synchronous_entry_; |
| 364 | 363 |
| 365 std::queue<SimpleEntryOperation> pending_operations_; | 364 std::queue<SimpleEntryOperation> pending_operations_; |
| 366 | 365 |
| 367 net::BoundNetLog net_log_; | 366 net::BoundNetLog net_log_; |
| 368 | 367 |
| 369 scoped_ptr<SimpleEntryOperation> executing_operation_; | 368 std::unique_ptr<SimpleEntryOperation> executing_operation_; |
| 370 | 369 |
| 371 // Unlike other streams, stream 0 data is read from the disk when the entry is | 370 // Unlike other streams, stream 0 data is read from the disk when the entry is |
| 372 // opened, and then kept in memory. All read/write operations on stream 0 | 371 // opened, and then kept in memory. All read/write operations on stream 0 |
| 373 // affect the |stream_0_data_| buffer. When the entry is closed, | 372 // affect the |stream_0_data_| buffer. When the entry is closed, |
| 374 // |stream_0_data_| is written to the disk. | 373 // |stream_0_data_| is written to the disk. |
| 375 // Stream 0 is kept in memory because it is stored in the same file as stream | 374 // Stream 0 is kept in memory because it is stored in the same file as stream |
| 376 // 1 on disk, to reduce the number of file descriptors and save disk space. | 375 // 1 on disk, to reduce the number of file descriptors and save disk space. |
| 377 // This strategy allows stream 1 to change size easily. Since stream 0 is only | 376 // This strategy allows stream 1 to change size easily. Since stream 0 is only |
| 378 // used to write HTTP headers, the memory consumption of keeping it in memory | 377 // used to write HTTP headers, the memory consumption of keeping it in memory |
| 379 // is acceptable. | 378 // is acceptable. |
| 380 scoped_refptr<net::GrowableIOBuffer> stream_0_data_; | 379 scoped_refptr<net::GrowableIOBuffer> stream_0_data_; |
| 381 }; | 380 }; |
| 382 | 381 |
| 383 } // namespace disk_cache | 382 } // namespace disk_cache |
| 384 | 383 |
| 385 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ | 384 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
| OLD | NEW |