| OLD | NEW |
| 1 // Copyright (c) 2012 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 // 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 <map> |
| 18 #include <set> | 19 #include <set> |
| 19 #include <string> | 20 #include <string> |
| 20 | 21 |
| 21 #include "base/basictypes.h" | 22 #include "base/basictypes.h" |
| 22 #include "base/containers/hash_tables.h" | 23 #include "base/containers/hash_tables.h" |
| 23 #include "base/files/file_path.h" | 24 #include "base/files/file_path.h" |
| 24 #include "base/memory/scoped_ptr.h" | 25 #include "base/memory/scoped_ptr.h" |
| 25 #include "base/memory/weak_ptr.h" | 26 #include "base/memory/weak_ptr.h" |
| 26 #include "base/threading/non_thread_safe.h" | 27 #include "base/threading/non_thread_safe.h" |
| 27 #include "base/time/time.h" | 28 #include "base/time/time.h" |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 195 |
| 195 // Initializes the Infinite Cache, if selected by the field trial. | 196 // Initializes the Infinite Cache, if selected by the field trial. |
| 196 void InitializeInfiniteCache(const base::FilePath& path); | 197 void InitializeInfiniteCache(const base::FilePath& path); |
| 197 | 198 |
| 198 // Causes all transactions created after this point to effectively bypass | 199 // Causes all transactions created after this point to effectively bypass |
| 199 // the cache lock whenever there is lock contention. | 200 // the cache lock whenever there is lock contention. |
| 200 void BypassLockForTest() { | 201 void BypassLockForTest() { |
| 201 bypass_lock_for_test_ = true; | 202 bypass_lock_for_test_ = true; |
| 202 } | 203 } |
| 203 | 204 |
| 205 bool use_stale_while_revalidate() const { |
| 206 return use_stale_while_revalidate_; |
| 207 } |
| 208 |
| 209 // Enable stale_while_revalidate functionality for testing purposes. |
| 210 void set_use_stale_while_revalidate_for_testing( |
| 211 bool use_stale_while_revalidate) { |
| 212 use_stale_while_revalidate_ = use_stale_while_revalidate; |
| 213 } |
| 214 |
| 204 // HttpTransactionFactory implementation: | 215 // HttpTransactionFactory implementation: |
| 205 virtual int CreateTransaction(RequestPriority priority, | 216 virtual int CreateTransaction(RequestPriority priority, |
| 206 scoped_ptr<HttpTransaction>* trans) OVERRIDE; | 217 scoped_ptr<HttpTransaction>* trans) OVERRIDE; |
| 207 virtual HttpCache* GetCache() OVERRIDE; | 218 virtual HttpCache* GetCache() OVERRIDE; |
| 208 virtual HttpNetworkSession* GetSession() OVERRIDE; | 219 virtual HttpNetworkSession* GetSession() OVERRIDE; |
| 209 | 220 |
| 210 base::WeakPtr<HttpCache> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } | 221 base::WeakPtr<HttpCache> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } |
| 211 | 222 |
| 212 // Resets the network layer to allow for tests that probe | 223 // Resets the network layer to allow for tests that probe |
| 213 // network changes (e.g. host unreachable). The old network layer is | 224 // network changes (e.g. host unreachable). The old network layer is |
| (...skipping 16 matching lines...) Expand all Loading... |
| 230 kNumCacheEntryDataIndices | 241 kNumCacheEntryDataIndices |
| 231 }; | 242 }; |
| 232 | 243 |
| 233 class MetadataWriter; | 244 class MetadataWriter; |
| 234 class QuicServerInfoFactoryAdaptor; | 245 class QuicServerInfoFactoryAdaptor; |
| 235 class Transaction; | 246 class Transaction; |
| 236 class WorkItem; | 247 class WorkItem; |
| 237 friend class Transaction; | 248 friend class Transaction; |
| 238 friend class ViewCacheHelper; | 249 friend class ViewCacheHelper; |
| 239 struct PendingOp; // Info for an entry under construction. | 250 struct PendingOp; // Info for an entry under construction. |
| 251 class AsyncValidation; // Encapsulates a single async revalidation. |
| 240 | 252 |
| 241 typedef std::list<Transaction*> TransactionList; | 253 typedef std::list<Transaction*> TransactionList; |
| 242 typedef std::list<WorkItem*> WorkItemList; | 254 typedef std::list<WorkItem*> WorkItemList; |
| 255 typedef std::map<std::string, AsyncValidation*> AsyncValidationMap; |
| 243 | 256 |
| 244 struct ActiveEntry { | 257 struct ActiveEntry { |
| 245 explicit ActiveEntry(disk_cache::Entry* entry); | 258 explicit ActiveEntry(disk_cache::Entry* entry); |
| 246 ~ActiveEntry(); | 259 ~ActiveEntry(); |
| 247 | 260 |
| 248 disk_cache::Entry* disk_entry; | 261 disk_cache::Entry* disk_entry; |
| 249 Transaction* writer; | 262 Transaction* writer; |
| 250 TransactionList readers; | 263 TransactionList readers; |
| 251 TransactionList pending_queue; | 264 TransactionList pending_queue; |
| 252 bool will_process_pending_queue; | 265 bool will_process_pending_queue; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 // Removes the transaction |trans|, from the pending list of |pending_op|. | 380 // Removes the transaction |trans|, from the pending list of |pending_op|. |
| 368 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, | 381 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, |
| 369 Transaction* trans); | 382 Transaction* trans); |
| 370 | 383 |
| 371 // Instantiates and sets QUIC server info factory. | 384 // Instantiates and sets QUIC server info factory. |
| 372 void SetupQuicServerInfoFactory(HttpNetworkSession* session); | 385 void SetupQuicServerInfoFactory(HttpNetworkSession* session); |
| 373 | 386 |
| 374 // Resumes processing the pending list of |entry|. | 387 // Resumes processing the pending list of |entry|. |
| 375 void ProcessPendingQueue(ActiveEntry* entry); | 388 void ProcessPendingQueue(ActiveEntry* entry); |
| 376 | 389 |
| 390 // Called by Transaction to perform an asynchronous revalidation. Creates a |
| 391 // new independent transaction as a copy of the original. |
| 392 void PerformAsyncValidation(const HttpRequestInfo& original_request, |
| 393 const BoundNetLog& net_log); |
| 394 |
| 395 // Remove the AsyncValidation with url |url| from the |async_validations_| set |
| 396 // and delete it. |
| 397 void DeleteAsyncValidation(const std::string& url); |
| 398 |
| 377 // Events (called via PostTask) --------------------------------------------- | 399 // Events (called via PostTask) --------------------------------------------- |
| 378 | 400 |
| 379 void OnProcessPendingQueue(ActiveEntry* entry); | 401 void OnProcessPendingQueue(ActiveEntry* entry); |
| 380 | 402 |
| 381 // Callbacks ---------------------------------------------------------------- | 403 // Callbacks ---------------------------------------------------------------- |
| 382 | 404 |
| 383 // Processes BackendCallback notifications. | 405 // Processes BackendCallback notifications. |
| 384 void OnIOComplete(int result, PendingOp* entry); | 406 void OnIOComplete(int result, PendingOp* entry); |
| 385 | 407 |
| 386 // Helper to conditionally delete |pending_op| if the HttpCache object it | 408 // Helper to conditionally delete |pending_op| if the HttpCache object it |
| (...skipping 11 matching lines...) Expand all Loading... |
| 398 | 420 |
| 399 // Variables ---------------------------------------------------------------- | 421 // Variables ---------------------------------------------------------------- |
| 400 | 422 |
| 401 NetLog* net_log_; | 423 NetLog* net_log_; |
| 402 | 424 |
| 403 // Used when lazily constructing the disk_cache_. | 425 // Used when lazily constructing the disk_cache_. |
| 404 scoped_ptr<BackendFactory> backend_factory_; | 426 scoped_ptr<BackendFactory> backend_factory_; |
| 405 bool building_backend_; | 427 bool building_backend_; |
| 406 bool bypass_lock_for_test_; | 428 bool bypass_lock_for_test_; |
| 407 | 429 |
| 430 // true if the implementation of Cache-Control: stale-while-revalidate |
| 431 // directive is enabled (either via command-line flag or experiment). |
| 432 bool use_stale_while_revalidate_; |
| 433 |
| 408 Mode mode_; | 434 Mode mode_; |
| 409 | 435 |
| 410 scoped_ptr<QuicServerInfoFactoryAdaptor> quic_server_info_factory_; | 436 scoped_ptr<QuicServerInfoFactoryAdaptor> quic_server_info_factory_; |
| 411 | 437 |
| 412 scoped_ptr<HttpTransactionFactory> network_layer_; | 438 scoped_ptr<HttpTransactionFactory> network_layer_; |
| 413 | 439 |
| 414 scoped_ptr<disk_cache::Backend> disk_cache_; | 440 scoped_ptr<disk_cache::Backend> disk_cache_; |
| 415 | 441 |
| 416 scoped_ptr<DiskBasedCertCache> cert_cache_; | 442 scoped_ptr<DiskBasedCertCache> cert_cache_; |
| 417 | 443 |
| 418 // The set of active entries indexed by cache key. | 444 // The set of active entries indexed by cache key. |
| 419 ActiveEntriesMap active_entries_; | 445 ActiveEntriesMap active_entries_; |
| 420 | 446 |
| 421 // The set of doomed entries. | 447 // The set of doomed entries. |
| 422 ActiveEntriesSet doomed_entries_; | 448 ActiveEntriesSet doomed_entries_; |
| 423 | 449 |
| 424 // The set of entries "under construction". | 450 // The set of entries "under construction". |
| 425 PendingOpsMap pending_ops_; | 451 PendingOpsMap pending_ops_; |
| 426 | 452 |
| 427 scoped_ptr<PlaybackCacheMap> playback_cache_map_; | 453 scoped_ptr<PlaybackCacheMap> playback_cache_map_; |
| 428 | 454 |
| 455 // The async validations currently in progress, keyed by URL. |
| 456 AsyncValidationMap async_validations_; |
| 457 |
| 429 base::WeakPtrFactory<HttpCache> weak_factory_; | 458 base::WeakPtrFactory<HttpCache> weak_factory_; |
| 430 | 459 |
| 431 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 460 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
| 432 }; | 461 }; |
| 433 | 462 |
| 434 } // namespace net | 463 } // namespace net |
| 435 | 464 |
| 436 #endif // NET_HTTP_HTTP_CACHE_H_ | 465 #endif // NET_HTTP_HTTP_CACHE_H_ |
| OLD | NEW |