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 3 // found in the LICENSE file. 4 5 // Defines the public interface of the disk cache. For more details see 6 // http://dev.chromium.org/developers/design-documents/network-stack/disk-cache 7 8 #ifndef NET_DISK_CACHE_DISK_CACHE_H_ 9 #define NET_DISK_CACHE_DISK_CACHE_H_ 10 11 #include <string> 12 #include <vector> 13 14 #include "base/basictypes.h" 15 #include "base/time/time.h" 16 #include "net/base/cache_type.h" 17 #include "net/base/completion_callback.h" 18 #include "net/base/net_export.h" 19 20 namespace base { 21 class FilePath; 22 class MessageLoopProxy; 23 } 24 25 namespace net { 26 class IOBuffer; 27 class NetLog; 28 } 29 30 namespace disk_cache { 31 32 class Entry; 33 class Backend; 34 35 // Returns an instance of a Backend of the given |type|. |path| points to a 36 // folder where the cached data will be stored (if appropriate). This cache 37 // instance must be the only object that will be reading or writing files to 38 // that folder. The returned object should be deleted when not needed anymore. 39 // If |force| is true, and there is a problem with the cache initialization, the 40 // files will be deleted and a new set will be created. |max_bytes| is the 41 // maximum size the cache can grow to. If zero is passed in as |max_bytes|, the 42 // cache will determine the value to use. |thread| can be used to perform IO 43 // operations if a dedicated thread is required; a valid value is expected for 44 // any backend that performs operations on a disk. The returned pointer can be 45 // NULL if a fatal error is found. The actual return value of the function is a 46 // net error code. If this function returns ERR_IO_PENDING, the |callback| will 47 // be invoked when a backend is available or a fatal error condition is reached. 48 // The pointer to receive the |backend| must remain valid until the operation 49 // completes (the callback is notified). 50 NET_EXPORT int CreateCacheBackend(net::CacheType type, 51 net::BackendType backend_type, 52 const base::FilePath& path, 53 int max_bytes, 54 bool force, 55 base::MessageLoopProxy* thread, 56 net::NetLog* net_log, 57 scoped_ptr<Backend>* backend, 58 const net::CompletionCallback& callback); 59 60 // The root interface for a disk cache instance. 61 class NET_EXPORT Backend { 62 public: 63 typedef net::CompletionCallback CompletionCallback; 64 65 // If the backend is destroyed when there are operations in progress (any 66 // callback that has not been invoked yet), this method cancels said 67 // operations so the callbacks are not invoked, possibly leaving the work 68 // half way (for instance, dooming just a few entries). Note that pending IO 69 // for a given Entry (as opposed to the Backend) will still generate a 70 // callback from within this method. ~Backend()71 virtual ~Backend() {} 72 73 // Returns the type of this cache. 74 virtual net::CacheType GetCacheType() const = 0; 75 76 // Returns the number of entries in the cache. 77 virtual int32 GetEntryCount() const = 0; 78 79 // Opens an existing entry. Upon success, |entry| holds a pointer to an Entry 80 // object representing the specified disk cache entry. When the entry pointer 81 // is no longer needed, its Close method should be called. The return value is 82 // a net error code. If this method returns ERR_IO_PENDING, the |callback| 83 // will be invoked when the entry is available. The pointer to receive the 84 // |entry| must remain valid until the operation completes. 85 virtual int OpenEntry(const std::string& key, Entry** entry, 86 const CompletionCallback& callback) = 0; 87 88 // Creates a new entry. Upon success, the out param holds a pointer to an 89 // Entry object representing the newly created disk cache entry. When the 90 // entry pointer is no longer needed, its Close method should be called. The 91 // return value is a net error code. If this method returns ERR_IO_PENDING, 92 // the |callback| will be invoked when the entry is available. The pointer to 93 // receive the |entry| must remain valid until the operation completes. 94 virtual int CreateEntry(const std::string& key, Entry** entry, 95 const CompletionCallback& callback) = 0; 96 97 // Marks the entry, specified by the given key, for deletion. The return value 98 // is a net error code. If this method returns ERR_IO_PENDING, the |callback| 99 // will be invoked after the entry is doomed. 100 virtual int DoomEntry(const std::string& key, 101 const CompletionCallback& callback) = 0; 102 103 // Marks all entries for deletion. The return value is a net error code. If 104 // this method returns ERR_IO_PENDING, the |callback| will be invoked when the 105 // operation completes. 106 virtual int DoomAllEntries(const CompletionCallback& callback) = 0; 107 108 // Marks a range of entries for deletion. This supports unbounded deletes in 109 // either direction by using null Time values for either argument. The return 110 // value is a net error code. If this method returns ERR_IO_PENDING, the 111 // |callback| will be invoked when the operation completes. 112 // Entries with |initial_time| <= access time < |end_time| are deleted. 113 virtual int DoomEntriesBetween(base::Time initial_time, 114 base::Time end_time, 115 const CompletionCallback& callback) = 0; 116 117 // Marks all entries accessed since |initial_time| for deletion. The return 118 // value is a net error code. If this method returns ERR_IO_PENDING, the 119 // |callback| will be invoked when the operation completes. 120 // Entries with |initial_time| <= access time are deleted. 121 virtual int DoomEntriesSince(base::Time initial_time, 122 const CompletionCallback& callback) = 0; 123 124 // Enumerates the cache. Initialize |iter| to NULL before calling this method 125 // the first time. That will cause the enumeration to start at the head of 126 // the cache. For subsequent calls, pass the same |iter| pointer again without 127 // changing its value. This method returns ERR_FAILED when there are no more 128 // entries to enumerate. When the entry pointer is no longer needed, its 129 // Close method should be called. The return value is a net error code. If 130 // this method returns ERR_IO_PENDING, the |callback| will be invoked when the 131 // |next_entry| is available. The pointer to receive the |next_entry| must 132 // remain valid until the operation completes. 133 // 134 // NOTE: This method does not modify the last_used field of the entry, and 135 // therefore it does not impact the eviction ranking of the entry. However, 136 // an enumeration will go through all entries on the cache only if the cache 137 // is not modified while the enumeration is taking place. Significantly 138 // altering the entry pointed by |iter| (for example, deleting the entry) will 139 // invalidate |iter|. Performing operations on an entry that modify the entry 140 // may result in loops in the iteration, skipped entries or similar. 141 virtual int OpenNextEntry(void** iter, Entry** next_entry, 142 const CompletionCallback& callback) = 0; 143 144 // Releases iter without returning the next entry. Whenever OpenNextEntry() 145 // returns true, but the caller is not interested in continuing the 146 // enumeration by calling OpenNextEntry() again, the enumeration must be 147 // ended by calling this method with iter returned by OpenNextEntry(). 148 virtual void EndEnumeration(void** iter) = 0; 149 150 // Return a list of cache statistics. 151 virtual void GetStats( 152 std::vector<std::pair<std::string, std::string> >* stats) = 0; 153 154 // Called whenever an external cache in the system reuses the resource 155 // referred to by |key|. 156 virtual void OnExternalCacheHit(const std::string& key) = 0; 157 }; 158 159 // This interface represents an entry in the disk cache. 160 class NET_EXPORT Entry { 161 public: 162 typedef net::CompletionCallback CompletionCallback; 163 typedef net::IOBuffer IOBuffer; 164 165 // Marks this cache entry for deletion. 166 virtual void Doom() = 0; 167 168 // Releases this entry. Calling this method does not cancel pending IO 169 // operations on this entry. Even after the last reference to this object has 170 // been released, pending completion callbacks may be invoked. 171 virtual void Close() = 0; 172 173 // Returns the key associated with this cache entry. 174 virtual std::string GetKey() const = 0; 175 176 // Returns the time when this cache entry was last used. 177 virtual base::Time GetLastUsed() const = 0; 178 179 // Returns the time when this cache entry was last modified. 180 virtual base::Time GetLastModified() const = 0; 181 182 // Returns the size of the cache data with the given index. 183 virtual int32 GetDataSize(int index) const = 0; 184 185 // Copies cached data into the given buffer of length |buf_len|. Returns the 186 // number of bytes read or a network error code. If this function returns 187 // ERR_IO_PENDING, the completion callback will be called on the current 188 // thread when the operation completes, and a reference to |buf| will be 189 // retained until the callback is called. Note that as long as the function 190 // does not complete immediately, the callback will always be invoked, even 191 // after Close has been called; in other words, the caller may close this 192 // entry without having to wait for all the callbacks, and still rely on the 193 // cleanup performed from the callback code. 194 virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len, 195 const CompletionCallback& callback) = 0; 196 197 // Copies data from the given buffer of length |buf_len| into the cache. 198 // Returns the number of bytes written or a network error code. If this 199 // function returns ERR_IO_PENDING, the completion callback will be called 200 // on the current thread when the operation completes, and a reference to 201 // |buf| will be retained until the callback is called. Note that as long as 202 // the function does not complete immediately, the callback will always be 203 // invoked, even after Close has been called; in other words, the caller may 204 // close this entry without having to wait for all the callbacks, and still 205 // rely on the cleanup performed from the callback code. 206 // If truncate is true, this call will truncate the stored data at the end of 207 // what we are writing here. 208 virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len, 209 const CompletionCallback& callback, 210 bool truncate) = 0; 211 212 // Sparse entries support: 213 // 214 // A Backend implementation can support sparse entries, so the cache keeps 215 // track of which parts of the entry have been written before. The backend 216 // will never return data that was not written previously, so reading from 217 // such region will return 0 bytes read (or actually the number of bytes read 218 // before reaching that region). 219 // 220 // There are only two streams for sparse entries: a regular control stream 221 // (index 0) that must be accessed through the regular API (ReadData and 222 // WriteData), and one sparse stream that must me accessed through the sparse- 223 // aware API that follows. Calling a non-sparse aware method with an index 224 // argument other than 0 is a mistake that results in implementation specific 225 // behavior. Using a sparse-aware method with an entry that was not stored 226 // using the same API, or with a backend that doesn't support sparse entries 227 // will return ERR_CACHE_OPERATION_NOT_SUPPORTED. 228 // 229 // The storage granularity of the implementation should be at least 1 KB. In 230 // other words, storing less than 1 KB may result in an implementation 231 // dropping the data completely, and writing at offsets not aligned with 1 KB, 232 // or with lengths not a multiple of 1 KB may result in the first or last part 233 // of the data being discarded. However, two consecutive writes should not 234 // result in a hole in between the two parts as long as they are sequential 235 // (the second one starts where the first one ended), and there is no other 236 // write between them. 237 // 238 // The Backend implementation is free to evict any range from the cache at any 239 // moment, so in practice, the previously stated granularity of 1 KB is not 240 // as bad as it sounds. 241 // 242 // The sparse methods don't support multiple simultaneous IO operations to the 243 // same physical entry, so in practice a single object should be instantiated 244 // for a given key at any given time. Once an operation has been issued, the 245 // caller should wait until it completes before starting another one. This 246 // requirement includes the case when an entry is closed while some operation 247 // is in progress and another object is instantiated; any IO operation will 248 // fail while the previous operation is still in-flight. In order to deal with 249 // this requirement, the caller could either wait until the operation 250 // completes before closing the entry, or call CancelSparseIO() before closing 251 // the entry, and call ReadyForSparseIO() on the new entry and wait for the 252 // callback before issuing new operations. 253 254 // Behaves like ReadData() except that this method is used to access sparse 255 // entries. 256 virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, 257 const CompletionCallback& callback) = 0; 258 259 // Behaves like WriteData() except that this method is used to access sparse 260 // entries. |truncate| is not part of this interface because a sparse entry 261 // is not expected to be reused with new data. To delete the old data and 262 // start again, or to reduce the total size of the stream data (which implies 263 // that the content has changed), the whole entry should be doomed and 264 // re-created. 265 virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, 266 const CompletionCallback& callback) = 0; 267 268 // Returns information about the currently stored portion of a sparse entry. 269 // |offset| and |len| describe a particular range that should be scanned to 270 // find out if it is stored or not. |start| will contain the offset of the 271 // first byte that is stored within this range, and the return value is the 272 // minimum number of consecutive stored bytes. Note that it is possible that 273 // this entry has stored more than the returned value. This method returns a 274 // net error code whenever the request cannot be completed successfully. If 275 // this method returns ERR_IO_PENDING, the |callback| will be invoked when the 276 // operation completes, and |start| must remain valid until that point. 277 virtual int GetAvailableRange(int64 offset, int len, int64* start, 278 const CompletionCallback& callback) = 0; 279 280 // Returns true if this entry could be a sparse entry or false otherwise. This 281 // is a quick test that may return true even if the entry is not really 282 // sparse. This method doesn't modify the state of this entry (it will not 283 // create sparse tracking data). GetAvailableRange or ReadSparseData can be 284 // used to perform a definitive test of whether an existing entry is sparse or 285 // not, but that method may modify the current state of the entry (making it 286 // sparse, for instance). The purpose of this method is to test an existing 287 // entry, but without generating actual IO to perform a thorough check. 288 virtual bool CouldBeSparse() const = 0; 289 290 // Cancels any pending sparse IO operation (if any). The completion callback 291 // of the operation in question will still be called when the operation 292 // finishes, but the operation will finish sooner when this method is used. 293 virtual void CancelSparseIO() = 0; 294 295 // Returns OK if this entry can be used immediately. If that is not the 296 // case, returns ERR_IO_PENDING and invokes the provided callback when this 297 // entry is ready to use. This method always returns OK for non-sparse 298 // entries, and returns ERR_IO_PENDING when a previous operation was cancelled 299 // (by calling CancelSparseIO), but the cache is still busy with it. If there 300 // is a pending operation that has not been cancelled, this method will return 301 // OK although another IO operation cannot be issued at this time; in this 302 // case the caller should just wait for the regular callback to be invoked 303 // instead of using this method to provide another callback. 304 // 305 // Note that CancelSparseIO may have been called on another instance of this 306 // object that refers to the same physical disk entry. 307 // Note: This method is deprecated. 308 virtual int ReadyForSparseIO(const CompletionCallback& callback) = 0; 309 310 protected: ~Entry()311 virtual ~Entry() {} 312 }; 313 314 struct EntryDeleter { operatorEntryDeleter315 void operator()(Entry* entry) { 316 // Note that |entry| is ref-counted. 317 entry->Close(); 318 } 319 }; 320 321 // Automatically closes an entry when it goes out of scope. 322 typedef scoped_ptr<Entry, EntryDeleter> ScopedEntryPtr; 323 324 } // namespace disk_cache 325 326 #endif // NET_DISK_CACHE_DISK_CACHE_H_ 327