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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 public: | 62 public: |
63 // The cache mode of operation. | 63 // The cache mode of operation. |
64 enum Mode { | 64 enum Mode { |
65 // Normal mode just behaves like a standard web cache. | 65 // Normal mode just behaves like a standard web cache. |
66 NORMAL = 0, | 66 NORMAL = 0, |
67 // Disables reads and writes from the cache. | 67 // Disables reads and writes from the cache. |
68 // Equivalent to setting LOAD_DISABLE_CACHE on every request. | 68 // Equivalent to setting LOAD_DISABLE_CACHE on every request. |
69 DISABLE | 69 DISABLE |
70 }; | 70 }; |
71 | 71 |
72 class Transaction; | |
73 using TransactionList = std::list<Transaction*>; | |
74 using TransactionSet = std::unordered_set<Transaction*>; | |
75 | |
72 // A BackendFactory creates a backend object to be used by the HttpCache. | 76 // A BackendFactory creates a backend object to be used by the HttpCache. |
73 class NET_EXPORT BackendFactory { | 77 class NET_EXPORT BackendFactory { |
74 public: | 78 public: |
75 virtual ~BackendFactory() {} | 79 virtual ~BackendFactory() {} |
76 | 80 |
77 // The actual method to build the backend. Returns a net error code. If | 81 // The actual method to build the backend. Returns a net error code. If |
78 // ERR_IO_PENDING is returned, the |callback| will be notified when the | 82 // ERR_IO_PENDING is returned, the |callback| will be notified when the |
79 // operation completes, and |backend| must remain valid until the | 83 // operation completes, and |backend| must remain valid until the |
80 // notification arrives. | 84 // notification arrives. |
81 // The implementation must not access the factory object after invoking the | 85 // The implementation must not access the factory object after invoking the |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 kResponseInfoIndex = 0, | 235 kResponseInfoIndex = 0, |
232 kResponseContentIndex, | 236 kResponseContentIndex, |
233 kMetadataIndex, | 237 kMetadataIndex, |
234 | 238 |
235 // Must remain at the end of the enum. | 239 // Must remain at the end of the enum. |
236 kNumCacheEntryDataIndices | 240 kNumCacheEntryDataIndices |
237 }; | 241 }; |
238 | 242 |
239 class MetadataWriter; | 243 class MetadataWriter; |
240 class QuicServerInfoFactoryAdaptor; | 244 class QuicServerInfoFactoryAdaptor; |
241 class Transaction; | |
242 class WorkItem; | 245 class WorkItem; |
243 friend class Transaction; | 246 friend class Transaction; |
244 friend class ViewCacheHelper; | 247 friend class ViewCacheHelper; |
245 struct PendingOp; // Info for an entry under construction. | 248 struct PendingOp; // Info for an entry under construction. |
246 | 249 |
247 using TransactionList = std::list<Transaction*>; | |
248 using TransactionSet = std::unordered_set<Transaction*>; | |
249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; | 250 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; |
250 | 251 |
251 struct ActiveEntry { | 252 struct ActiveEntry { |
252 explicit ActiveEntry(disk_cache::Entry* entry); | 253 explicit ActiveEntry(disk_cache::Entry* entry); |
253 ~ActiveEntry(); | 254 ~ActiveEntry(); |
254 size_t EstimateMemoryUsage() const; | 255 size_t EstimateMemoryUsage() const; |
255 | 256 |
256 // Returns true if no transactions are associated with this entry. | 257 // Returns true if no transactions are associated with this entry. |
257 bool HasNoTransactions(); | 258 bool HasNoTransactions(); |
258 | 259 |
259 disk_cache::Entry* disk_entry; | 260 // Returns true if no active readers/writer transactions are associated |
260 Transaction* writer; | 261 // with this entry. |
262 bool HasNoActiveTransactions(); | |
263 | |
264 // Returns true if the given transaction is a reader. | |
265 bool IsReader(Transaction* transaction); | |
shivanisha
2017/03/16 15:43:26
Will remove this function since it is no longer in
shivanisha
2017/03/20 16:34:52
done
| |
266 | |
267 disk_cache::Entry* disk_entry = nullptr; | |
268 | |
269 // We implement a basic reader/writer lock for the disk cache entry. If | |
270 // there is a writer, then all read-only transactions must wait. Non | |
271 // read-only transactions can proceed to their validation phase. Validation | |
272 // is allowed for one transaction at a time so that we do not end up with | |
273 // wasted network requests. | |
274 | |
275 // The following variables represent the various transactions associated | |
276 // with this entry in different stages. A transaction goes through these | |
277 // transitions. | |
278 // | |
279 // Write mode transactions: | |
280 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> | |
281 // writer | |
282 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> | |
283 // readers (once the data is written to the cache by another writer) | |
284 // | |
285 // Read only transactions: | |
286 // add_to_entry_queue-> readers (once the data is written to the cache by | |
287 // the writer) | |
288 | |
289 // Transactions waiting to be added to entry. | |
290 TransactionList add_to_entry_queue; | |
291 | |
292 // Transaction currently in the headers phase, either validating the | |
293 // response or getting new headers. This can exist simultaneously with | |
294 // writer or readers while validating existing headers. | |
295 Transaction* headers_transaction = nullptr; | |
296 | |
297 // Transactions that have completed their headers phase and are waiting | |
298 // to read the response body or write the response body. | |
299 TransactionList done_headers_queue; | |
300 | |
301 // Transaction currently reading from the network and writing to the cache. | |
302 Transaction* writer = nullptr; | |
303 | |
304 // Transactions that can only read from the cache. Only one of writer or | |
305 // readers can exist at a time. | |
261 TransactionSet readers; | 306 TransactionSet readers; |
262 TransactionList pending_queue; | 307 |
263 bool will_process_pending_queue; | 308 // The following variables are true if OnProcessQueuedTransactions is posted |
264 bool doomed; | 309 bool will_process_queued_transactions = false; |
310 | |
311 // True if entry is doomed. | |
312 bool doomed = false; | |
265 }; | 313 }; |
266 | 314 |
267 using ActiveEntriesMap = | 315 using ActiveEntriesMap = |
268 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; | 316 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; |
269 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; | 317 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; |
270 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; | 318 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; |
271 using PlaybackCacheMap = std::unordered_map<std::string, int>; | 319 using PlaybackCacheMap = std::unordered_map<std::string, int>; |
272 | 320 |
273 // Methods ------------------------------------------------------------------ | 321 // Methods ------------------------------------------------------------------ |
274 | 322 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 | 383 |
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. This method returns ERR_IO_PENDING |
346 // the transaction will be notified about completion via its IO callback. This | 394 // 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 | 395 int AddTransactionToEntry(ActiveEntry* entry, Transaction* transaction); |
348 // added to the provided entry, and it should retry the process with another | 396 |
349 // one (in this case, the entry is no longer valid). | 397 // Checks if a transaction can be added to the entry. If yes, it will |
350 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); | 398 // invoke the IO callback of the transaction. |
399 void ProcessAddToEntryTransaction(ActiveEntry* entry); | |
400 | |
401 // Checks if a transaction that has already completed the response headers | |
402 // phase can resume reading/writing the response body. If yes, it will | |
403 // invoke the IO callback of the transaction. | |
404 void ProcessDoneHeadersTransaction(ActiveEntry* entry); | |
Randy Smith (Not in Mondays)
2017/03/17 18:13:05
Suggestion: Include in the comments that the above
shivanisha
2017/03/20 16:34:52
Done
| |
405 | |
406 // Transaction invokes this when its response headers phase is complete | |
407 // either on validating existing headers/ skippig validation or getting new | |
shivanisha
2017/03/16 00:54:54
*skipping
shivanisha
2017/03/20 16:34:52
done
| |
408 // headers. is_match should be passed as true if the validation was a match or | |
409 // it was new headers. | |
shivanisha
2017/03/16 15:43:26
*or headers were retrieved the first time.
shivanisha
2017/03/20 16:34:52
done
| |
410 // If its not a match, this entry is doomed/destroyed and pending | |
411 // transactions are restarted. Returns OK. | |
412 // If it is a match, the transaction is added to a queue and a task is posted | |
413 // to process queued transactions of the entry. Returns ERR_IO_PENDING. | |
414 int DoneResponseHeaders(ActiveEntry* entry, | |
415 Transaction* transaction, | |
416 bool is_match); | |
351 | 417 |
352 // Called when the transaction has finished working with this entry. |cancel| | 418 // 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 | 419 // is true if the operation was cancelled by the caller instead of running |
354 // to completion. | 420 // to completion. |
355 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); | 421 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); |
356 | 422 |
357 // Called when the transaction has finished writing to this entry. |success| | 423 // Called when the transaction has finished writing to this entry. |success| |
358 // is false if the cache entry should be deleted. | 424 // is false if the cache entry should be deleted. |
359 void DoneWritingToEntry(ActiveEntry* entry, bool success); | 425 void DoneWritingToEntry(ActiveEntry* entry, |
426 bool success, | |
427 Transaction* transaction); | |
428 | |
429 // Processes other transactions when a transaction is done writing to the | |
430 // entry, based on success or failure of this transaction. | |
431 void DoneWritingToEntryProcessOtherTransactions(ActiveEntry* entry, | |
432 bool success); | |
shivanisha
2017/03/16 15:43:26
Will remove this function declaration as it is no
shivanisha
2017/03/20 16:34:52
done
| |
360 | 433 |
361 // Called when the transaction has finished reading from this entry. | 434 // Called when the transaction has finished reading from this entry. |
362 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); | 435 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* transaction); |
363 | 436 |
364 // Converts the active writer transaction to a reader so that other | 437 // Converts the active writer/validating transaction to a reader so that other |
365 // transactions can start reading from this entry. | 438 // transactions can start reading from this entry. It is invoked in the |
366 void ConvertWriterToReader(ActiveEntry* entry); | 439 // validation phase of a transaction when the current entry is validated. It |
440 // needs to differentiate between the scenarios when the entry is already | |
441 // completely written to by another transaction or is currently being written. | |
442 // In the former case, the transaction will become a reader at this time. | |
443 // void ConvertWriterToReader(ActiveEntry* entry, Transaction* transaction); | |
shivanisha
2017/03/16 00:54:54
Will remove this commented code.
shivanisha
2017/03/20 16:34:52
done
| |
367 | 444 |
368 // Returns the LoadState of the provided pending transaction. | 445 // Returns the LoadState of the provided pending transaction. |
369 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); | 446 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); |
370 | 447 |
371 // Removes the transaction |trans|, from the pending list of an entry | 448 // Removes the transaction |trans|, from the pending list of an entry |
372 // (PendingOp, active or doomed entry). | 449 // (PendingOp, active or doomed entry). |
373 void RemovePendingTransaction(Transaction* trans); | 450 void RemovePendingTransaction(Transaction* trans); |
374 | 451 |
375 // Removes the transaction |trans|, from the pending list of |entry|. | 452 // Removes the transaction |trans|, from the pending list of |entry|. |
376 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, | 453 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, |
377 Transaction* trans); | 454 Transaction* trans); |
378 | 455 |
379 // Removes the transaction |trans|, from the pending list of |pending_op|. | 456 // Removes the transaction |trans|, from the pending list of |pending_op|. |
380 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, | 457 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, |
381 Transaction* trans); | 458 Transaction* trans); |
382 // Resumes processing the pending list of |entry|. | 459 |
383 void ProcessPendingQueue(ActiveEntry* entry); | 460 // Returns all queued transactions in the list in FIFO order. This included |
461 // transactions that have completed te headers phase and those that have not | |
462 // been added to the entry yet. | |
463 TransactionList GetAllPendingTransactions(ActiveEntry* entry); | |
464 | |
465 // Resumes processing the queued transactions of |entry|. | |
466 void ProcessQueuedTransactions(ActiveEntry* entry); | |
467 | |
468 // Processes either writer's failure to write response body or | |
469 // headers_transactions's failure to write headers. | |
470 void ProcessEntryFailure(ActiveEntry* entry); | |
384 | 471 |
385 // Events (called via PostTask) --------------------------------------------- | 472 // Events (called via PostTask) --------------------------------------------- |
386 | 473 |
387 void OnProcessPendingQueue(ActiveEntry* entry); | 474 void OnProcessQueuedTransactions(ActiveEntry* entry); |
388 | 475 |
389 // Callbacks ---------------------------------------------------------------- | 476 // Callbacks ---------------------------------------------------------------- |
390 | 477 |
391 // Processes BackendCallback notifications. | 478 // Processes BackendCallback notifications. |
392 void OnIOComplete(int result, PendingOp* entry); | 479 void OnIOComplete(int result, PendingOp* entry); |
393 | 480 |
394 // Helper to conditionally delete |pending_op| if the HttpCache object it | 481 // Helper to conditionally delete |pending_op| if the HttpCache object it |
395 // is meant for has been deleted. | 482 // is meant for has been deleted. |
396 // | 483 // |
397 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might | 484 // 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_; | 522 std::unique_ptr<base::Clock> clock_; |
436 | 523 |
437 base::WeakPtrFactory<HttpCache> weak_factory_; | 524 base::WeakPtrFactory<HttpCache> weak_factory_; |
438 | 525 |
439 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 526 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
440 }; | 527 }; |
441 | 528 |
442 } // namespace net | 529 } // namespace net |
443 | 530 |
444 #endif // NET_HTTP_HTTP_CACHE_H_ | 531 #endif // NET_HTTP_HTTP_CACHE_H_ |
OLD | NEW |