Chromium Code Reviews| 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> | 18 #include <map> |
| 19 #include <memory> | 19 #include <memory> |
| 20 #include <string> | 20 #include <string> |
| 21 #include <unordered_map> | 21 #include <unordered_map> |
| 22 #include <unordered_set> | |
| 23 #include <utility> | |
|
Randy Smith (Not in Mondays)
2017/01/19 00:53:42
What is this include for? I think of <utility> as
shivanisha
2017/01/19 21:13:06
Included this as per git cl lint. I agree it's not
Randy Smith (Not in Mondays)
2017/01/20 19:54:31
Weird. No, I don't think it's overriding an autom
shivanisha
2017/01/25 19:46:12
Removed the include.
| |
| 22 | 24 |
| 23 #include "base/files/file_path.h" | 25 #include "base/files/file_path.h" |
| 24 #include "base/macros.h" | 26 #include "base/macros.h" |
| 25 #include "base/memory/weak_ptr.h" | 27 #include "base/memory/weak_ptr.h" |
| 26 #include "base/threading/non_thread_safe.h" | 28 #include "base/threading/non_thread_safe.h" |
| 27 #include "base/time/clock.h" | 29 #include "base/time/clock.h" |
| 28 #include "base/time/time.h" | 30 #include "base/time/time.h" |
| 29 #include "net/base/cache_type.h" | 31 #include "net/base/cache_type.h" |
| 30 #include "net/base/completion_callback.h" | 32 #include "net/base/completion_callback.h" |
| 31 #include "net/base/load_states.h" | 33 #include "net/base/load_states.h" |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 } | 197 } |
| 196 | 198 |
| 197 // Causes all transactions created after this point to generate a failure | 199 // Causes all transactions created after this point to generate a failure |
| 198 // when attempting to conditionalize a network request. | 200 // when attempting to conditionalize a network request. |
| 199 void FailConditionalizationForTest() { | 201 void FailConditionalizationForTest() { |
| 200 fail_conditionalization_for_test_ = true; | 202 fail_conditionalization_for_test_ = true; |
| 201 } | 203 } |
| 202 | 204 |
| 203 // HttpTransactionFactory implementation: | 205 // HttpTransactionFactory implementation: |
| 204 int CreateTransaction(RequestPriority priority, | 206 int CreateTransaction(RequestPriority priority, |
| 205 std::unique_ptr<HttpTransaction>* trans) override; | 207 std::unique_ptr<HttpTransaction>* transaction) override; |
| 206 HttpCache* GetCache() override; | 208 HttpCache* GetCache() override; |
| 207 HttpNetworkSession* GetSession() override; | 209 HttpNetworkSession* GetSession() override; |
| 208 | 210 |
| 209 base::WeakPtr<HttpCache> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } | 211 base::WeakPtr<HttpCache> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } |
| 210 | 212 |
| 211 // Resets the network layer to allow for tests that probe | 213 // Resets the network layer to allow for tests that probe |
| 212 // network changes (e.g. host unreachable). The old network layer is | 214 // network changes (e.g. host unreachable). The old network layer is |
| 213 // returned to allow for filter patterns that only intercept | 215 // returned to allow for filter patterns that only intercept |
| 214 // some creation requests. Note ownership exchange. | 216 // some creation requests. Note ownership exchange. |
| 215 std::unique_ptr<HttpTransactionFactory> | 217 std::unique_ptr<HttpTransactionFactory> |
| 216 SetHttpNetworkTransactionFactoryForTesting( | 218 SetHttpNetworkTransactionFactoryForTesting( |
| 217 std::unique_ptr<HttpTransactionFactory> new_network_layer); | 219 std::unique_ptr<HttpTransactionFactory> new_network_layer); |
| 218 | 220 |
| 219 private: | 221 private: |
| 220 // Types -------------------------------------------------------------------- | 222 // Types -------------------------------------------------------------------- |
| 221 | 223 |
| 222 // Disk cache entry data indices. | 224 // Disk cache entry data indices. |
| 223 enum { | 225 enum { |
| 224 kResponseInfoIndex = 0, | 226 kResponseInfoIndex = 0, |
| 225 kResponseContentIndex, | 227 kResponseContentIndex, |
| 226 kMetadataIndex, | 228 kMetadataIndex, |
| 227 | 229 |
| 228 // Must remain at the end of the enum. | 230 // Must remain at the end of the enum. |
| 229 kNumCacheEntryDataIndices | 231 kNumCacheEntryDataIndices |
| 230 }; | 232 }; |
| 231 | 233 |
| 234 class DataAccess; | |
| 235 class SharedWriters; | |
| 236 class Transaction; | |
| 237 typedef std::list<Transaction*> TransactionList; | |
| 238 typedef std::unordered_set<Transaction*> TransactionSet; | |
| 232 class MetadataWriter; | 239 class MetadataWriter; |
| 233 class QuicServerInfoFactoryAdaptor; | 240 class QuicServerInfoFactoryAdaptor; |
| 234 class Transaction; | |
| 235 class WorkItem; | 241 class WorkItem; |
| 236 friend class Transaction; | |
| 237 friend class ViewCacheHelper; | 242 friend class ViewCacheHelper; |
| 238 struct PendingOp; // Info for an entry under construction. | 243 struct PendingOp; // Info for an entry under construction. |
| 239 | 244 |
| 240 typedef std::list<Transaction*> TransactionList; | |
| 241 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; | 245 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; |
| 242 | 246 |
| 243 struct ActiveEntry { | 247 struct ActiveEntry { |
| 244 explicit ActiveEntry(disk_cache::Entry* entry); | 248 explicit ActiveEntry(disk_cache::Entry* entry); |
| 245 ~ActiveEntry(); | 249 ~ActiveEntry(); |
| 246 | 250 |
| 247 disk_cache::Entry* disk_entry; | 251 disk_cache::Entry* disk_entry; |
| 252 // Either writer or shared_writers will be non-null at any point in time. | |
| 248 Transaction* writer; | 253 Transaction* writer; |
| 249 TransactionList readers; | 254 std::unique_ptr<SharedWriters> shared_writers; |
| 255 TransactionSet readers; | |
| 250 TransactionList pending_queue; | 256 TransactionList pending_queue; |
| 251 bool will_process_pending_queue; | 257 bool will_process_pending_queue; |
| 252 bool doomed; | 258 bool doomed; |
| 253 }; | 259 }; |
| 254 | 260 |
| 255 using ActiveEntriesMap = | 261 using ActiveEntriesMap = |
| 256 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; | 262 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; |
| 257 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; | 263 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; |
| 258 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; | 264 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; |
| 259 using PlaybackCacheMap = std::unordered_map<std::string, int>; | 265 using PlaybackCacheMap = std::unordered_map<std::string, int>; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 // Closes a previously doomed entry. | 301 // Closes a previously doomed entry. |
| 296 void FinalizeDoomedEntry(ActiveEntry* entry); | 302 void FinalizeDoomedEntry(ActiveEntry* entry); |
| 297 | 303 |
| 298 // Returns an entry that is currently in use and not doomed, or NULL. | 304 // Returns an entry that is currently in use and not doomed, or NULL. |
| 299 ActiveEntry* FindActiveEntry(const std::string& key); | 305 ActiveEntry* FindActiveEntry(const std::string& key); |
| 300 | 306 |
| 301 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk | 307 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk |
| 302 // cache entry. | 308 // cache entry. |
| 303 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); | 309 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); |
| 304 | 310 |
| 305 // Deletes an ActiveEntry. | 311 // Deletes an ActiveEntry. Expects all of the transaction pointers to be |
| 312 // null and containers to be empty: writer, shared_writers, pending_queue | |
| 313 // and readers. | |
| 306 void DeactivateEntry(ActiveEntry* entry); | 314 void DeactivateEntry(ActiveEntry* entry); |
| 307 | 315 |
| 308 // Deletes an ActiveEntry using an exhaustive search. | 316 // Deletes an ActiveEntry using an exhaustive search. |
| 309 void SlowDeactivateEntry(ActiveEntry* entry); | 317 void SlowDeactivateEntry(ActiveEntry* entry); |
| 310 | 318 |
| 311 // Returns the PendingOp for the desired |key|. If an entry is not under | 319 // Returns the PendingOp for the desired |key|. If an entry is not under |
| 312 // construction already, a new PendingOp structure is created. | 320 // construction already, a new PendingOp structure is created. |
| 313 PendingOp* GetPendingOp(const std::string& key); | 321 PendingOp* GetPendingOp(const std::string& key); |
| 314 | 322 |
| 315 // Deletes a PendingOp. | 323 // Deletes a PendingOp. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might | 393 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might |
| 386 // be possible to simplify it using either base::Owned() or base::Passed() | 394 // be possible to simplify it using either base::Owned() or base::Passed() |
| 387 // with the callback. | 395 // with the callback. |
| 388 static void OnPendingOpComplete(const base::WeakPtr<HttpCache>& cache, | 396 static void OnPendingOpComplete(const base::WeakPtr<HttpCache>& cache, |
| 389 PendingOp* pending_op, | 397 PendingOp* pending_op, |
| 390 int result); | 398 int result); |
| 391 | 399 |
| 392 // Processes the backend creation notification. | 400 // Processes the backend creation notification. |
| 393 void OnBackendCreated(int result, PendingOp* pending_op); | 401 void OnBackendCreated(int result, PendingOp* pending_op); |
| 394 | 402 |
| 403 // Destroys the entry and any pending queue transactions' callbacks are | |
| 404 // invoked with ERR_CACHE_RACE that restarts their state machine. | |
| 405 void DestroyEntryAndRestartPendingQueueTransactions(ActiveEntry* entry); | |
| 406 | |
| 407 // Writes response info to entry. | |
| 408 int WriteResponseInfo(ActiveEntry* entry, | |
| 409 const HttpResponseInfo* response, | |
| 410 CompletionCallback& callback, | |
| 411 bool truncated, | |
| 412 int* io_buf_len); | |
| 413 | |
| 414 // Checks if the response is completed based on content length. | |
| 415 bool IsResponseCompleted(const ActiveEntry* entry, | |
| 416 const HttpResponseInfo* response); | |
| 417 | |
| 418 // Returns true if we should bother attempting to resume this request if it is | |
| 419 // aborted while in progress. If |has_data| is true, the size of the stored | |
| 420 // data is considered for the result. | |
| 421 bool CanResumeEntry(bool has_data, | |
| 422 const std::string& method, | |
| 423 const HttpResponseInfo* response, | |
| 424 ActiveEntry* entry); | |
| 425 | |
| 426 // Creates a SharedWriters object. The ownership of the network_transaction is | |
| 427 // transferred to a new DataAccess object owned by the SharedWriters object. | |
| 428 void CreateSharedWriters(Transaction* cacheTransaction, | |
| 429 std::unique_ptr<HttpTransaction> network_transaction, | |
| 430 RequestPriority priority); | |
| 431 | |
| 432 // Shared Writing scenarios requiring processing of transactions in the entry | |
| 433 // or removal of entry. | |
| 434 // | |
| 435 // These are invoked from SharedWriters IO callback and will lead to shared | |
| 436 // writers' ownership being released from entry which will be deleted at the | |
| 437 // end of the callback processing by the SharedWriters object itself. | |
| 438 // | |
|
Randy Smith (Not in Mondays)
2017/01/19 00:53:42
To me, it would make it more clear that these comm
shivanisha
2017/01/25 19:46:12
N/A due to recent changes.
| |
| 439 // Response data completely written to the cache successfully. | |
| 440 void ResponseCompleteSharedWriters(ActiveEntry* entry); | |
| 441 | |
| 442 // Response data could not be written to the cache successfully, or | |
| 443 // Response data could not be read from the network successfully. | |
| 444 // Success is true if the entry was marked as truncated successfully. | |
| 445 void ResponseFailedSharedWriters(bool success, ActiveEntry* entry); | |
| 446 | |
| 447 // Resets or releases if entry contains an empty SharedWriters object and | |
| 448 // calls ProcessPendingQueue. It will release if SharedWriters is about to | |
| 449 // delete itself at the end of the flow, else resets. | |
| 450 void ResetSharedWritersProcessPendingQueue(ActiveEntry* entry); | |
| 451 | |
| 452 // These are invoked from HttpCache::Transaction and might lead to shared | |
| 453 // writers being destroyed. | |
| 454 // | |
| 455 // DoneReading() API invoked on the transaction. | |
| 456 void DoneReadingSharedWriters(Transaction* transaction, ActiveEntry* entry); | |
| 457 | |
| 458 // StopCaching() API invoked on the transaction. | |
| 459 void StopCachingSharedWriters(Transaction* transaction, ActiveEntry* entry); | |
| 460 | |
| 461 // Validating transaction receives a non-304 response. | |
| 462 std::unique_ptr<HttpTransaction> ValidationNoMatchSharedWriters( | |
| 463 const std::string& key, | |
| 464 Transaction* transaction, | |
| 465 std::unique_ptr<HttpTransaction> network_transaction, | |
| 466 RequestPriority priority, | |
| 467 ActiveEntry* entry); | |
| 468 | |
| 469 // Current Writer is destroyed by its consumer. | |
| 470 void RemoveCurrentSharedWriter(Transaction* transaction, ActiveEntry* entry); | |
| 471 | |
| 472 // An idle writer is destroyed by its consumer. | |
| 473 void RemoveIdleSharedWriter(Transaction* transaction, ActiveEntry* entry); | |
| 474 | |
| 475 // Validating transaction is destroyed by its consumer. | |
| 476 void RemoveValidatingTransSharedWriters(Transaction* transaction, | |
| 477 ActiveEntry* entry); | |
| 478 | |
| 479 // Invokes the io callback of the transaction with the result, if the | |
| 480 // transaction is still alive. | |
| 481 void NotifyTransaction(base::WeakPtr<Transaction> transaction, int result); | |
| 482 | |
| 483 // Reset SharedWriters except in the scenario given here: SharedWriters may | |
| 484 // be in a flow where it is meant to destroy itself at the end of the flow | |
| 485 // and we are here as a result of a callback. Do not reset in that case and | |
| 486 // just release the ownership of SharedWriters. | |
| 487 void ResetOrReleaseSharedWriters(ActiveEntry* entry); | |
| 488 | |
| 395 // Variables ---------------------------------------------------------------- | 489 // Variables ---------------------------------------------------------------- |
| 396 | 490 |
| 397 NetLog* net_log_; | 491 NetLog* net_log_; |
| 398 | 492 |
| 399 // Used when lazily constructing the disk_cache_. | 493 // Used when lazily constructing the disk_cache_. |
| 400 std::unique_ptr<BackendFactory> backend_factory_; | 494 std::unique_ptr<BackendFactory> backend_factory_; |
| 401 bool building_backend_; | 495 bool building_backend_; |
| 402 bool bypass_lock_for_test_; | 496 bool bypass_lock_for_test_; |
| 403 bool fail_conditionalization_for_test_; | 497 bool fail_conditionalization_for_test_; |
| 404 | 498 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 423 std::unique_ptr<base::Clock> clock_; | 517 std::unique_ptr<base::Clock> clock_; |
| 424 | 518 |
| 425 base::WeakPtrFactory<HttpCache> weak_factory_; | 519 base::WeakPtrFactory<HttpCache> weak_factory_; |
| 426 | 520 |
| 427 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 521 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
| 428 }; | 522 }; |
| 429 | 523 |
| 430 } // namespace net | 524 } // namespace net |
| 431 | 525 |
| 432 #endif // NET_HTTP_HTTP_CACHE_H_ | 526 #endif // NET_HTTP_HTTP_CACHE_H_ |
| OLD | NEW |