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 | |
247 using TransactionList = std::list<Transaction*>; | 250 using TransactionList = std::list<Transaction*>; |
248 using TransactionSet = std::unordered_set<Transaction*>; | 251 using TransactionSet = std::unordered_set<Transaction*>; |
249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; | 252 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; |
250 | 253 |
251 struct ActiveEntry { | 254 struct ActiveEntry { |
252 explicit ActiveEntry(disk_cache::Entry* entry); | 255 explicit ActiveEntry(disk_cache::Entry* entry); |
253 ~ActiveEntry(); | 256 ~ActiveEntry(); |
254 size_t EstimateMemoryUsage() const; | 257 size_t EstimateMemoryUsage() const; |
255 | 258 |
256 // Returns true if no transactions are associated with this entry. | 259 // Returns true if no transactions are associated with this entry. |
257 bool HasNoTransactions(); | 260 bool HasNoTransactions(); |
258 | 261 |
259 disk_cache::Entry* disk_entry; | 262 // Returns true if no active readers/writer transactions are associated |
260 Transaction* writer; | 263 // with this entry. |
264 bool HasNoActiveTransactions(); | |
265 | |
266 disk_cache::Entry* disk_entry = nullptr; | |
267 | |
268 // We implement a basic reader/writer lock for the disk cache entry. If | |
jkarlin
2017/04/06 17:37:13
Pull this comment up above the definition of the A
shivanisha
2017/04/07 21:11:16
done and revised this comment for read-only transa
| |
269 // there is a writer, then all read-only transactions must wait. Non | |
270 // read-only transactions can proceed to their validation phase. Validation | |
271 // is allowed for one transaction at a time so that we do not end up with | |
272 // wasted network requests. | |
273 | |
274 // The following variables represent the various transactions associated | |
275 // with this entry in different stages. A transaction goes through these | |
276 // transitions. | |
277 // | |
278 // Write mode transactions: | |
279 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> | |
jkarlin
2017/04/06 17:37:13
I'd love to see the state machine description abov
shivanisha
2017/04/07 21:11:16
done
| |
280 // writer | |
281 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> | |
282 // readers (once the data is written to the cache by another writer) | |
283 // | |
284 // Read only transactions: | |
285 // add_to_entry_queue-> readers (once the data is written to the cache by | |
jkarlin
2017/04/06 17:37:13
Why special case read-only transactions? It seems
shivanisha
2017/04/07 21:11:16
done
| |
286 // the writer) | |
287 | |
288 // Transactions waiting to be added to entry. | |
289 TransactionList add_to_entry_queue; | |
290 | |
291 // Transaction currently in the headers phase, either validating the | |
292 // response or getting new headers. This can exist simultaneously with | |
293 // writer or readers while validating existing headers. | |
294 Transaction* headers_transaction = nullptr; | |
295 | |
296 // Transactions that have completed their headers phase and are waiting | |
297 // to read the response body or write the response body. | |
298 TransactionList done_headers_queue; | |
299 | |
300 // Transaction currently reading from the network and writing to the cache. | |
301 Transaction* writer = nullptr; | |
302 | |
303 // Transactions that can only read from the cache. Only one of writer or | |
304 // readers can exist at a time. | |
261 TransactionSet readers; | 305 TransactionSet readers; |
262 TransactionList pending_queue; | 306 |
263 bool will_process_pending_queue; | 307 // The following variables are true if OnProcessQueuedTransactions is posted |
264 bool doomed; | 308 bool will_process_queued_transactions = false; |
309 | |
310 // True if entry is doomed. | |
311 bool doomed = false; | |
265 }; | 312 }; |
266 | 313 |
267 using ActiveEntriesMap = | 314 using ActiveEntriesMap = |
268 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; | 315 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; |
269 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; | 316 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; |
270 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; | 317 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; |
271 using PlaybackCacheMap = std::unordered_map<std::string, int>; | 318 using PlaybackCacheMap = std::unordered_map<std::string, int>; |
272 | 319 |
273 // Methods ------------------------------------------------------------------ | 320 // Methods ------------------------------------------------------------------ |
274 | 321 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 | 382 |
336 // Creates the disk cache entry associated with |key|, returning an | 383 // Creates the disk cache entry associated with |key|, returning an |
337 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if | 384 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if |
338 // this method returns ERR_IO_PENDING. | 385 // this method returns ERR_IO_PENDING. |
339 int CreateEntry(const std::string& key, ActiveEntry** entry, | 386 int CreateEntry(const std::string& key, ActiveEntry** entry, |
340 Transaction* trans); | 387 Transaction* trans); |
341 | 388 |
342 // Destroys an ActiveEntry (active or doomed). | 389 // Destroys an ActiveEntry (active or doomed). |
343 void DestroyEntry(ActiveEntry* entry); | 390 void DestroyEntry(ActiveEntry* entry); |
344 | 391 |
345 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING | 392 // 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 | 393 // and the transaction will be notified about completion via its IO callback. |
jkarlin
2017/04/06 17:37:13
Comment on what error to expect on failure.
shivanisha
2017/04/07 21:11:16
done
| |
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 // either on validating existing headers/skipping validation or getting new |
398 // headers. The transaction is added to a queue and a task is posted | |
399 // to process queued transactions of the entry. Returns ERR_IO_PENDING. | |
jkarlin
2017/04/06 17:37:13
Comment on what error to expect on failure.
jkarlin
2017/04/06 17:37:13
Add "the transaction will be notified about comple
shivanisha
2017/04/07 21:11:16
done
| |
400 int DoneWithResponseHeaders(ActiveEntry* entry, Transaction* transaction); | |
351 | 401 |
352 // Called when the transaction has finished working with this entry. |cancel| | 402 // 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 | 403 // is true if the operation was cancelled by the caller instead of running |
354 // to completion. | 404 // to completion. |
355 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); | 405 void DoneWithEntry(ActiveEntry* entry, Transaction* transaction, bool cancel); |
406 | |
407 // Returns true if this transaction can write headers to the entry. | |
408 bool CanTransactionWriteResponseHeaders(ActiveEntry* entry, | |
409 Transaction* transaction, | |
410 int response_code) const; | |
411 | |
412 // Returns true if this transaction is currently writing or about to start | |
413 // writing the response body. | |
jkarlin
2017/04/06 17:37:13
Given the above method, and the name of this metho
shivanisha
2017/04/07 21:11:16
I tried doing that and made CanTransactionWriteRes
jkarlin
2017/04/11 14:38:28
Acknowledged.
| |
414 bool CanTransactionWriteResponseBody(ActiveEntry* entry, | |
415 Transaction* transaction, | |
416 const std::string& method) const; | |
356 | 417 |
357 // Called when the transaction has finished writing to this entry. |success| | 418 // Called when the transaction has finished writing to this entry. |success| |
358 // is false if the cache entry should be deleted. | 419 // is false if the cache entry should be deleted. |
359 void DoneWritingToEntry(ActiveEntry* entry, bool success); | 420 void DoneWritingToEntry(ActiveEntry* entry, |
421 bool success, | |
422 Transaction* transaction); | |
360 | 423 |
361 // Called when the transaction has finished reading from this entry. | 424 // Called when the transaction has finished reading from this entry. |
362 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); | 425 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 | 426 |
368 // Returns the LoadState of the provided pending transaction. | 427 // Returns the LoadState of the provided pending transaction. |
369 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); | 428 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); |
370 | 429 |
371 // Removes the transaction |trans|, from the pending list of an entry | 430 // Removes the transaction |trans|, from the pending list of an entry |
372 // (PendingOp, active or doomed entry). | 431 // (PendingOp, active or doomed entry). |
373 void RemovePendingTransaction(Transaction* trans); | 432 void RemovePendingTransaction(Transaction* trans); |
374 | 433 |
375 // Removes the transaction |trans|, from the pending list of |entry|. | 434 // Removes the transaction |trans|, from the pending list of |entry|. |
376 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, | 435 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, |
377 Transaction* trans); | 436 Transaction* trans); |
378 | 437 |
379 // Removes the transaction |trans|, from the pending list of |pending_op|. | 438 // Removes the transaction |trans|, from the pending list of |pending_op|. |
380 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, | 439 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, |
381 Transaction* trans); | 440 Transaction* trans); |
382 // Resumes processing the pending list of |entry|. | 441 |
383 void ProcessPendingQueue(ActiveEntry* entry); | 442 // Removes and returns all queued transactions in |entry| in FIFO order. This |
443 // includes transactions that have completed the headers phase and those that | |
444 // have not been added to the entry yet in that order. |list| is the output | |
445 // argument. | |
446 void RemoveAllQueuedTransactions(ActiveEntry* entry, TransactionList* list); | |
jkarlin
2017/04/06 17:37:13
Note that these methods need to appear in cc file
shivanisha
2017/04/07 21:11:16
done
| |
447 | |
448 // Resumes processing the queued transactions of |entry|. | |
449 void ProcessQueuedTransactions(ActiveEntry* entry); | |
450 | |
451 // Checks if a transaction can be added to the entry. If yes, it will | |
452 // invoke the IO callback of the transaction. This is a helper function for | |
453 // OnProcessQueuedTransactions. | |
454 void ProcessAddToEntryQueue(ActiveEntry* entry); | |
455 | |
456 // Checks if a transaction that has already completed the response headers | |
457 // phase can resume reading/writing the response body. If yes, it will | |
458 // invoke the IO callback of the transaction. This is a helper function for | |
459 // OnProcessQueuedTransactions. | |
460 void ProcessDoneHeadersQueue(ActiveEntry* entry); | |
461 | |
462 // Processes either writer's failure to write response body or | |
463 // headers_transactions's failure to write headers. | |
464 void ProcessEntryFailure(ActiveEntry* entry); | |
384 | 465 |
385 // Events (called via PostTask) --------------------------------------------- | 466 // Events (called via PostTask) --------------------------------------------- |
386 | 467 |
387 void OnProcessPendingQueue(ActiveEntry* entry); | 468 void OnProcessQueuedTransactions(ActiveEntry* entry); |
388 | 469 |
389 // Callbacks ---------------------------------------------------------------- | 470 // Callbacks ---------------------------------------------------------------- |
390 | 471 |
391 // Processes BackendCallback notifications. | 472 // Processes BackendCallback notifications. |
392 void OnIOComplete(int result, PendingOp* entry); | 473 void OnIOComplete(int result, PendingOp* entry); |
393 | 474 |
394 // Helper to conditionally delete |pending_op| if the HttpCache object it | 475 // Helper to conditionally delete |pending_op| if the HttpCache object it |
395 // is meant for has been deleted. | 476 // is meant for has been deleted. |
396 // | 477 // |
397 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might | 478 // 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_; | 516 std::unique_ptr<base::Clock> clock_; |
436 | 517 |
437 base::WeakPtrFactory<HttpCache> weak_factory_; | 518 base::WeakPtrFactory<HttpCache> weak_factory_; |
438 | 519 |
439 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 520 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
440 }; | 521 }; |
441 | 522 |
442 } // namespace net | 523 } // namespace net |
443 | 524 |
444 #endif // NET_HTTP_HTTP_CACHE_H_ | 525 #endif // NET_HTTP_HTTP_CACHE_H_ |
OLD | NEW |