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