| 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. |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 }; | 237 }; |
| 238 | 238 |
| 239 class MetadataWriter; | 239 class MetadataWriter; |
| 240 class QuicServerInfoFactoryAdaptor; | 240 class QuicServerInfoFactoryAdaptor; |
| 241 class Transaction; | 241 class Transaction; |
| 242 class WorkItem; | 242 class WorkItem; |
| 243 friend class Transaction; | 243 friend class Transaction; |
| 244 friend class ViewCacheHelper; | 244 friend class ViewCacheHelper; |
| 245 struct PendingOp; // Info for an entry under construction. | 245 struct PendingOp; // Info for an entry under construction. |
| 246 | 246 |
| 247 // To help with testing. | |
| 248 friend class MockHttpCache; | |
| 249 | |
| 250 using TransactionList = std::list<Transaction*>; | 247 using TransactionList = std::list<Transaction*>; |
| 251 using TransactionSet = std::unordered_set<Transaction*>; | 248 using TransactionSet = std::unordered_set<Transaction*>; |
| 252 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; | 249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; |
| 253 | 250 |
| 254 // We implement a basic reader/writer lock for the disk cache entry. If there | |
| 255 // is a writer, then all transactions must wait to read the body. But the | |
| 256 // waiting transactions can start their headers phase in parallel. Headers | |
| 257 // phase is allowed for one transaction at a time so that if it doesn't match | |
| 258 // the existing headers, remaining transactions do not also try to match the | |
| 259 // existing entry in parallel leading to wasted network requests. If the | |
| 260 // headers do not match, this entry will be doomed. | |
| 261 // | |
| 262 // A transaction goes through these state transitions. | |
| 263 // | |
| 264 // Write mode transactions: | |
| 265 // add_to_entry_queue-> headers_transaction -> writer | |
| 266 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> readers | |
| 267 // (once the data is written to the cache by another writer) | |
| 268 // | |
| 269 // Read only transactions: | |
| 270 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> readers | |
| 271 // (once the data is written to the cache by the writer) | |
| 272 | |
| 273 struct ActiveEntry { | 251 struct ActiveEntry { |
| 274 explicit ActiveEntry(disk_cache::Entry* entry); | 252 explicit ActiveEntry(disk_cache::Entry* entry); |
| 275 ~ActiveEntry(); | 253 ~ActiveEntry(); |
| 276 size_t EstimateMemoryUsage() const; | 254 size_t EstimateMemoryUsage() const; |
| 277 | 255 |
| 278 // Returns true if no transactions are associated with this entry. | 256 // Returns true if no transactions are associated with this entry. |
| 279 bool HasNoTransactions(); | 257 bool HasNoTransactions(); |
| 280 | 258 |
| 281 // Returns true if no active readers/writer transactions are associated | 259 disk_cache::Entry* disk_entry; |
| 282 // with this entry. | 260 Transaction* writer; |
| 283 bool HasNoActiveTransactions(); | |
| 284 | |
| 285 disk_cache::Entry* disk_entry = nullptr; | |
| 286 | |
| 287 // Transactions waiting to be added to entry. | |
| 288 TransactionList add_to_entry_queue; | |
| 289 | |
| 290 // Transaction currently in the headers phase, either validating the | |
| 291 // response or getting new headers. This can exist simultaneously with | |
| 292 // writer or readers while validating existing headers. | |
| 293 Transaction* headers_transaction = nullptr; | |
| 294 | |
| 295 // Transactions that have completed their headers phase and are waiting | |
| 296 // to read the response body or write the response body. | |
| 297 TransactionList done_headers_queue; | |
| 298 | |
| 299 // Transaction currently reading from the network and writing to the cache. | |
| 300 Transaction* writer = nullptr; | |
| 301 | |
| 302 // Transactions that can only read from the cache. Only one of writer or | |
| 303 // readers can exist at a time. | |
| 304 TransactionSet readers; | 261 TransactionSet readers; |
| 305 | 262 TransactionList pending_queue; |
| 306 // The following variables are true if OnProcessQueuedTransactions is posted | 263 bool will_process_pending_queue; |
| 307 bool will_process_queued_transactions = false; | 264 bool doomed; |
| 308 | |
| 309 // True if entry is doomed. | |
| 310 bool doomed = false; | |
| 311 }; | 265 }; |
| 312 | 266 |
| 313 using ActiveEntriesMap = | 267 using ActiveEntriesMap = |
| 314 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; | 268 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; |
| 315 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; | 269 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; |
| 316 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; | 270 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; |
| 317 using PlaybackCacheMap = std::unordered_map<std::string, int>; | 271 using PlaybackCacheMap = std::unordered_map<std::string, int>; |
| 318 | 272 |
| 319 // Methods ------------------------------------------------------------------ | 273 // Methods ------------------------------------------------------------------ |
| 320 | 274 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 | 335 |
| 382 // Creates the disk cache entry associated with |key|, returning an | 336 // Creates the disk cache entry associated with |key|, returning an |
| 383 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if | 337 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if |
| 384 // this method returns ERR_IO_PENDING. | 338 // this method returns ERR_IO_PENDING. |
| 385 int CreateEntry(const std::string& key, ActiveEntry** entry, | 339 int CreateEntry(const std::string& key, ActiveEntry** entry, |
| 386 Transaction* trans); | 340 Transaction* trans); |
| 387 | 341 |
| 388 // Destroys an ActiveEntry (active or doomed). | 342 // Destroys an ActiveEntry (active or doomed). |
| 389 void DestroyEntry(ActiveEntry* entry); | 343 void DestroyEntry(ActiveEntry* entry); |
| 390 | 344 |
| 391 // Adds a transaction to an ActiveEntry. This method returns ERR_IO_PENDING | 345 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING |
| 392 // and the transaction will be notified about completion via its IO callback. | 346 // the transaction will be notified about completion via its IO callback. This |
| 393 // In a failure case, the callback will be invoked with ERR_CACHE_RACE. | 347 // method returns ERR_CACHE_RACE to signal the transaction that it cannot be |
| 394 int AddTransactionToEntry(ActiveEntry* entry, Transaction* transaction); | 348 // added to the provided entry, and it should retry the process with another |
| 395 | 349 // one (in this case, the entry is no longer valid). |
| 396 // Transaction invokes this when its response headers phase is complete | 350 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); |
| 397 // If the transaction is responsible for writing the response body, | |
| 398 // it becomes the writer and returns OK. In other cases ERR_IO_PENDING is | |
| 399 // returned and the transaction will be notified about completion via its | |
| 400 // IO callback. In a failure case, the callback will be invoked with | |
| 401 // ERR_CACHE_RACE. | |
| 402 int DoneWithResponseHeaders(ActiveEntry* entry, Transaction* transaction); | |
| 403 | 351 |
| 404 // Called when the transaction has finished working with this entry. |cancel| | 352 // Called when the transaction has finished working with this entry. |cancel| |
| 405 // is true if the operation was cancelled by the caller instead of running | 353 // is true if the operation was cancelled by the caller instead of running |
| 406 // to completion. | 354 // to completion. |
| 407 void DoneWithEntry(ActiveEntry* entry, Transaction* transaction, bool cancel); | 355 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); |
| 408 | 356 |
| 409 // Called when the transaction has finished writing to this entry. |success| | 357 // Called when the transaction has finished writing to this entry. |success| |
| 410 // is false if the cache entry should be deleted. | 358 // is false if the cache entry should be deleted. |
| 411 void DoneWritingToEntry(ActiveEntry* entry, | 359 void DoneWritingToEntry(ActiveEntry* entry, bool success); |
| 412 bool success, | |
| 413 Transaction* transaction); | |
| 414 | 360 |
| 415 // Called when the transaction has finished reading from this entry. | 361 // Called when the transaction has finished reading from this entry. |
| 416 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* transaction); | 362 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); |
| 417 | 363 |
| 418 // Removes and returns all queued transactions in |entry| in FIFO order. This | 364 // Converts the active writer transaction to a reader so that other |
| 419 // includes transactions that have completed the headers phase and those that | 365 // transactions can start reading from this entry. |
| 420 // have not been added to the entry yet in that order. |list| is the output | 366 void ConvertWriterToReader(ActiveEntry* entry); |
| 421 // argument. | |
| 422 void RemoveAllQueuedTransactions(ActiveEntry* entry, TransactionList* list); | |
| 423 | |
| 424 // Processes either writer's failure to write response body or | |
| 425 // headers_transactions's failure to write headers. Also invoked when headers | |
| 426 // transaction's validation result is not a match. | |
| 427 void ProcessEntryFailure(ActiveEntry* entry); | |
| 428 | |
| 429 // Resumes processing the queued transactions of |entry|. | |
| 430 void ProcessQueuedTransactions(ActiveEntry* entry); | |
| 431 | |
| 432 // Checks if a transaction can be added to the entry. If yes, it will | |
| 433 // invoke the IO callback of the transaction. This is a helper function for | |
| 434 // OnProcessQueuedTransactions. It will take a transaction from | |
| 435 // add_to_entry_queue and make it a headers_transaction, if one doesn't exist | |
| 436 // already. | |
| 437 void ProcessAddToEntryQueue(ActiveEntry* entry); | |
| 438 | |
| 439 // Invoked when a transaction that has already completed the response headers | |
| 440 // phase can resume reading/writing the response body. It will invoke the IO | |
| 441 // callback of the transaction. This is a helper function for | |
| 442 // OnProcessQueuedTransactions. | |
| 443 void ProcessDoneHeadersQueue(ActiveEntry* entry); | |
| 444 | |
| 445 // Returns true if this transaction can write headers to the entry. | |
| 446 bool CanTransactionWriteResponseHeaders(ActiveEntry* entry, | |
| 447 Transaction* transaction, | |
| 448 bool is_match) const; | |
| 449 | |
| 450 // Returns true if |transaction| is about to start writing response body or | |
| 451 // already started but not yet finished. | |
| 452 bool IsTransactionWritingIncomplete(ActiveEntry* entry, | |
| 453 Transaction* transaction, | |
| 454 const std::string& method) const; | |
| 455 | |
| 456 // Returns true if a transaction is currently writing the response body. | |
| 457 bool IsWritingInProgress(ActiveEntry* entry) const; | |
| 458 | 367 |
| 459 // Returns the LoadState of the provided pending transaction. | 368 // Returns the LoadState of the provided pending transaction. |
| 460 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); | 369 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); |
| 461 | 370 |
| 462 // Removes the transaction |trans|, from the pending list of an entry | 371 // Removes the transaction |trans|, from the pending list of an entry |
| 463 // (PendingOp, active or doomed entry). | 372 // (PendingOp, active or doomed entry). |
| 464 void RemovePendingTransaction(Transaction* trans); | 373 void RemovePendingTransaction(Transaction* trans); |
| 465 | 374 |
| 466 // Removes the transaction |trans|, from the pending list of |entry|. | 375 // Removes the transaction |trans|, from the pending list of |entry|. |
| 467 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, | 376 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, |
| 468 Transaction* trans); | 377 Transaction* trans); |
| 469 | 378 |
| 470 // Removes the transaction |trans|, from the pending list of |pending_op|. | 379 // Removes the transaction |trans|, from the pending list of |pending_op|. |
| 471 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, | 380 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, |
| 472 Transaction* trans); | 381 Transaction* trans); |
| 382 // Resumes processing the pending list of |entry|. |
| 383 void ProcessPendingQueue(ActiveEntry* entry); |
| 473 | 384 |
| 474 // Events (called via PostTask) --------------------------------------------- | 385 // Events (called via PostTask) --------------------------------------------- |
| 475 | 386 |
| 476 void OnProcessQueuedTransactions(ActiveEntry* entry); | 387 void OnProcessPendingQueue(ActiveEntry* entry); |
| 477 | 388 |
| 478 // Callbacks ---------------------------------------------------------------- | 389 // Callbacks ---------------------------------------------------------------- |
| 479 | 390 |
| 480 // Processes BackendCallback notifications. | 391 // Processes BackendCallback notifications. |
| 481 void OnIOComplete(int result, PendingOp* entry); | 392 void OnIOComplete(int result, PendingOp* entry); |
| 482 | 393 |
| 483 // Helper to conditionally delete |pending_op| if the HttpCache object it | 394 // Helper to conditionally delete |pending_op| if the HttpCache object it |
| 484 // is meant for has been deleted. | 395 // is meant for has been deleted. |
| 485 // | 396 // |
| 486 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might | 397 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 std::unique_ptr<base::Clock> clock_; | 435 std::unique_ptr<base::Clock> clock_; |
| 525 | 436 |
| 526 base::WeakPtrFactory<HttpCache> weak_factory_; | 437 base::WeakPtrFactory<HttpCache> weak_factory_; |
| 527 | 438 |
| 528 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 439 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
| 529 }; | 440 }; |
| 530 | 441 |
| 531 } // namespace net | 442 } // namespace net |
| 532 | 443 |
| 533 #endif // NET_HTTP_HTTP_CACHE_H_ | 444 #endif // NET_HTTP_HTTP_CACHE_H_ |
| OLD | NEW |