| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_ENTRY_IMPL_H_ | 5 #ifndef NET_DISK_CACHE_ENTRY_IMPL_H_ |
| 6 #define NET_DISK_CACHE_ENTRY_IMPL_H_ | 6 #define NET_DISK_CACHE_ENTRY_IMPL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "net/base/net_log.h" | 10 #include "net/base/net_log.h" |
| 11 #include "net/disk_cache/disk_cache.h" | 11 #include "net/disk_cache/disk_cache.h" |
| 12 #include "net/disk_cache/storage_block.h" | 12 #include "net/disk_cache/storage_block.h" |
| 13 #include "net/disk_cache/storage_block-inl.h" | 13 #include "net/disk_cache/storage_block-inl.h" |
| 14 | 14 |
| 15 namespace disk_cache { | 15 namespace disk_cache { |
| 16 | 16 |
| 17 class BackendImpl; | 17 class BackendImpl; |
| 18 class InFlightBackendIO; |
| 18 class SparseControl; | 19 class SparseControl; |
| 19 | 20 |
| 20 // This class implements the Entry interface. An object of this | 21 // This class implements the Entry interface. An object of this |
| 21 // class represents a single entry on the cache. | 22 // class represents a single entry on the cache. |
| 22 class NET_EXPORT_PRIVATE EntryImpl | 23 class NET_EXPORT_PRIVATE EntryImpl |
| 23 : public Entry, | 24 : public Entry, |
| 24 public base::RefCounted<EntryImpl> { | 25 public base::RefCounted<EntryImpl> { |
| 25 friend class base::RefCounted<EntryImpl>; | 26 friend class base::RefCounted<EntryImpl>; |
| 26 friend class SparseControl; | 27 friend class SparseControl; |
| 27 public: | 28 public: |
| 28 enum Operation { | 29 enum Operation { |
| 29 kRead, | 30 kRead, |
| 30 kWrite, | 31 kWrite, |
| 31 kSparseRead, | 32 kSparseRead, |
| 32 kSparseWrite, | 33 kSparseWrite, |
| 33 kAsyncIO, | 34 kAsyncIO, |
| 34 kReadAsync1, | 35 kReadAsync1, |
| 35 kWriteAsync1 | 36 kWriteAsync1 |
| 36 }; | 37 }; |
| 37 | 38 |
| 38 EntryImpl(BackendImpl* backend, Addr address, bool read_only); | 39 EntryImpl(BackendImpl* backend, Addr address, bool read_only); |
| 39 | 40 |
| 40 // Background implementation of the Entry interface. | 41 // Background implementation of the Entry interface. |
| 41 void DoomImpl(); | 42 void DoomImpl(); |
| 42 int ReadDataImpl(int index, int offset, net::IOBuffer* buf, int buf_len, | 43 int ReadDataImpl(int index, int offset, IOBuffer* buf, int buf_len, |
| 43 const net::CompletionCallback& callback); | 44 const CompletionCallback& callback); |
| 44 int WriteDataImpl(int index, int offset, net::IOBuffer* buf, int buf_len, | 45 int WriteDataImpl(int index, int offset, IOBuffer* buf, int buf_len, |
| 45 const net::CompletionCallback& callback, bool truncate); | 46 const CompletionCallback& callback, bool truncate); |
| 46 int ReadSparseDataImpl(int64 offset, net::IOBuffer* buf, int buf_len, | 47 int ReadSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, |
| 47 const net::CompletionCallback& callback); | 48 const CompletionCallback& callback); |
| 48 int WriteSparseDataImpl(int64 offset, net::IOBuffer* buf, int buf_len, | 49 int WriteSparseDataImpl(int64 offset, IOBuffer* buf, int buf_len, |
| 49 const net::CompletionCallback& callback); | 50 const CompletionCallback& callback); |
| 50 int GetAvailableRangeImpl(int64 offset, int len, int64* start); | 51 int GetAvailableRangeImpl(int64 offset, int len, int64* start); |
| 51 void CancelSparseIOImpl(); | 52 void CancelSparseIOImpl(); |
| 52 int ReadyForSparseIOImpl(const net::CompletionCallback& callback); | 53 int ReadyForSparseIOImpl(const CompletionCallback& callback); |
| 53 | 54 |
| 54 inline CacheEntryBlock* entry() { | 55 inline CacheEntryBlock* entry() { |
| 55 return &entry_; | 56 return &entry_; |
| 56 } | 57 } |
| 57 | 58 |
| 58 inline CacheRankingsBlock* rankings() { | 59 inline CacheRankingsBlock* rankings() { |
| 59 return &node_; | 60 return &node_; |
| 60 } | 61 } |
| 61 | 62 |
| 62 uint32 GetHash(); | 63 uint32 GetHash(); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 bool SanityCheck(); | 115 bool SanityCheck(); |
| 115 bool DataSanityCheck(); | 116 bool DataSanityCheck(); |
| 116 | 117 |
| 117 // Attempts to make this entry reachable though the key. | 118 // Attempts to make this entry reachable though the key. |
| 118 void FixForDelete(); | 119 void FixForDelete(); |
| 119 | 120 |
| 120 // Handle the pending asynchronous IO count. | 121 // Handle the pending asynchronous IO count. |
| 121 void IncrementIoCount(); | 122 void IncrementIoCount(); |
| 122 void DecrementIoCount(); | 123 void DecrementIoCount(); |
| 123 | 124 |
| 125 // This entry is being returned to the user. It is always called from the |
| 126 // primary thread (not the dedicated cache thread). |
| 127 void OnEntryCreated(BackendImpl* backend); |
| 128 |
| 124 // Set the access times for this entry. This method provides support for | 129 // Set the access times for this entry. This method provides support for |
| 125 // the upgrade tool. | 130 // the upgrade tool. |
| 126 void SetTimes(base::Time last_used, base::Time last_modified); | 131 void SetTimes(base::Time last_used, base::Time last_modified); |
| 127 | 132 |
| 128 // Generates a histogram for the time spent working on this operation. | 133 // Generates a histogram for the time spent working on this operation. |
| 129 void ReportIOTime(Operation op, const base::TimeTicks& start); | 134 void ReportIOTime(Operation op, const base::TimeTicks& start); |
| 130 | 135 |
| 131 // Logs a begin event and enables logging for the EntryImpl. Will also cause | 136 // Logs a begin event and enables logging for the EntryImpl. Will also cause |
| 132 // an end event to be logged on destruction. The EntryImpl must have its key | 137 // an end event to be logged on destruction. The EntryImpl must have its key |
| 133 // initialized before this is called. |created| is true if the Entry was | 138 // initialized before this is called. |created| is true if the Entry was |
| 134 // created rather than opened. | 139 // created rather than opened. |
| 135 void BeginLogging(net::NetLog* net_log, bool created); | 140 void BeginLogging(net::NetLog* net_log, bool created); |
| 136 | 141 |
| 137 const net::BoundNetLog& net_log() const; | 142 const net::BoundNetLog& net_log() const; |
| 138 | 143 |
| 139 // Returns the number of blocks needed to store an EntryStore. | 144 // Returns the number of blocks needed to store an EntryStore. |
| 140 static int NumBlocksForEntry(int key_size); | 145 static int NumBlocksForEntry(int key_size); |
| 141 | 146 |
| 142 // Entry interface. | 147 // Entry interface. |
| 143 virtual void Doom() OVERRIDE; | 148 virtual void Doom() OVERRIDE; |
| 144 virtual void Close() OVERRIDE; | 149 virtual void Close() OVERRIDE; |
| 145 virtual std::string GetKey() const OVERRIDE; | 150 virtual std::string GetKey() const OVERRIDE; |
| 146 virtual base::Time GetLastUsed() const OVERRIDE; | 151 virtual base::Time GetLastUsed() const OVERRIDE; |
| 147 virtual base::Time GetLastModified() const OVERRIDE; | 152 virtual base::Time GetLastModified() const OVERRIDE; |
| 148 virtual int32 GetDataSize(int index) const OVERRIDE; | 153 virtual int32 GetDataSize(int index) const OVERRIDE; |
| 149 virtual int ReadData( | 154 virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len, |
| 150 int index, int offset, net::IOBuffer* buf, int buf_len, | 155 const CompletionCallback& callback) OVERRIDE; |
| 151 const net::CompletionCallback& callback) OVERRIDE; | 156 virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len, |
| 152 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 157 const CompletionCallback& callback, |
| 153 const net::CompletionCallback& callback, | |
| 154 bool truncate) OVERRIDE; | 158 bool truncate) OVERRIDE; |
| 155 virtual int ReadSparseData( | 159 virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, |
| 156 int64 offset, net::IOBuffer* buf, int buf_len, | 160 const CompletionCallback& callback) OVERRIDE; |
| 157 const net::CompletionCallback& callback) OVERRIDE; | 161 virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, |
| 158 virtual int WriteSparseData( | 162 const CompletionCallback& callback) OVERRIDE; |
| 159 int64 offset, net::IOBuffer* buf, int buf_len, | 163 virtual int GetAvailableRange(int64 offset, int len, int64* start, |
| 160 const net::CompletionCallback& callback) OVERRIDE; | 164 const CompletionCallback& callback) OVERRIDE; |
| 161 virtual int GetAvailableRange( | |
| 162 int64 offset, int len, int64* start, | |
| 163 const net::CompletionCallback& callback) OVERRIDE; | |
| 164 virtual bool CouldBeSparse() const OVERRIDE; | 165 virtual bool CouldBeSparse() const OVERRIDE; |
| 165 virtual void CancelSparseIO() OVERRIDE; | 166 virtual void CancelSparseIO() OVERRIDE; |
| 166 virtual int ReadyForSparseIO( | 167 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; |
| 167 const net::CompletionCallback& callback) OVERRIDE; | |
| 168 | 168 |
| 169 private: | 169 private: |
| 170 enum { | 170 enum { |
| 171 kNumStreams = 3 | 171 kNumStreams = 3 |
| 172 }; | 172 }; |
| 173 class UserBuffer; | 173 class UserBuffer; |
| 174 | 174 |
| 175 virtual ~EntryImpl(); | 175 virtual ~EntryImpl(); |
| 176 | 176 |
| 177 // Do all the work for ReadDataImpl and WriteDataImpl. Implemented as | 177 // Do all the work for ReadDataImpl and WriteDataImpl. Implemented as |
| 178 // separate functions to make logging of results simpler. | 178 // separate functions to make logging of results simpler. |
| 179 int InternalReadData(int index, int offset, net::IOBuffer* buf, | 179 int InternalReadData(int index, int offset, IOBuffer* buf, |
| 180 int buf_len, const net::CompletionCallback& callback); | 180 int buf_len, const CompletionCallback& callback); |
| 181 int InternalWriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 181 int InternalWriteData(int index, int offset, IOBuffer* buf, int buf_len, |
| 182 const net::CompletionCallback& callback, bool truncate); | 182 const CompletionCallback& callback, bool truncate); |
| 183 | 183 |
| 184 // Initializes the storage for an internal or external data block. | 184 // Initializes the storage for an internal or external data block. |
| 185 bool CreateDataBlock(int index, int size); | 185 bool CreateDataBlock(int index, int size); |
| 186 | 186 |
| 187 // Initializes the storage for an internal or external generic block. | 187 // Initializes the storage for an internal or external generic block. |
| 188 bool CreateBlock(int size, Addr* address); | 188 bool CreateBlock(int size, Addr* address); |
| 189 | 189 |
| 190 // Deletes the data pointed by address, maybe backed by files_[index]. | 190 // Deletes the data pointed by address, maybe backed by files_[index]. |
| 191 // Note that most likely the caller should delete (and store) the reference to | 191 // Note that most likely the caller should delete (and store) the reference to |
| 192 // |address| *before* calling this method because we don't want to have an | 192 // |address| *before* calling this method because we don't want to have an |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 // responsible for deleting the block (or file) from the backing store at some | 247 // responsible for deleting the block (or file) from the backing store at some |
| 248 // point; there is no need to report any storage-size change, only to do the | 248 // point; there is no need to report any storage-size change, only to do the |
| 249 // actual cleanup. | 249 // actual cleanup. |
| 250 void GetData(int index, char** buffer, Addr* address); | 250 void GetData(int index, char** buffer, Addr* address); |
| 251 | 251 |
| 252 // Logs this entry to the internal trace buffer. | 252 // Logs this entry to the internal trace buffer. |
| 253 void Log(const char* msg); | 253 void Log(const char* msg); |
| 254 | 254 |
| 255 CacheEntryBlock entry_; // Key related information for this entry. | 255 CacheEntryBlock entry_; // Key related information for this entry. |
| 256 CacheRankingsBlock node_; // Rankings related information for this entry. | 256 CacheRankingsBlock node_; // Rankings related information for this entry. |
| 257 BackendImpl* backend_; // Back pointer to the cache. | 257 base::WeakPtr<BackendImpl> backend_; // Back pointer to the cache. |
| 258 base::WeakPtr<InFlightBackendIO> background_queue_; // In-progress queue. |
| 258 scoped_ptr<UserBuffer> user_buffers_[kNumStreams]; // Stores user data. | 259 scoped_ptr<UserBuffer> user_buffers_[kNumStreams]; // Stores user data. |
| 259 // Files to store external user data and key. | 260 // Files to store external user data and key. |
| 260 scoped_refptr<File> files_[kNumStreams + 1]; | 261 scoped_refptr<File> files_[kNumStreams + 1]; |
| 261 mutable std::string key_; // Copy of the key. | 262 mutable std::string key_; // Copy of the key. |
| 262 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. | 263 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. |
| 263 bool doomed_; // True if this entry was removed from the cache. | 264 bool doomed_; // True if this entry was removed from the cache. |
| 264 bool read_only_; // True if not yet writing. | 265 bool read_only_; // True if not yet writing. |
| 265 bool dirty_; // True if we detected that this is a dirty entry. | 266 bool dirty_; // True if we detected that this is a dirty entry. |
| 266 scoped_ptr<SparseControl> sparse_; // Support for sparse entries. | 267 scoped_ptr<SparseControl> sparse_; // Support for sparse entries. |
| 267 | 268 |
| 268 net::BoundNetLog net_log_; | 269 net::BoundNetLog net_log_; |
| 269 | 270 |
| 270 DISALLOW_COPY_AND_ASSIGN(EntryImpl); | 271 DISALLOW_COPY_AND_ASSIGN(EntryImpl); |
| 271 }; | 272 }; |
| 272 | 273 |
| 273 } // namespace disk_cache | 274 } // namespace disk_cache |
| 274 | 275 |
| 275 #endif // NET_DISK_CACHE_ENTRY_IMPL_H_ | 276 #endif // NET_DISK_CACHE_ENTRY_IMPL_H_ |
| OLD | NEW |