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> |
| 9 |
8 #include <queue> | 10 #include <queue> |
9 #include <string> | 11 #include <string> |
10 | 12 |
11 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
14 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
15 #include "net/base/cache_type.h" | 17 #include "net/base/cache_type.h" |
16 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
17 #include "net/disk_cache/disk_cache.h" | 19 #include "net/disk_cache/disk_cache.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 // The Backend provides an |ActiveEntryProxy| instance to this entry when it | 52 // 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 | 53 // is active, meaning it's the canonical entry for this |entry_hash_|. The |
52 // entry can make itself inactive by deleting its proxy. | 54 // entry can make itself inactive by deleting its proxy. |
53 class ActiveEntryProxy { | 55 class ActiveEntryProxy { |
54 public: | 56 public: |
55 virtual ~ActiveEntryProxy() = 0; | 57 virtual ~ActiveEntryProxy() = 0; |
56 }; | 58 }; |
57 | 59 |
58 SimpleEntryImpl(net::CacheType cache_type, | 60 SimpleEntryImpl(net::CacheType cache_type, |
59 const base::FilePath& path, | 61 const base::FilePath& path, |
60 uint64 entry_hash, | 62 uint64_t entry_hash, |
61 OperationsMode operations_mode, | 63 OperationsMode operations_mode, |
62 SimpleBackendImpl* backend, | 64 SimpleBackendImpl* backend, |
63 net::NetLog* net_log); | 65 net::NetLog* net_log); |
64 | 66 |
65 void SetActiveEntryProxy( | 67 void SetActiveEntryProxy( |
66 scoped_ptr<ActiveEntryProxy> active_entry_proxy); | 68 scoped_ptr<ActiveEntryProxy> active_entry_proxy); |
67 | 69 |
68 // 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 |
69 // |entry|. | 71 // |entry|. |
70 int OpenEntry(Entry** entry, const CompletionCallback& callback); | 72 int OpenEntry(Entry** entry, const CompletionCallback& callback); |
71 | 73 |
72 // Creates this entry, if possible. Returns |this| to |entry|. | 74 // Creates this entry, if possible. Returns |this| to |entry|. |
73 int CreateEntry(Entry** entry, const CompletionCallback& callback); | 75 int CreateEntry(Entry** entry, const CompletionCallback& callback); |
74 | 76 |
75 // Identical to Backend::Doom() except that it accepts a CompletionCallback. | 77 // Identical to Backend::Doom() except that it accepts a CompletionCallback. |
76 int DoomEntry(const CompletionCallback& callback); | 78 int DoomEntry(const CompletionCallback& callback); |
77 | 79 |
78 const std::string& key() const { return key_; } | 80 const std::string& key() const { return key_; } |
79 uint64 entry_hash() const { return entry_hash_; } | 81 uint64_t entry_hash() const { return entry_hash_; } |
80 void SetKey(const std::string& key); | 82 void SetKey(const std::string& key); |
81 | 83 |
82 // From Entry: | 84 // From Entry: |
83 void Doom() override; | 85 void Doom() override; |
84 void Close() override; | 86 void Close() override; |
85 std::string GetKey() const override; | 87 std::string GetKey() const override; |
86 base::Time GetLastUsed() const override; | 88 base::Time GetLastUsed() const override; |
87 base::Time GetLastModified() const override; | 89 base::Time GetLastModified() const override; |
88 int32 GetDataSize(int index) const override; | 90 int32_t GetDataSize(int index) const override; |
89 int ReadData(int stream_index, | 91 int ReadData(int stream_index, |
90 int offset, | 92 int offset, |
91 net::IOBuffer* buf, | 93 net::IOBuffer* buf, |
92 int buf_len, | 94 int buf_len, |
93 const CompletionCallback& callback) override; | 95 const CompletionCallback& callback) override; |
94 int WriteData(int stream_index, | 96 int WriteData(int stream_index, |
95 int offset, | 97 int offset, |
96 net::IOBuffer* buf, | 98 net::IOBuffer* buf, |
97 int buf_len, | 99 int buf_len, |
98 const CompletionCallback& callback, | 100 const CompletionCallback& callback, |
99 bool truncate) override; | 101 bool truncate) override; |
100 int ReadSparseData(int64 offset, | 102 int ReadSparseData(int64_t offset, |
101 net::IOBuffer* buf, | 103 net::IOBuffer* buf, |
102 int buf_len, | 104 int buf_len, |
103 const CompletionCallback& callback) override; | 105 const CompletionCallback& callback) override; |
104 int WriteSparseData(int64 offset, | 106 int WriteSparseData(int64_t offset, |
105 net::IOBuffer* buf, | 107 net::IOBuffer* buf, |
106 int buf_len, | 108 int buf_len, |
107 const CompletionCallback& callback) override; | 109 const CompletionCallback& callback) override; |
108 int GetAvailableRange(int64 offset, | 110 int GetAvailableRange(int64_t offset, |
109 int len, | 111 int len, |
110 int64* start, | 112 int64_t* start, |
111 const CompletionCallback& callback) override; | 113 const CompletionCallback& callback) override; |
112 bool CouldBeSparse() const override; | 114 bool CouldBeSparse() const override; |
113 void CancelSparseIO() override; | 115 void CancelSparseIO() override; |
114 int ReadyForSparseIO(const CompletionCallback& callback) override; | 116 int ReadyForSparseIO(const CompletionCallback& callback) override; |
115 | 117 |
116 private: | 118 private: |
117 class ScopedOperationRunner; | 119 class ScopedOperationRunner; |
118 friend class ScopedOperationRunner; | 120 friend class ScopedOperationRunner; |
119 | 121 |
120 enum State { | 122 enum State { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 int buf_len, | 188 int buf_len, |
187 const CompletionCallback& callback); | 189 const CompletionCallback& callback); |
188 | 190 |
189 void WriteDataInternal(int index, | 191 void WriteDataInternal(int index, |
190 int offset, | 192 int offset, |
191 net::IOBuffer* buf, | 193 net::IOBuffer* buf, |
192 int buf_len, | 194 int buf_len, |
193 const CompletionCallback& callback, | 195 const CompletionCallback& callback, |
194 bool truncate); | 196 bool truncate); |
195 | 197 |
196 void ReadSparseDataInternal(int64 sparse_offset, | 198 void ReadSparseDataInternal(int64_t sparse_offset, |
197 net::IOBuffer* buf, | 199 net::IOBuffer* buf, |
198 int buf_len, | 200 int buf_len, |
199 const CompletionCallback& callback); | 201 const CompletionCallback& callback); |
200 | 202 |
201 void WriteSparseDataInternal(int64 sparse_offset, | 203 void WriteSparseDataInternal(int64_t sparse_offset, |
202 net::IOBuffer* buf, | 204 net::IOBuffer* buf, |
203 int buf_len, | 205 int buf_len, |
204 const CompletionCallback& callback); | 206 const CompletionCallback& callback); |
205 | 207 |
206 void GetAvailableRangeInternal(int64 sparse_offset, | 208 void GetAvailableRangeInternal(int64_t sparse_offset, |
207 int len, | 209 int len, |
208 int64* out_start, | 210 int64_t* out_start, |
209 const CompletionCallback& callback); | 211 const CompletionCallback& callback); |
210 | 212 |
211 void DoomEntryInternal(const CompletionCallback& callback); | 213 void DoomEntryInternal(const CompletionCallback& callback); |
212 | 214 |
213 // Called after a SimpleSynchronousEntry has completed CreateEntry() or | 215 // Called after a SimpleSynchronousEntry has completed CreateEntry() or |
214 // 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 |
215 // can return |this| SimpleEntryImpl to |*out_entry|. Runs | 217 // can return |this| SimpleEntryImpl to |*out_entry|. Runs |
216 // |completion_callback|. | 218 // |completion_callback|. |
217 void CreationOperationComplete( | 219 void CreationOperationComplete( |
218 const CompletionCallback& completion_callback, | 220 const CompletionCallback& completion_callback, |
(...skipping 10 matching lines...) Expand all Loading... |
229 // Internal utility method used by other completion methods. Calls | 231 // Internal utility method used by other completion methods. Calls |
230 // |completion_callback| after updating state and dooming on errors. | 232 // |completion_callback| after updating state and dooming on errors. |
231 void EntryOperationComplete(const CompletionCallback& completion_callback, | 233 void EntryOperationComplete(const CompletionCallback& completion_callback, |
232 const SimpleEntryStat& entry_stat, | 234 const SimpleEntryStat& entry_stat, |
233 scoped_ptr<int> result); | 235 scoped_ptr<int> result); |
234 | 236 |
235 // Called after an asynchronous read. Updates |crc32s_| if possible. | 237 // Called after an asynchronous read. Updates |crc32s_| if possible. |
236 void ReadOperationComplete(int stream_index, | 238 void ReadOperationComplete(int stream_index, |
237 int offset, | 239 int offset, |
238 const CompletionCallback& completion_callback, | 240 const CompletionCallback& completion_callback, |
239 scoped_ptr<uint32> read_crc32, | 241 scoped_ptr<uint32_t> read_crc32, |
240 scoped_ptr<SimpleEntryStat> entry_stat, | 242 scoped_ptr<SimpleEntryStat> entry_stat, |
241 scoped_ptr<int> result); | 243 scoped_ptr<int> result); |
242 | 244 |
243 // Called after an asynchronous write completes. | 245 // Called after an asynchronous write completes. |
244 void WriteOperationComplete(int stream_index, | 246 void WriteOperationComplete(int stream_index, |
245 const CompletionCallback& completion_callback, | 247 const CompletionCallback& completion_callback, |
246 scoped_ptr<SimpleEntryStat> entry_stat, | 248 scoped_ptr<SimpleEntryStat> entry_stat, |
247 scoped_ptr<int> result); | 249 scoped_ptr<int> result); |
248 | 250 |
249 void ReadSparseOperationComplete( | 251 void ReadSparseOperationComplete( |
(...skipping 22 matching lines...) Expand all Loading... |
272 int stream_index, | 274 int stream_index, |
273 int orig_result, | 275 int orig_result, |
274 const CompletionCallback& completion_callback, | 276 const CompletionCallback& completion_callback, |
275 scoped_ptr<int> result); | 277 scoped_ptr<int> result); |
276 | 278 |
277 // Called after completion of asynchronous IO and receiving file metadata for | 279 // Called after completion of asynchronous IO and receiving file metadata for |
278 // the entry in |entry_stat|. Updates the metadata in the entry and in the | 280 // the entry in |entry_stat|. Updates the metadata in the entry and in the |
279 // index to make them available on next IO operations. | 281 // index to make them available on next IO operations. |
280 void UpdateDataFromEntryStat(const SimpleEntryStat& entry_stat); | 282 void UpdateDataFromEntryStat(const SimpleEntryStat& entry_stat); |
281 | 283 |
282 int64 GetDiskUsage() const; | 284 int64_t GetDiskUsage() const; |
283 | 285 |
284 // Used to report histograms. | 286 // Used to report histograms. |
285 void RecordReadIsParallelizable(const SimpleEntryOperation& operation) const; | 287 void RecordReadIsParallelizable(const SimpleEntryOperation& operation) const; |
286 void RecordWriteDependencyType(const SimpleEntryOperation& operation) const; | 288 void RecordWriteDependencyType(const SimpleEntryOperation& operation) const; |
287 | 289 |
288 // Reads from the stream 0 data kept in memory. | 290 // Reads from the stream 0 data kept in memory. |
289 int ReadStream0Data(net::IOBuffer* buf, int offset, int buf_len); | 291 int ReadStream0Data(net::IOBuffer* buf, int offset, int buf_len); |
290 | 292 |
291 // Copies data from |buf| to the internal in-memory buffer for stream 0. If | 293 // Copies data from |buf| to the internal in-memory buffer for stream 0. If |
292 // |truncate| is set to true, the target buffer will be truncated at |offset| | 294 // |truncate| is set to true, the target buffer will be truncated at |offset| |
(...skipping 12 matching lines...) Expand all Loading... |
305 scoped_ptr<ActiveEntryProxy> active_entry_proxy_; | 307 scoped_ptr<ActiveEntryProxy> active_entry_proxy_; |
306 | 308 |
307 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 309 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
308 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 310 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
309 base::ThreadChecker io_thread_checker_; | 311 base::ThreadChecker io_thread_checker_; |
310 | 312 |
311 const base::WeakPtr<SimpleBackendImpl> backend_; | 313 const base::WeakPtr<SimpleBackendImpl> backend_; |
312 const net::CacheType cache_type_; | 314 const net::CacheType cache_type_; |
313 const scoped_refptr<base::TaskRunner> worker_pool_; | 315 const scoped_refptr<base::TaskRunner> worker_pool_; |
314 const base::FilePath path_; | 316 const base::FilePath path_; |
315 const uint64 entry_hash_; | 317 const uint64_t entry_hash_; |
316 const bool use_optimistic_operations_; | 318 const bool use_optimistic_operations_; |
317 std::string key_; | 319 std::string key_; |
318 | 320 |
319 // |last_used_|, |last_modified_| and |data_size_| are copied from the | 321 // |last_used_|, |last_modified_| and |data_size_| are copied from the |
320 // synchronous entry at the completion of each item of asynchronous IO. | 322 // synchronous entry at the completion of each item of asynchronous IO. |
321 // TODO(clamy): Unify last_used_ with data in the index. | 323 // TODO(clamy): Unify last_used_ with data in the index. |
322 base::Time last_used_; | 324 base::Time last_used_; |
323 base::Time last_modified_; | 325 base::Time last_modified_; |
324 int32 data_size_[kSimpleEntryStreamCount]; | 326 int32_t data_size_[kSimpleEntryStreamCount]; |
325 int32 sparse_data_size_; | 327 int32_t sparse_data_size_; |
326 | 328 |
327 // Number of times this object has been returned from Backend::OpenEntry() and | 329 // Number of times this object has been returned from Backend::OpenEntry() and |
328 // Backend::CreateEntry() without subsequent Entry::Close() calls. Used to | 330 // Backend::CreateEntry() without subsequent Entry::Close() calls. Used to |
329 // notify the backend when this entry not used by any callers. | 331 // notify the backend when this entry not used by any callers. |
330 int open_count_; | 332 int open_count_; |
331 | 333 |
332 bool doomed_; | 334 bool doomed_; |
333 | 335 |
334 State state_; | 336 State state_; |
335 | 337 |
336 // When possible, we compute a crc32, for the data in each entry as we read or | 338 // When possible, we compute a crc32, for the data in each entry as we read or |
337 // write. For each stream, |crc32s_[index]| is the crc32 of that stream from | 339 // write. For each stream, |crc32s_[index]| is the crc32 of that stream from |
338 // [0 .. |crc32s_end_offset_|). If |crc32s_end_offset_[index] == 0| then the | 340 // [0 .. |crc32s_end_offset_|). If |crc32s_end_offset_[index] == 0| then the |
339 // value of |crc32s_[index]| is undefined. | 341 // value of |crc32s_[index]| is undefined. |
340 // Note at this can only be done in the current implementation in the case of | 342 // Note at this can only be done in the current implementation in the case of |
341 // a single entry reader that reads serially through the entire file. | 343 // a single entry reader that reads serially through the entire file. |
342 // Extending this to multiple readers is possible, but isn't currently worth | 344 // Extending this to multiple readers is possible, but isn't currently worth |
343 // it; see http://crbug.com/488076#c3 for details. | 345 // it; see http://crbug.com/488076#c3 for details. |
344 int32 crc32s_end_offset_[kSimpleEntryStreamCount]; | 346 int32_t crc32s_end_offset_[kSimpleEntryStreamCount]; |
345 uint32 crc32s_[kSimpleEntryStreamCount]; | 347 uint32_t crc32s_[kSimpleEntryStreamCount]; |
346 | 348 |
347 // If |have_written_[index]| is true, we have written to the file that | 349 // If |have_written_[index]| is true, we have written to the file that |
348 // contains stream |index|. | 350 // contains stream |index|. |
349 bool have_written_[kSimpleEntryStreamCount]; | 351 bool have_written_[kSimpleEntryStreamCount]; |
350 | 352 |
351 // Reflects how much CRC checking has been done with the entry. This state is | 353 // Reflects how much CRC checking has been done with the entry. This state is |
352 // reported on closing each entry stream. | 354 // reported on closing each entry stream. |
353 CheckCrcResult crc_check_state_[kSimpleEntryStreamCount]; | 355 CheckCrcResult crc_check_state_[kSimpleEntryStreamCount]; |
354 | 356 |
355 // The |synchronous_entry_| is the worker thread object that performs IO on | 357 // The |synchronous_entry_| is the worker thread object that performs IO on |
(...skipping 18 matching lines...) Expand all Loading... |
374 // 1 on disk, to reduce the number of file descriptors and save disk space. | 376 // 1 on disk, to reduce the number of file descriptors and save disk space. |
375 // This strategy allows stream 1 to change size easily. Since stream 0 is only | 377 // This strategy allows stream 1 to change size easily. Since stream 0 is only |
376 // used to write HTTP headers, the memory consumption of keeping it in memory | 378 // used to write HTTP headers, the memory consumption of keeping it in memory |
377 // is acceptable. | 379 // is acceptable. |
378 scoped_refptr<net::GrowableIOBuffer> stream_0_data_; | 380 scoped_refptr<net::GrowableIOBuffer> stream_0_data_; |
379 }; | 381 }; |
380 | 382 |
381 } // namespace disk_cache | 383 } // namespace disk_cache |
382 | 384 |
383 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ | 385 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_IMPL_H_ |
OLD | NEW |