1 // Copyright (c) 2006-2009 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 8 #include "base/scoped_ptr.h" 9 #include "net/disk_cache/disk_cache.h" 10 #include "net/disk_cache/storage_block.h" 11 #include "net/disk_cache/storage_block-inl.h" 12 13 namespace disk_cache { 14 15 class BackendImpl; 16 class SparseControl; 17 18 // This class implements the Entry interface. An object of this 19 // class represents a single entry on the cache. 20 class EntryImpl : public Entry, public base::RefCounted<EntryImpl> { 21 friend class base::RefCounted<EntryImpl>; 22 friend class SparseControl; 23 public: 24 enum Operation { 25 kRead, 26 kWrite, 27 kSparseRead, 28 kSparseWrite, 29 kAsyncIO 30 }; 31 32 EntryImpl(BackendImpl* backend, Addr address); 33 34 // Entry interface. 35 virtual void Doom(); 36 virtual void Close(); 37 virtual std::string GetKey() const; 38 virtual base::Time GetLastUsed() const; 39 virtual base::Time GetLastModified() const; 40 virtual int32 GetDataSize(int index) const; 41 virtual int ReadData(int index, int offset, net::IOBuffer* buf, int buf_len, 42 net::CompletionCallback* completion_callback); 43 virtual int WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, 44 net::CompletionCallback* completion_callback, 45 bool truncate); 46 virtual int ReadSparseData(int64 offset, net::IOBuffer* buf, int buf_len, 47 net::CompletionCallback* completion_callback); 48 virtual int WriteSparseData(int64 offset, net::IOBuffer* buf, int buf_len, 49 net::CompletionCallback* completion_callback); 50 virtual int GetAvailableRange(int64 offset, int len, int64* start); 51 virtual int GetAvailableRange(int64 offset, int len, int64* start, 52 CompletionCallback* callback); 53 virtual void CancelSparseIO(); 54 virtual int ReadyForSparseIO(net::CompletionCallback* completion_callback); 55 entry()56 inline CacheEntryBlock* entry() { 57 return &entry_; 58 } 59 rankings()60 inline CacheRankingsBlock* rankings() { 61 return &node_; 62 } 63 64 uint32 GetHash(); 65 66 // Performs the initialization of a EntryImpl that will be added to the 67 // cache. 68 bool CreateEntry(Addr node_address, const std::string& key, uint32 hash); 69 70 // Returns true if this entry matches the lookup arguments. 71 bool IsSameEntry(const std::string& key, uint32 hash); 72 73 // Permamently destroys this entry. 74 void InternalDoom(); 75 76 // Deletes this entry from disk. If |everything| is false, only the user data 77 // will be removed, leaving the key and control data intact. 78 void DeleteEntryData(bool everything); 79 80 // Returns the address of the next entry on the list of entries with the same 81 // hash. 82 CacheAddr GetNextAddress(); 83 84 // Sets the address of the next entry on the list of entries with the same 85 // hash. 86 void SetNextAddress(Addr address); 87 88 // Reloads the rankings node information. 89 bool LoadNodeAddress(); 90 91 // Updates the stored data to reflect the run-time information for this entry. 92 // Returns false if the data could not be updated. The purpose of this method 93 // is to be able to detect entries that are currently in use. 94 bool Update(); 95 96 // Returns true if this entry is marked as dirty on disk. 97 bool IsDirty(int32 current_id); 98 void ClearDirtyFlag(); 99 100 // Fixes this entry so it can be treated as valid (to delete it). 101 void SetPointerForInvalidEntry(int32 new_id); 102 103 // Returns false if the entry is clearly invalid. 104 bool SanityCheck(); 105 106 // Handle the pending asynchronous IO count. 107 void IncrementIoCount(); 108 void DecrementIoCount(); 109 110 // Set the access times for this entry. This method provides support for 111 // the upgrade tool. 112 void SetTimes(base::Time last_used, base::Time last_modified); 113 114 // Generates a histogram for the time spent working on this operation. 115 void ReportIOTime(Operation op, const base::Time& start); 116 117 private: 118 enum { 119 kNumStreams = 3 120 }; 121 122 ~EntryImpl(); 123 124 // Initializes the storage for an internal or external data block. 125 bool CreateDataBlock(int index, int size); 126 127 // Initializes the storage for an internal or external generic block. 128 bool CreateBlock(int size, Addr* address); 129 130 // Deletes the data pointed by address, maybe backed by files_[index]. 131 void DeleteData(Addr address, int index); 132 133 // Updates ranking information. 134 void UpdateRank(bool modified); 135 136 // Returns a pointer to the file that stores the given address. 137 File* GetBackingFile(Addr address, int index); 138 139 // Returns a pointer to the file that stores external data. 140 File* GetExternalFile(Addr address, int index); 141 142 // Prepares the target file or buffer for a write of buf_len bytes at the 143 // given offset. 144 bool PrepareTarget(int index, int offset, int buf_len, bool truncate); 145 146 // Grows the size of the storage used to store user data, if needed. 147 bool GrowUserBuffer(int index, int offset, int buf_len, bool truncate); 148 149 // Reads from a block data file to this object's memory buffer. 150 bool MoveToLocalBuffer(int index); 151 152 // Loads the external file to this object's memory buffer. 153 bool ImportSeparateFile(int index, int offset, int buf_len); 154 155 // Flush the in-memory data to the backing storage. 156 bool Flush(int index, int size, bool async); 157 158 // Initializes the sparse control object. Returns a net error code. 159 int InitSparseData(); 160 161 // Adds the provided |flags| to the current EntryFlags for this entry. 162 void SetEntryFlags(uint32 flags); 163 164 // Returns the current EntryFlags for this entry. 165 uint32 GetEntryFlags(); 166 167 // Gets the data stored at the given index. If the information is in memory, 168 // a buffer will be allocated and the data will be copied to it (the caller 169 // can find out the size of the buffer before making this call). Otherwise, 170 // the cache address of the data will be returned, and that address will be 171 // removed from the regular book keeping of this entry so the caller is 172 // responsible for deleting the block (or file) from the backing store at some 173 // point; there is no need to report any storage-size change, only to do the 174 // actual cleanup. 175 void GetData(int index, char** buffer, Addr* address); 176 177 // Logs this entry to the internal trace buffer. 178 void Log(const char* msg); 179 180 CacheEntryBlock entry_; // Key related information for this entry. 181 CacheRankingsBlock node_; // Rankings related information for this entry. 182 BackendImpl* backend_; // Back pointer to the cache. 183 scoped_array<char> user_buffers_[kNumStreams]; // Store user data. 184 // Files to store external user data and key. 185 scoped_refptr<File> files_[kNumStreams + 1]; 186 // Copy of the file used to store the key. We don't own this object. 187 mutable File* key_file_; 188 int unreported_size_[kNumStreams]; // Bytes not reported yet to the backend. 189 bool doomed_; // True if this entry was removed from the cache. 190 scoped_ptr<SparseControl> sparse_; // Support for sparse entries. 191 192 DISALLOW_EVIL_CONSTRUCTORS(EntryImpl); 193 }; 194 195 } // namespace disk_cache 196 197 #endif // NET_DISK_CACHE_ENTRY_IMPL_H_ 198