| 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" | |
| 15 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 16 #include "net/base/cache_type.h" | 15 #include "net/base/cache_type.h" |
| 17 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
| 18 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
| 19 #include "net/disk_cache/disk_cache.h" | 18 #include "net/disk_cache/disk_cache.h" |
| 20 #include "net/disk_cache/simple/simple_entry_format.h" | 19 #include "net/disk_cache/simple/simple_entry_format.h" |
| 21 #include "net/disk_cache/simple/simple_entry_operation.h" | 20 #include "net/disk_cache/simple/simple_entry_operation.h" |
| 22 | 21 |
| 23 namespace base { | 22 namespace base { |
| 24 class TaskRunner; | 23 class TaskRunner; |
| 25 } | 24 } |
| 26 | 25 |
| 27 namespace net { | 26 namespace net { |
| 28 class GrowableIOBuffer; | 27 class GrowableIOBuffer; |
| 29 class IOBuffer; | 28 class IOBuffer; |
| 30 } | 29 } |
| 31 | 30 |
| 32 namespace disk_cache { | 31 namespace disk_cache { |
| 33 | 32 |
| 34 class SimpleBackendImpl; | 33 class SimpleBackendImpl; |
| 35 class SimpleSynchronousEntry; | 34 class SimpleSynchronousEntry; |
| 36 class SimpleEntryStat; | 35 class SimpleEntryStat; |
| 37 struct SimpleEntryCreationResults; | 36 struct SimpleEntryCreationResults; |
| 38 | 37 |
| 39 // SimpleEntryImpl is the IO thread interface to an entry in the very simple | 38 // SimpleEntryImpl is the IO thread interface to an entry in the very simple |
| 40 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO | 39 // disk cache. It proxies for the SimpleSynchronousEntry, which performs IO |
| 41 // on the worker thread. | 40 // on the worker thread. |
| 42 class NET_EXPORT_PRIVATE SimpleEntryImpl : public Entry, | 41 class NET_EXPORT_PRIVATE SimpleEntryImpl : public Entry, |
| 43 public base::RefCounted<SimpleEntryImpl>, | 42 public base::RefCounted<SimpleEntryImpl> { |
| 44 public base::SupportsWeakPtr<SimpleEntryImpl> { | |
| 45 friend class base::RefCounted<SimpleEntryImpl>; | 43 friend class base::RefCounted<SimpleEntryImpl>; |
| 46 public: | 44 public: |
| 47 enum OperationsMode { | 45 enum OperationsMode { |
| 48 NON_OPTIMISTIC_OPERATIONS, | 46 NON_OPTIMISTIC_OPERATIONS, |
| 49 OPTIMISTIC_OPERATIONS, | 47 OPTIMISTIC_OPERATIONS, |
| 50 }; | 48 }; |
| 51 | 49 |
| 50 // The Backend provides an |ActiveEntryProxy| instance to this entry when it |
| 51 // is active, meaning it's the canonical entry for this |entry_hash_|. The |
| 52 // entry can make itself inactive by deleting its proxy. |
| 53 class ActiveEntryProxy { |
| 54 public: |
| 55 virtual ~ActiveEntryProxy() = 0; |
| 56 }; |
| 57 |
| 52 SimpleEntryImpl(net::CacheType cache_type, | 58 SimpleEntryImpl(net::CacheType cache_type, |
| 53 const base::FilePath& path, | 59 const base::FilePath& path, |
| 54 uint64 entry_hash, | 60 uint64 entry_hash, |
| 55 OperationsMode operations_mode, | 61 OperationsMode operations_mode, |
| 56 SimpleBackendImpl* backend, | 62 SimpleBackendImpl* backend, |
| 57 net::NetLog* net_log); | 63 net::NetLog* net_log); |
| 58 | 64 |
| 65 void SetActiveEntryProxy( |
| 66 scoped_ptr<ActiveEntryProxy> active_entry_proxy); |
| 67 |
| 59 // Adds another reader/writer to this entry, if possible, returning |this| to | 68 // Adds another reader/writer to this entry, if possible, returning |this| to |
| 60 // |entry|. | 69 // |entry|. |
| 61 int OpenEntry(Entry** entry, const CompletionCallback& callback); | 70 int OpenEntry(Entry** entry, const CompletionCallback& callback); |
| 62 | 71 |
| 63 // Creates this entry, if possible. Returns |this| to |entry|. | 72 // Creates this entry, if possible. Returns |this| to |entry|. |
| 64 int CreateEntry(Entry** entry, const CompletionCallback& callback); | 73 int CreateEntry(Entry** entry, const CompletionCallback& callback); |
| 65 | 74 |
| 66 // Identical to Backend::Doom() except that it accepts a CompletionCallback. | 75 // Identical to Backend::Doom() except that it accepts a CompletionCallback. |
| 67 int DoomEntry(const CompletionCallback& callback); | 76 int DoomEntry(const CompletionCallback& callback); |
| 68 | 77 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 // expect). | 152 // expect). |
| 144 void PostClientCallback(const CompletionCallback& callback, int result); | 153 void PostClientCallback(const CompletionCallback& callback, int result); |
| 145 | 154 |
| 146 // Sets entry to STATE_UNINITIALIZED. | 155 // Sets entry to STATE_UNINITIALIZED. |
| 147 void MakeUninitialized(); | 156 void MakeUninitialized(); |
| 148 | 157 |
| 149 // Return this entry to a user of the API in |out_entry|. Increments the user | 158 // Return this entry to a user of the API in |out_entry|. Increments the user |
| 150 // count. | 159 // count. |
| 151 void ReturnEntryToCaller(Entry** out_entry); | 160 void ReturnEntryToCaller(Entry** out_entry); |
| 152 | 161 |
| 153 // Ensures that |this| is no longer referenced by our |backend_|, this | |
| 154 // guarantees that this entry cannot have OpenEntry/CreateEntry called again. | |
| 155 void RemoveSelfFromBackend(); | |
| 156 | |
| 157 // An error occured, and the SimpleSynchronousEntry should have Doomed | 162 // An error occured, and the SimpleSynchronousEntry should have Doomed |
| 158 // us at this point. We need to remove |this| from the Backend and the | 163 // us at this point. We need to remove |this| from the Backend and the |
| 159 // index. | 164 // index. |
| 160 void MarkAsDoomed(); | 165 void MarkAsDoomed(); |
| 161 | 166 |
| 162 // Runs the next operation in the queue, if any and if there is no other | 167 // Runs the next operation in the queue, if any and if there is no other |
| 163 // operation running at the moment. | 168 // operation running at the moment. |
| 164 // WARNING: May delete |this|, as an operation in the queue can contain | 169 // WARNING: May delete |this|, as an operation in the queue can contain |
| 165 // the last reference. | 170 // the last reference. |
| 166 void RunNextOperationIfNeeded(); | 171 void RunNextOperationIfNeeded(); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 int offset, int buf_len, | 295 int offset, int buf_len, |
| 291 bool truncate); | 296 bool truncate); |
| 292 | 297 |
| 293 // Updates |crc32s_| and |crc32s_end_offset_| for a write of the data in | 298 // Updates |crc32s_| and |crc32s_end_offset_| for a write of the data in |
| 294 // |buffer| on |stream_index|, starting at |offset| and of length |length|. | 299 // |buffer| on |stream_index|, starting at |offset| and of length |length|. |
| 295 void AdvanceCrc(net::IOBuffer* buffer, | 300 void AdvanceCrc(net::IOBuffer* buffer, |
| 296 int offset, | 301 int offset, |
| 297 int length, | 302 int length, |
| 298 int stream_index); | 303 int stream_index); |
| 299 | 304 |
| 305 scoped_ptr<ActiveEntryProxy> active_entry_proxy_; |
| 306 |
| 300 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 307 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
| 301 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 308 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
| 302 base::ThreadChecker io_thread_checker_; | 309 base::ThreadChecker io_thread_checker_; |
| 303 | 310 |
| 304 const base::WeakPtr<SimpleBackendImpl> backend_; | 311 const base::WeakPtr<SimpleBackendImpl> backend_; |
| 305 const net::CacheType cache_type_; | 312 const net::CacheType cache_type_; |
| 306 const scoped_refptr<base::TaskRunner> worker_pool_; | 313 const scoped_refptr<base::TaskRunner> worker_pool_; |
| 307 const base::FilePath path_; | 314 const base::FilePath path_; |
| 308 const uint64 entry_hash_; | 315 const uint64 entry_hash_; |
| 309 const bool use_optimistic_operations_; | 316 const bool use_optimistic_operations_; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 // 1 on disk, to reduce the number of file descriptors and save disk space. | 370 // 1 on disk, to reduce the number of file descriptors and save disk space. |
| 364 // This strategy allows stream 1 to change size easily. Since stream 0 is only | 371 // This strategy allows stream 1 to change size easily. Since stream 0 is only |
| 365 // used to write HTTP headers, the memory consumption of keeping it in memory | 372 // used to write HTTP headers, the memory consumption of keeping it in memory |
| 366 // is acceptable. | 373 // is acceptable. |
| 367 scoped_refptr<net::GrowableIOBuffer> stream_0_data_; | 374 scoped_refptr<net::GrowableIOBuffer> stream_0_data_; |
| 368 }; | 375 }; |
| 369 | 376 |
| 370 } // namespace disk_cache | 377 } // namespace disk_cache |
| 371 | 378 |
| 372 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ | 379 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
| OLD | NEW |