OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // This file declares a HttpTransactionFactory implementation that can be | 5 // This file declares a HttpTransactionFactory implementation that can be |
6 // layered on top of another HttpTransactionFactory to add HTTP caching. The | 6 // layered on top of another HttpTransactionFactory to add HTTP caching. The |
7 // caching logic follows RFC 2616 (any exceptions are called out in the code). | 7 // caching logic follows RFC 2616 (any exceptions are called out in the code). |
8 // | 8 // |
9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for | 9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for |
10 // the cache storage. | 10 // the cache storage. |
11 // | 11 // |
12 // See HttpTransactionFactory and HttpTransaction for more details. | 12 // See HttpTransactionFactory and HttpTransaction for more details. |
13 | 13 |
14 #ifndef NET_HTTP_HTTP_CACHE_H_ | 14 #ifndef NET_HTTP_HTTP_CACHE_H_ |
15 #define NET_HTTP_HTTP_CACHE_H_ | 15 #define NET_HTTP_HTTP_CACHE_H_ |
16 | 16 |
17 #include <list> | 17 #include <list> |
18 #include <set> | 18 #include <set> |
19 | 19 |
20 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
21 #include "base/file_path.h" | 21 #include "base/file_path.h" |
22 #include "base/hash_tables.h" | 22 #include "base/hash_tables.h" |
23 #include "base/scoped_ptr.h" | 23 #include "base/scoped_ptr.h" |
24 #include "base/task.h" | 24 #include "base/task.h" |
25 #include "base/weak_ptr.h" | 25 #include "base/weak_ptr.h" |
26 #include "net/base/cache_type.h" | 26 #include "net/base/cache_type.h" |
27 #include "net/base/completion_callback.h" | 27 #include "net/base/completion_callback.h" |
28 #include "net/http/http_transaction_factory.h" | 28 #include "net/http/http_transaction_factory.h" |
| 29 #include "testing/gtest/include/gtest/gtest_prod.h" |
29 | 30 |
30 class GURL; | 31 class GURL; |
31 class MessageLoop; | 32 class MessageLoop; |
32 class ViewCacheHelper; | 33 class ViewCacheHelper; |
33 | 34 |
34 namespace disk_cache { | 35 namespace disk_cache { |
35 class Backend; | 36 class Backend; |
36 class Entry; | 37 class Entry; |
37 } | 38 } |
38 | 39 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 kResponseInfoIndex = 0, | 178 kResponseInfoIndex = 0, |
178 kResponseContentIndex, | 179 kResponseContentIndex, |
179 kMetadataIndex, | 180 kMetadataIndex, |
180 | 181 |
181 // Must remain at the end of the enum. | 182 // Must remain at the end of the enum. |
182 kNumCacheEntryDataIndices | 183 kNumCacheEntryDataIndices |
183 }; | 184 }; |
184 friend class ::ViewCacheHelper; | 185 friend class ::ViewCacheHelper; |
185 | 186 |
186 private: | 187 private: |
| 188 FRIEND_TEST(HttpCacheTest, SimpleGET_WaitForBackend); |
| 189 FRIEND_TEST(HttpCacheTest, SimpleGET_WaitForBackend_CancelCreate); |
187 | 190 |
188 // Types -------------------------------------------------------------------- | 191 // Types -------------------------------------------------------------------- |
189 | 192 |
190 class BackendCallback; | 193 class BackendCallback; |
191 class MetadataWriter; | 194 class MetadataWriter; |
192 class Transaction; | 195 class Transaction; |
193 class WorkItem; | 196 class WorkItem; |
194 friend class Transaction; | 197 friend class Transaction; |
195 struct NewEntry; // Info for an entry under construction. | 198 struct PendingOp; // Info for an entry under construction. |
196 | 199 |
197 typedef std::list<Transaction*> TransactionList; | 200 typedef std::list<Transaction*> TransactionList; |
198 typedef std::list<WorkItem*> WorkItemList; | 201 typedef std::list<WorkItem*> WorkItemList; |
199 | 202 |
200 struct ActiveEntry { | 203 struct ActiveEntry { |
201 disk_cache::Entry* disk_entry; | 204 disk_cache::Entry* disk_entry; |
202 Transaction* writer; | 205 Transaction* writer; |
203 TransactionList readers; | 206 TransactionList readers; |
204 TransactionList pending_queue; | 207 TransactionList pending_queue; |
205 bool will_process_pending_queue; | 208 bool will_process_pending_queue; |
206 bool doomed; | 209 bool doomed; |
207 | 210 |
208 explicit ActiveEntry(disk_cache::Entry*); | 211 explicit ActiveEntry(disk_cache::Entry*); |
209 ~ActiveEntry(); | 212 ~ActiveEntry(); |
210 }; | 213 }; |
211 | 214 |
212 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; | 215 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; |
213 typedef base::hash_map<std::string, NewEntry*> NewEntriesMap; | 216 typedef base::hash_map<std::string, PendingOp*> PendingOpsMap; |
214 typedef std::set<ActiveEntry*> ActiveEntriesSet; | 217 typedef std::set<ActiveEntry*> ActiveEntriesSet; |
215 | 218 |
| 219 typedef int (*CreateCacheBackendFn)(CacheType, const FilePath&, int, |
| 220 bool, MessageLoop*, disk_cache::Backend**, |
| 221 CompletionCallback*); |
| 222 |
216 // Methods ------------------------------------------------------------------ | 223 // Methods ------------------------------------------------------------------ |
217 | 224 |
| 225 // Creates the |backend| object and notifies the |callback| when the operation |
| 226 // completes. Returns an error code. |
| 227 int CreateBackend(disk_cache::Backend** backend, |
| 228 CompletionCallback* callback); |
| 229 |
| 230 // Makes sure that the backend creation is complete before allowing the |
| 231 // provided transaction to use the object. Returns an error code. |trans| |
| 232 // will be notified via its IO callback if this method returns ERR_IO_PENDING. |
| 233 // The transaction is free to use the backend directly at any time after |
| 234 // receiving the notification. |
| 235 int GetBackendForTransaction(Transaction* trans); |
| 236 |
218 // Generates the cache key for this request. | 237 // Generates the cache key for this request. |
219 std::string GenerateCacheKey(const HttpRequestInfo*); | 238 std::string GenerateCacheKey(const HttpRequestInfo*); |
220 | 239 |
221 // Dooms the entry selected by |key|. |trans| will be notified via its IO | 240 // Dooms the entry selected by |key|. |trans| will be notified via its IO |
222 // callback if this method returns ERR_IO_PENDING. The entry can be | 241 // callback if this method returns ERR_IO_PENDING. The entry can be |
223 // currently in use or not. | 242 // currently in use or not. |
224 int DoomEntry(const std::string& key, Transaction* trans); | 243 int DoomEntry(const std::string& key, Transaction* trans); |
225 | 244 |
226 // Dooms the entry selected by |key|. |trans| will be notified via its IO | 245 // Dooms the entry selected by |key|. |trans| will be notified via its IO |
227 // callback if this method returns ERR_IO_PENDING. The entry should not | 246 // callback if this method returns ERR_IO_PENDING. The entry should not |
(...skipping 11 matching lines...) Expand all Loading... |
239 // TODO(rvargas): remove the |key| argument. | 258 // TODO(rvargas): remove the |key| argument. |
240 ActiveEntry* ActivateEntry(const std::string& key, | 259 ActiveEntry* ActivateEntry(const std::string& key, |
241 disk_cache::Entry* disk_entry); | 260 disk_cache::Entry* disk_entry); |
242 | 261 |
243 // Deletes an ActiveEntry. | 262 // Deletes an ActiveEntry. |
244 void DeactivateEntry(ActiveEntry* entry); | 263 void DeactivateEntry(ActiveEntry* entry); |
245 | 264 |
246 // Deletes an ActiveEntry using an exhaustive search. | 265 // Deletes an ActiveEntry using an exhaustive search. |
247 void SlowDeactivateEntry(ActiveEntry* entry); | 266 void SlowDeactivateEntry(ActiveEntry* entry); |
248 | 267 |
249 // Returns the NewEntry for the desired |key|. If an entry is not under | 268 // Returns the PendingOp for the desired |key|. If an entry is not under |
250 // construction already, a new NewEntry structure is created. | 269 // construction already, a new PendingOp structure is created. |
251 NewEntry* GetNewEntry(const std::string& key); | 270 PendingOp* GetPendingOp(const std::string& key); |
252 | 271 |
253 // Deletes a NewEntry. | 272 // Deletes a PendingOp. |
254 void DeleteNewEntry(NewEntry* entry); | 273 void DeletePendingOp(PendingOp* pending_op); |
255 | 274 |
256 // Opens the disk cache entry associated with |key|, returning an ActiveEntry | 275 // Opens the disk cache entry associated with |key|, returning an ActiveEntry |
257 // in |*entry|. |trans| will be notified via its IO callback if this method | 276 // in |*entry|. |trans| will be notified via its IO callback if this method |
258 // returns ERR_IO_PENDING. | 277 // returns ERR_IO_PENDING. |
259 int OpenEntry(const std::string& key, ActiveEntry** entry, | 278 int OpenEntry(const std::string& key, ActiveEntry** entry, |
260 Transaction* trans); | 279 Transaction* trans); |
261 | 280 |
262 // Creates the disk cache entry associated with |key|, returning an | 281 // Creates the disk cache entry associated with |key|, returning an |
263 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if | 282 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if |
264 // this method returns ERR_IO_PENDING. | 283 // this method returns ERR_IO_PENDING. |
(...skipping 20 matching lines...) Expand all Loading... |
285 void DoneWritingToEntry(ActiveEntry* entry, bool success); | 304 void DoneWritingToEntry(ActiveEntry* entry, bool success); |
286 | 305 |
287 // Called when the transaction has finished reading from this entry. | 306 // Called when the transaction has finished reading from this entry. |
288 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); | 307 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); |
289 | 308 |
290 // Convers the active writter transaction to a reader so that other | 309 // Convers the active writter transaction to a reader so that other |
291 // transactions can start reading from this entry. | 310 // transactions can start reading from this entry. |
292 void ConvertWriterToReader(ActiveEntry* entry); | 311 void ConvertWriterToReader(ActiveEntry* entry); |
293 | 312 |
294 // Removes the transaction |trans|, from the pending list of an entry | 313 // Removes the transaction |trans|, from the pending list of an entry |
295 // (NewEntry, active or doomed entry). | 314 // (PendingOp, active or doomed entry). |
296 void RemovePendingTransaction(Transaction* trans); | 315 void RemovePendingTransaction(Transaction* trans); |
297 | 316 |
298 // Removes the transaction |trans|, from the pending list of |entry|. | 317 // Removes the transaction |trans|, from the pending list of |entry|. |
299 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, | 318 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, |
300 Transaction* trans); | 319 Transaction* trans); |
301 | 320 |
302 // Removes the transaction |trans|, from the pending list of |entry|. | 321 // Removes the transaction |trans|, from the pending list of |pending_op|. |
303 bool RemovePendingTransactionFromNewEntry(NewEntry* entry, | 322 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, |
304 Transaction* trans); | 323 Transaction* trans); |
305 | 324 |
306 // Resumes processing the pending list of |entry|. | 325 // Resumes processing the pending list of |entry|. |
307 void ProcessPendingQueue(ActiveEntry* entry); | 326 void ProcessPendingQueue(ActiveEntry* entry); |
308 | 327 |
309 // Events (called via PostTask) --------------------------------------------- | 328 // Events (called via PostTask) --------------------------------------------- |
310 | 329 |
311 void OnProcessPendingQueue(ActiveEntry* entry); | 330 void OnProcessPendingQueue(ActiveEntry* entry); |
312 | 331 |
313 // Callbacks ---------------------------------------------------------------- | 332 // Callbacks ---------------------------------------------------------------- |
314 | 333 |
315 // Processes BackendCallback notifications. | 334 // Processes BackendCallback notifications. |
316 void OnIOComplete(int result, NewEntry* entry); | 335 void OnIOComplete(int result, PendingOp* entry); |
| 336 |
| 337 // Processes the backend creation notification. |
| 338 void OnBackendCreated(int result, PendingOp* pending_op); |
317 | 339 |
318 | 340 |
319 // Variables ---------------------------------------------------------------- | 341 // Variables ---------------------------------------------------------------- |
320 | 342 |
321 // Used when lazily constructing the disk_cache_. | 343 // Used when lazily constructing the disk_cache_. |
322 FilePath disk_cache_dir_; | 344 FilePath disk_cache_dir_; |
323 MessageLoop* cache_thread_; | 345 MessageLoop* cache_thread_; |
| 346 disk_cache::Backend* temp_backend_; |
| 347 bool building_backend_; |
324 | 348 |
325 Mode mode_; | 349 Mode mode_; |
326 CacheType type_; | 350 CacheType type_; |
327 | 351 |
328 scoped_ptr<HttpTransactionFactory> network_layer_; | 352 scoped_ptr<HttpTransactionFactory> network_layer_; |
329 scoped_ptr<disk_cache::Backend> disk_cache_; | 353 scoped_ptr<disk_cache::Backend> disk_cache_; |
330 | 354 |
331 // The set of active entries indexed by cache key. | 355 // The set of active entries indexed by cache key. |
332 ActiveEntriesMap active_entries_; | 356 ActiveEntriesMap active_entries_; |
333 | 357 |
334 // The set of doomed entries. | 358 // The set of doomed entries. |
335 ActiveEntriesSet doomed_entries_; | 359 ActiveEntriesSet doomed_entries_; |
336 | 360 |
337 // The set of entries "under construction". | 361 // The set of entries "under construction". |
338 NewEntriesMap new_entries_; | 362 PendingOpsMap pending_ops_; |
339 | 363 |
340 ScopedRunnableMethodFactory<HttpCache> task_factory_; | 364 ScopedRunnableMethodFactory<HttpCache> task_factory_; |
341 | 365 |
342 bool enable_range_support_; | 366 bool enable_range_support_; |
343 int cache_size_; | 367 int cache_size_; |
344 | 368 |
345 typedef base::hash_map<std::string, int> PlaybackCacheMap; | 369 typedef base::hash_map<std::string, int> PlaybackCacheMap; |
346 scoped_ptr<PlaybackCacheMap> playback_cache_map_; | 370 scoped_ptr<PlaybackCacheMap> playback_cache_map_; |
347 | 371 |
| 372 // Used for unit tests. |
| 373 CreateCacheBackendFn create_backend_fn_; |
| 374 |
348 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 375 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
349 }; | 376 }; |
350 | 377 |
351 } // namespace net | 378 } // namespace net |
352 | 379 |
353 #endif // NET_HTTP_HTTP_CACHE_H_ | 380 #endif // NET_HTTP_HTTP_CACHE_H_ |
OLD | NEW |