1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_DISK_CACHE_ENTRY_IMPL_H_ 6 #define NET_DISK_CACHE_ENTRY_IMPL_H_ 7 #pragma once 8 9 #include "base/memory/scoped_ptr.h" 10 #include "net/base/net_log.h" 11 #include "net/disk_cache/disk_cache.h" 12 #include "net/disk_cache/storage_block.h" 13 #include "net/disk_cache/storage_block-inl.h" 14 15 namespace disk_cache { 16 17 class BackendImpl; 18 class SparseControl; 19 20 // This class implements the Entry interface. An object of this 21 // class represents a single entry on the cache. 22 class EntryImpl : public Entry, public base::RefCounted<EntryImpl> { 23 friend class base::RefCounted<EntryImpl>; 24 friend class SparseControl; 25 public: 26 enum Operation { 27 kRead, 28 kWrite, 29 kSparseRead, 30 kSparseWrite, 31 kAsyncIO 32 }; 33 34 EntryImpl(BackendImpl* backend, Addr address, bool read_only); 35 36 // Background implementation of the Entry interface. 37 void DoomImpl(); 38 int ReadDataImpl(int index, int offset, net::IOBuffer* buf, int buf_len, 39 CompletionCallback* callback); 40 int WriteDataImpl(int index, int offset, net::IOBuffer* buf, int buf_len, 41 CompletionCallback* callback, bool truncate); 42 int ReadSparseDataImpl(int64 offset, net::IOBuffer* buf, int buf_len, 43 CompletionCallback* callback); 44 int WriteSparseDataImpl(int64 offset, net::IOBuffer* buf, int buf_len, 45 CompletionCallback* callback); 46 int GetAvailableRangeImpl(int64 offset, int len, int64* start); 47 void CancelSparseIOImpl(); 48 int ReadyForSparseIOImpl(CompletionCallback* callback); 49 entry()50 inline CacheEntryBlock* entry() { 51 return &entry_; 52 } 53 rankings()54 inline CacheRankingsBlock* rankings() { 55 return &node_; 56 } 57 58 uint32 GetHash(); 59 60 // Performs the initialization of a EntryImpl that will be added to the 61 // cache. 62 bool CreateEntry(Addr node_address, const std::string& key, uint32 hash); 63 64 // Returns true if this entry matches the lookup arguments. 65 bool IsSameEntry(const std::string& key, uint32 hash); 66 67 // Permamently destroys this entry. 68 void InternalDoom(); 69 70 // Deletes this entry from disk. If |everything| is false, only the user data 71 // will be removed, leaving the key and control data intact. 72 void DeleteEntryData(bool everything); 73 74 // Returns the address of the next entry on the list of entries with the same 75 // hash. 76 CacheAddr GetNextAddress(); 77 78 // Sets the address of the next entry on the list of entries with the same 79 // hash. 80 void SetNextAddress(Addr address); 81 82 // Reloads the rankings node information. 83 bool LoadNodeAddress(); 84 85 // Updates the stored data to reflect the run-time information for this entry. 86 // Returns false if the data could not be updated. The purpose of this method 87 // is to be able to detect entries that are currently in use. 88 bool Update(); 89 dirty()90 bool dirty() { 91 return dirty_; 92 } 93 doomed()94 bool doomed() { 95 return doomed_; 96 } 97 98 // Marks this entry as dirty (in memory) if needed. This is intended only for 99 // entries that are being read from disk, to be called during loading. 100 void SetDirtyFlag(int32 current_id); 101 102 // Fixes this entry so it can be treated as valid (to delete it). 103 void SetPointerForInvalidEntry(int32 new_id); 104 105 // Returns false if the entry is clearly invalid. 106 bool SanityCheck(); 107 108 // Handle the pending asynchronous IO count. 109 void IncrementIoCount(); 110 void DecrementIoCount(); 111 112 // Set the access times for this entry. This method provides support for 113 // the upgrade tool. 114 void SetTimes(base::Time last_used, base::Time last_modified); 115 116 // Generates a histogram for the time spent working on this operation. 117 void ReportIOTime(Operation op, const base::TimeTicks& start); 118 119 // Logs a begin event and enables logging for the EntryImpl. Will also cause 120 // an end event to be logged on destruction. The EntryImpl must have its key 121 // initialized before this is called. |created| is true if the Entry was 122 // created rather than opened. 123 void BeginLogging(net::NetLog* net_log, bool created); 124 125 const net::BoundNetLog& net_log() const; 126 127 // Entry interface. 128 virtual void Doom(); 129 virtual void Close(); 130 virtual std::string GetKey() const; 131 virtual base::Time GetLastUsed() const; 132 virtual base::Time GetLastModified() const; 133 virtual int32 GetDataSize(int index) const; 134 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, 135 net::CompletionCallback* completion_callback); 136 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, 137 net::CompletionCallback* completion_callback, 138 bool truncate); 139 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, 140 net::CompletionCallback* completion_callback); 141 virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len, 142 net::CompletionCallback* completion_callback); 143 virtual int GetAvailableRange(int64 offset, int len, int64* start, 144 CompletionCallback* callback); 145 virtual bool CouldBeSparse() const; 146 virtual void CancelSparseIO(); 147 virtual int ReadyForSparseIO(net::CompletionCallback* completion_callback); 148 149 private: 150 enum { 151 kNumStreams = 3 152 }; 153 class UserBuffer; 154 155 ~EntryImpl(); 156 157 // Do all the work for ReadDataImpl and WriteDataImpl. Implemented as 158 // separate functions to make logging of results simpler. 159 int InternalReadData(int index, int offset, net::IOBuffer* buf, 160 int buf_len, CompletionCallback* callback); 161 int InternalWriteData(int index, int offset, net::IOBuffer* buf, int buf_len, 162 CompletionCallback* callback, bool truncate); 163 164 // Initializes the storage for an internal or external data block. 165 bool CreateDataBlock(int index, int size); 166 167 // Initializes the storage for an internal or external generic block. 168 bool CreateBlock(int size, Addr* address); 169 170 // Deletes the data pointed by address, maybe backed by files_[index]. 171 // Note that most likely the caller should delete (and store) the reference to 172 // |address| *before* calling this method because we don't want to have an 173 // entry using an address that is already free. 174 void DeleteData(Addr address, int index); 175 176 // Updates ranking information. 177 void UpdateRank(bool modified); 178 179 // Returns a pointer to the file that stores the given address. 180 File* GetBackingFile(Addr address, int index); 181 182 // Returns a pointer to the file that stores external data. 183 File* GetExternalFile(Addr address, int index); 184 185 // Prepares the target file or buffer for a write of buf_len bytes at the 186 // given offset. 187 bool PrepareTarget(int index, int offset, int buf_len, bool truncate); 188 189 // Adjusts the internal buffer and file handle for a write that truncates this 190 // stream. 191 bool HandleTruncation(int index, int offset, int buf_len); 192 193 // Copies data from disk to the internal buffer. 194 bool CopyToLocalBuffer(int index); 195 196 // Reads from a block data file to this object's memory buffer. 197 bool MoveToLocalBuffer(int index); 198 199 // Loads the external file to this object's memory buffer. 200 bool ImportSeparateFile(int index, int new_size); 201 202 // Makes sure that the internal buffer can handle the a write of |buf_len| 203 // bytes to |offset|. 204 bool PrepareBuffer(int index, int offset, int buf_len); 205 206 // Flushes the in-memory data to the backing storage. The data destination 207 // is determined based on the current data length and |min_len|. 208 bool Flush(int index, int min_len); 209 210 // Updates the size of a given data stream. 211 void UpdateSize(int index, int old_size, int new_size); 212 213 // Initializes the sparse control object. Returns a net error code. 214 int InitSparseData(); 215 216 // Adds the provided |flags| to the current EntryFlags for this entry. 217 void SetEntryFlags(uint32 flags); 218 219 // Returns the current EntryFlags for this entry. 220 uint32 GetEntryFlags(); 221 222 // Gets the data stored at the given index. If the information is in memory, 223 // a buffer will be allocated and the data will be copied to it (the caller 224 // can find out the size of the buffer before making this call). Otherwise, 225 // the cache address of the data will be returned, and that address will be 226 // removed from the regular book keeping of this entry so the caller is 227 // responsible for deleting the block (or file) from the backing store at some 228 // point; there is no need to report any storage-size change, only to do the 229 // actual cleanup. 230 void GetData(int index, char** buffer, Addr* address); 231 232 // Logs this entry to the internal trace buffer. 233 void Log(const char* msg); 234 235 CacheEntryBlock entry_; // Key related information for this entry. 236 CacheRankingsBlock node_; // Rankings related information for this entry. 237 BackendImpl* backend_; // Back pointer to the cache. 238 scoped_ptr<UserBuffer> user_buffers_[kNumStreams]; // Stores user data. 239 // Files to store external user data and key. 240 scoped_refptr<File> files_[kNumStreams + 1]; 241 mutable std::string key_; // Copy of the key. 242 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. 243 bool doomed_; // True if this entry was removed from the cache. 244 bool read_only_; // True if not yet writing. 245 bool dirty_; // True if we detected that this is a dirty entry. 246 scoped_ptr<SparseControl> sparse_; // Support for sparse entries. 247 248 net::BoundNetLog net_log_; 249 250 DISALLOW_COPY_AND_ASSIGN(EntryImpl); 251 }; 252 253 } // namespace disk_cache 254 255 #endif // NET_DISK_CACHE_ENTRY_IMPL_H_ 256