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 // Returns true if the given transaction is a reader. | |
264 bool IsReader(Transaction* transaction); | |
265 | |
266 disk_cache::Entry* disk_entry = nullptr; | |
267 | |
268 // The following variables represent the various transactions associated | |
269 // with this entry in different stages. A transaction goes through these | |
270 // transitions. [] signifies an optional stage. | |
271 // | |
272 // Write mode transactions: | |
273 // [add_to_entry_queue]-> validating_transaction & writer -> writer | |
274 // [add_to_entry_queue]-> validating_transaction -> validated_queue -> | |
275 // readers (once the data is written to the cache by another writer) | |
276 // [add_to_entry_queue]-> validating_transaction -> writer | |
277 // (if the original writer could not write to the cache because of | |
278 // failure/cancellation) | |
279 // | |
280 // Read only transactions: | |
281 // [add_to_entry_queue]-> readers (once the data is written to the cache by | |
282 // the writer) | |
jkarlin
2017/03/09 17:42:56
How about the following design for the progression
| |
283 | |
284 // Transactions waiting to be added to entry. | |
285 TransactionList add_to_entry_queue; | |
286 | |
287 // Transaction currently validating the response. This can exist | |
288 // simultaneously with writer or readers. | |
289 Transaction* validating_transaction = nullptr; | |
290 | |
291 // Transactions that have completed their validation phase and are waiting | |
292 // to read the response body. | |
293 TransactionList validated_queue; | |
294 | |
295 // Transaction currently reading from the network and writing to the cache. | |
296 Transaction* writer = nullptr; | |
297 | |
298 // Transactions that can only read from the cache. Only one of writer or | |
299 // readers can exist at a time. | |
261 TransactionSet readers; | 300 TransactionSet readers; |
262 TransactionList pending_queue; | 301 |
263 bool will_process_pending_queue; | 302 // The following variables are true if OnProcessWaitingTransactions or |
264 bool doomed; | 303 // OnProcessParallelValidation is set to true. |
304 bool will_process_waiting_transactions = false; | |
305 bool will_process_parallel_validation = 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 28 matching lines...) Expand all Loading... | |
303 | 347 |
304 // Dooms the entry associated with a GET for a given |url|. | 348 // Dooms the entry associated with a GET for a given |url|. |
305 void DoomMainEntryForUrl(const GURL& url); | 349 void DoomMainEntryForUrl(const GURL& url); |
306 | 350 |
307 // Closes a previously doomed entry. | 351 // Closes a previously doomed entry. |
308 void FinalizeDoomedEntry(ActiveEntry* entry); | 352 void FinalizeDoomedEntry(ActiveEntry* entry); |
309 | 353 |
310 // Returns an entry that is currently in use and not doomed, or NULL. | 354 // Returns an entry that is currently in use and not doomed, or NULL. |
311 ActiveEntry* FindActiveEntry(const std::string& key); | 355 ActiveEntry* FindActiveEntry(const std::string& key); |
312 | 356 |
357 // Returns true if given entry is still alive, either as an active or doomed | |
358 // entry. | |
359 bool IsEntryAlive(const std::string& key, ActiveEntry* entry); | |
360 | |
313 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk | 361 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk |
314 // cache entry. | 362 // cache entry. |
315 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); | 363 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); |
316 | 364 |
317 // Deletes an ActiveEntry. | 365 // Deletes an ActiveEntry. |
318 void DeactivateEntry(ActiveEntry* entry); | 366 void DeactivateEntry(ActiveEntry* entry); |
319 | 367 |
320 // Deletes an ActiveEntry using an exhaustive search. | 368 // Deletes an ActiveEntry using an exhaustive search. |
321 void SlowDeactivateEntry(ActiveEntry* entry); | 369 void SlowDeactivateEntry(ActiveEntry* entry); |
322 | 370 |
(...skipping 13 matching lines...) Expand all Loading... | |
336 // Creates the disk cache entry associated with |key|, returning an | 384 // Creates the disk cache entry associated with |key|, returning an |
337 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if | 385 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if |
338 // this method returns ERR_IO_PENDING. | 386 // this method returns ERR_IO_PENDING. |
339 int CreateEntry(const std::string& key, ActiveEntry** entry, | 387 int CreateEntry(const std::string& key, ActiveEntry** entry, |
340 Transaction* trans); | 388 Transaction* trans); |
341 | 389 |
342 // Destroys an ActiveEntry (active or doomed). | 390 // Destroys an ActiveEntry (active or doomed). |
343 void DestroyEntry(ActiveEntry* entry); | 391 void DestroyEntry(ActiveEntry* entry); |
344 | 392 |
345 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING | 393 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING |
346 // the transaction will be notified about completion via its IO callback. This | 394 // 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 | 395 |
348 // added to the provided entry, and it should retry the process with another | 396 int AddTransactionToEntry(ActiveEntry* entry, Transaction* transaction); |
349 // one (in this case, the entry is no longer valid). | 397 int AddTransactionToEntryImpl(ActiveEntry* entry, Transaction* transaction); |
350 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); | 398 int AddTransactionToEntryForRead(ActiveEntry* entry, |
399 Transaction* transaction); | |
400 int AddTransactionToEntryForWrite(ActiveEntry* entry, | |
401 Transaction* transaction); | |
jkarlin
2017/03/09 17:42:57
The above 3 methods don't need to be class methods
shivanisha
2017/03/14 17:34:45
N/A after recent refactoring.
| |
402 | |
403 // Transaction invokes this when its validation matches the entry so another | |
404 // transaction can now start validation. is_match should be passed | |
405 // as true if the validation was a match. | |
406 // If its not a match, this entry is doomed and pending transactions are | |
407 // restarted. Returns OK. | |
408 // If it is a match, either the transaction becomes a reader if the entry is | |
409 // already written to the cache and returns OK or the transaction is added to | |
410 // a queue if response is being written to the cache by another transaction | |
411 // and returns ERR_IO_PENDING. | |
412 int DoneValidationWithEntry(ActiveEntry* entry, | |
413 Transaction* transaction, | |
414 bool is_match); | |
415 | |
416 // Transaction invokes this when it has completed validation and it is | |
417 // proceeding to start writing the response body to the cache. This is done so | |
418 // that another waiting transaction can start validating the headers in | |
419 // parallel. | |
420 void DoneValidationWriteEntry(ActiveEntry* entry, Transaction* transaction); | |
jkarlin
2017/03/09 17:42:56
So at this point the Writer is done with the respo
shivanisha
2017/03/14 17:34:45
Renamed to DoneResponseHeaders because in case of
| |
351 | 421 |
352 // Called when the transaction has finished working with this entry. |cancel| | 422 // 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 | 423 // is true if the operation was cancelled by the caller instead of running |
354 // to completion. | 424 // to completion. |
355 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); | 425 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); |
356 | 426 |
357 // Called when the transaction has finished writing to this entry. |success| | 427 // Called when the transaction has finished writing to this entry. |success| |
358 // is false if the cache entry should be deleted. | 428 // is false if the cache entry should be deleted. |
359 void DoneWritingToEntry(ActiveEntry* entry, bool success); | 429 void DoneWritingToEntry(ActiveEntry* entry, |
430 bool success, | |
431 Transaction* transaction); | |
432 | |
433 // Processes other transactions when a transaction is done writing to the | |
434 // entry, based on success or failure of this transaction. | |
435 void DoneWritingToEntryProcessOtherTransactions(ActiveEntry* entry, | |
jkarlin
2017/03/09 17:42:56
Should be in anonymous namespace?
shivanisha
2017/03/14 17:34:45
This function no longer exists
| |
436 bool success); | |
360 | 437 |
361 // Called when the transaction has finished reading from this entry. | 438 // Called when the transaction has finished reading from this entry. |
362 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); | 439 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* transaction); |
363 | 440 |
364 // Converts the active writer transaction to a reader so that other | 441 // Converts the active writer/validating transaction to a reader so that other |
365 // transactions can start reading from this entry. | 442 // transactions can start reading from this entry. It is invoked in the |
366 void ConvertWriterToReader(ActiveEntry* entry); | 443 // validation phase of a transaction when the current entry is validated. It |
444 // needs to differentiate between the scenarios when the entry is already | |
445 // completely written to by another transaction or is currently being written. | |
446 // In the latter case, the transaction cannot become a reader at this time and | |
447 // the function will return false, else it will return true. | |
448 bool ConvertWriterToReader(ActiveEntry* entry, Transaction* transaction); | |
367 | 449 |
368 // Returns the LoadState of the provided pending transaction. | 450 // Returns the LoadState of the provided pending transaction. |
369 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); | 451 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); |
370 | 452 |
371 // Removes the transaction |trans|, from the pending list of an entry | 453 // Removes the transaction |trans|, from the pending list of an entry |
372 // (PendingOp, active or doomed entry). | 454 // (PendingOp, active or doomed entry). |
373 void RemovePendingTransaction(Transaction* trans); | 455 void RemovePendingTransaction(Transaction* trans); |
374 | 456 |
375 // Removes the transaction |trans|, from the pending list of |entry|. | 457 // Removes the transaction |trans|, from the pending list of |entry|. |
376 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, | 458 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, |
377 Transaction* trans); | 459 Transaction* trans); |
378 | 460 |
379 // Removes the transaction |trans|, from the pending list of |pending_op|. | 461 // Removes the transaction |trans|, from the pending list of |pending_op|. |
380 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, | 462 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, |
381 Transaction* trans); | 463 Transaction* trans); |
382 // Resumes processing the pending list of |entry|. | 464 // Resumes processing the pending list of |entry|. This should always be |
383 void ProcessPendingQueue(ActiveEntry* entry); | 465 // called when the response is already written to the cache. |
466 void ProcessWaitingTransactions(ActiveEntry* entry); | |
jkarlin
2017/03/09 17:42:56
Should be in anonymous namespace?
| |
467 | |
468 // Invoked when a transaction can start validating the entry when there may be | |
469 // a writer/readers writing/reading to/from the entry. | |
470 void ProcessParallelValidation(ActiveEntry* entry); | |
jkarlin
2017/03/09 17:42:57
Should be in anonymous namespace?
| |
384 | 471 |
385 // Events (called via PostTask) --------------------------------------------- | 472 // Events (called via PostTask) --------------------------------------------- |
386 | 473 |
387 void OnProcessPendingQueue(ActiveEntry* entry); | 474 void OnProcessWaitingTransactions(const std::string& key, ActiveEntry* entry); |
jkarlin
2017/03/09 17:42:57
Should be in anonymous namespace?
| |
475 | |
476 void OnProcessParallelValidation(const std::string& key, ActiveEntry* entry); | |
jkarlin
2017/03/09 17:42:57
Should be in anonymous namespace?
| |
477 | |
478 // Dooms the entry and restarts all the pending transactions. | |
479 void DoomEntryRestartPendingTransactions(ActiveEntry* entry); | |
480 | |
481 // Destroys the entry and restarts all the pending transactions. | |
482 void DestroyEntryRestartPendingTransactions(ActiveEntry* entry); | |
483 | |
484 // Retrieves all pending transactions from entry in a single list. | |
485 TransactionList GetAllPendingTransactions(ActiveEntry* entry); | |
jkarlin
2017/03/09 17:42:57
Should be in anonymous namespace? Also, why is thi
shivanisha
2017/03/14 17:34:45
Since ActiveEntry is a private structure in HttpCa
| |
486 | |
487 // Helper function for restarting all pending transactions by invoking their | |
488 // IO callbacks with ERR_CACHE_RACE. | |
489 void RestartPendingTransactions(const TransactionList& list); | |
jkarlin
2017/03/09 17:42:57
Should be in anonymous namespace?
shivanisha
2017/03/14 17:34:45
done
| |
388 | 490 |
389 // Callbacks ---------------------------------------------------------------- | 491 // Callbacks ---------------------------------------------------------------- |
390 | 492 |
391 // Processes BackendCallback notifications. | 493 // Processes BackendCallback notifications. |
392 void OnIOComplete(int result, PendingOp* entry); | 494 void OnIOComplete(int result, PendingOp* entry); |
393 | 495 |
394 // Helper to conditionally delete |pending_op| if the HttpCache object it | 496 // Helper to conditionally delete |pending_op| if the HttpCache object it |
395 // is meant for has been deleted. | 497 // is meant for has been deleted. |
396 // | 498 // |
397 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might | 499 // 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_; | 537 std::unique_ptr<base::Clock> clock_; |
436 | 538 |
437 base::WeakPtrFactory<HttpCache> weak_factory_; | 539 base::WeakPtrFactory<HttpCache> weak_factory_; |
438 | 540 |
439 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 541 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
440 }; | 542 }; |
441 | 543 |
442 } // namespace net | 544 } // namespace net |
443 | 545 |
444 #endif // NET_HTTP_HTTP_CACHE_H_ | 546 #endif // NET_HTTP_HTTP_CACHE_H_ |
OLD | NEW |