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