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 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 kResponseInfoIndex = 0, | 231 kResponseInfoIndex = 0, |
232 kResponseContentIndex, | 232 kResponseContentIndex, |
233 kMetadataIndex, | 233 kMetadataIndex, |
234 | 234 |
235 // Must remain at the end of the enum. | 235 // Must remain at the end of the enum. |
236 kNumCacheEntryDataIndices | 236 kNumCacheEntryDataIndices |
237 }; | 237 }; |
238 | 238 |
239 class MetadataWriter; | 239 class MetadataWriter; |
240 class QuicServerInfoFactoryAdaptor; | 240 class QuicServerInfoFactoryAdaptor; |
241 class WorkItem; | |
jkarlin
2017/03/07 16:45:41
This isn't alphabetical.
jkarlin
2017/03/07 17:22:59
I actually don't know that they need to be alphabe
shivanisha
2017/03/08 21:42:12
Done.
| |
241 class Transaction; | 242 class Transaction; |
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 using TransactionList = std::list<Transaction*>; | 247 using TransactionList = std::list<Transaction*>; |
248 using TransactionSet = std::unordered_set<Transaction*>; | 248 using TransactionSet = std::unordered_set<Transaction*>; |
249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; | 249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; |
250 | 250 |
251 struct ActiveEntry { | 251 class ActiveEntry { |
jkarlin
2017/03/07 16:45:42
If it's a class the members need to be private and
shivanisha
2017/03/08 21:42:12
Reverted it to being a struct. I initially thought
| |
252 public: | |
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 the given transaction is a reader. |
260 Transaction* writer; | 261 bool IsReader(Transaction* transaction); |
262 | |
263 disk_cache::Entry* disk_entry = nullptr; | |
264 | |
265 // Transaction currently reading from the network and writing to the cache. | |
266 Transaction* writer = nullptr; | |
267 | |
268 // Transactions that can only read from the cache. Only one of writer or | |
269 // readers can exist at a time. | |
261 TransactionSet readers; | 270 TransactionSet readers; |
262 TransactionList pending_queue; | 271 |
263 bool will_process_pending_queue; | 272 // Transactions waiting to be added to entry. |
Randy Smith (Not in Mondays)
2017/03/07 22:54:30
nit: Thank you for the extra comments; they help!
Randy Smith (Not in Mondays)
2017/03/08 19:11:23
minor additional nit/suggestion: Ordering the memb
shivanisha
2017/03/08 21:42:12
Done
| |
264 bool doomed; | 273 TransactionList pending_queue; |
jkarlin
2017/03/07 16:45:41
pending_queue seems a bit vague now. Perhaps add_t
shivanisha
2017/03/08 21:42:12
Changed. add_to_entry_queue seems more consistent
| |
274 | |
275 // Transaction currently validating the response. This can exist | |
276 // simultaneously with writer or readers. | |
277 Transaction* validating_transaction = nullptr; | |
278 | |
279 // Transactions that have completed their validation phase and are waiting | |
280 // to read the response body. | |
281 TransactionList validated_queue; | |
282 | |
283 bool will_process_pending_queue = false; | |
284 bool will_process_parallel_validation = false; | |
285 bool doomed = false; | |
265 }; | 286 }; |
266 | 287 |
267 using ActiveEntriesMap = | 288 using ActiveEntriesMap = |
268 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; | 289 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; |
269 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; | 290 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; |
270 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; | 291 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; |
271 using PlaybackCacheMap = std::unordered_map<std::string, int>; | 292 using PlaybackCacheMap = std::unordered_map<std::string, int>; |
272 | 293 |
273 // Methods ------------------------------------------------------------------ | 294 // Methods ------------------------------------------------------------------ |
274 | 295 |
(...skipping 28 matching lines...) Expand all Loading... | |
303 | 324 |
304 // Dooms the entry associated with a GET for a given |url|. | 325 // Dooms the entry associated with a GET for a given |url|. |
305 void DoomMainEntryForUrl(const GURL& url); | 326 void DoomMainEntryForUrl(const GURL& url); |
306 | 327 |
307 // Closes a previously doomed entry. | 328 // Closes a previously doomed entry. |
308 void FinalizeDoomedEntry(ActiveEntry* entry); | 329 void FinalizeDoomedEntry(ActiveEntry* entry); |
309 | 330 |
310 // Returns an entry that is currently in use and not doomed, or NULL. | 331 // Returns an entry that is currently in use and not doomed, or NULL. |
311 ActiveEntry* FindActiveEntry(const std::string& key); | 332 ActiveEntry* FindActiveEntry(const std::string& key); |
312 | 333 |
334 // Returns true if given entry is still active. | |
jkarlin
2017/03/07 16:45:41
What does it mean for an entry to be active? It's
shivanisha
2017/03/08 21:42:12
Actually, it needs to search if the entry is alive
| |
335 bool IsEntryActive(const std::string& key, const ActiveEntry* entry); | |
336 | |
313 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk | 337 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk |
314 // cache entry. | 338 // cache entry. |
315 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); | 339 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); |
316 | 340 |
317 // Deletes an ActiveEntry. | 341 // Deletes an ActiveEntry. |
318 void DeactivateEntry(ActiveEntry* entry); | 342 void DeactivateEntry(ActiveEntry* entry); |
319 | 343 |
320 // Deletes an ActiveEntry using an exhaustive search. | 344 // Deletes an ActiveEntry using an exhaustive search. |
321 void SlowDeactivateEntry(ActiveEntry* entry); | 345 void SlowDeactivateEntry(ActiveEntry* entry); |
322 | 346 |
(...skipping 14 matching lines...) Expand all Loading... | |
337 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if | 361 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if |
338 // this method returns ERR_IO_PENDING. | 362 // this method returns ERR_IO_PENDING. |
339 int CreateEntry(const std::string& key, ActiveEntry** entry, | 363 int CreateEntry(const std::string& key, ActiveEntry** entry, |
340 Transaction* trans); | 364 Transaction* trans); |
341 | 365 |
342 // Destroys an ActiveEntry (active or doomed). | 366 // Destroys an ActiveEntry (active or doomed). |
343 void DestroyEntry(ActiveEntry* entry); | 367 void DestroyEntry(ActiveEntry* entry); |
344 | 368 |
345 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING | 369 // 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 | 370 // the transaction will be notified about completion via its IO callback. This |
347 // method returns ERR_CACHE_RACE to signal the transaction that it cannot be | 371 // method returns ERR_CACHE_RACE to signal the transaction that it cannot be |
jkarlin
2017/03/07 16:45:41
I don't think this actually does return ERR_CACHE_
shivanisha
2017/03/08 21:42:12
done
| |
348 // added to the provided entry, and it should retry the process with another | 372 // added to the provided entry, and it should retry the process with another |
349 // one (in this case, the entry is no longer valid). | 373 // one (in this case, the entry is no longer valid). validation_done should |
jkarlin
2017/03/07 16:45:41
|validation_done|. We wrap variable names in ||'s.
shivanisha
2017/03/08 21:42:12
N/A now after refactoring.
| |
350 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); | 374 // be passed as true if this transaction has already passed the validation |
375 // phase, new_transaction should be passed as true if this transaction is a | |
jkarlin
2017/03/07 16:45:41
same with |new_transaction|
shivanisha
2017/03/08 21:42:12
N/A now after refactoring. Removed these from the
| |
376 // new transaction and not already added to queued in the entry. | |
377 int AddTransactionToEntry(ActiveEntry* entry, | |
378 Transaction* transaction, | |
379 bool validation_done = false, | |
jkarlin
2017/03/07 16:45:41
nit: s/validation_done/validation_needed/
shivanisha
2017/03/08 21:42:12
N/A now after refactoring.
| |
380 bool new_transaction = false); | |
jkarlin
2017/03/07 16:45:41
nit: is_new_transaction to make it clear that it's
jkarlin
2017/03/07 16:45:41
This method has few enough existing callers that I
Randy Smith (Not in Mondays)
2017/03/08 19:11:23
The style guide generally recommends against defau
shivanisha
2017/03/08 21:42:12
Thanks for the link.
Removed default arguments as
shivanisha
2017/03/08 21:42:12
N/A now after refactoring.
| |
381 | |
382 // Transaction invokes this when its validation matches the entry so another | |
383 // transaction can now start validation. write_this_response should be passed | |
384 // as true if this transaction is also responsible for writing the response | |
385 // body to the cache. Returns OK if the transaction is an active writer or | |
386 // reader and ERR_IO_PENDING if the transaction should wait to be able to read | |
387 // from the network or cache. | |
388 int OnValidationMatch(ActiveEntry* entry, | |
jkarlin
2017/03/07 16:45:41
In keeping with the naming scheme, perhaps DoneWit
shivanisha
2017/03/08 21:42:12
Done. Created DoneValidationWithEntry() and merged
| |
389 Transaction* transaction, | |
390 bool write_this_response); | |
391 | |
392 // Transaction invokes this when its validation does not match the entry. | |
393 // Leads to dooming the entry and restarting the pending transactions. | |
394 void OnValidationNoMatch(ActiveEntry* entry, Transaction* transaction); | |
351 | 395 |
352 // Called when the transaction has finished working with this entry. |cancel| | 396 // 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 | 397 // is true if the operation was cancelled by the caller instead of running |
354 // to completion. | 398 // to completion. |
355 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); | 399 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); |
356 | 400 |
357 // Called when the transaction has finished writing to this entry. |success| | 401 // Called when the transaction has finished writing to this entry. |success| |
358 // is false if the cache entry should be deleted. | 402 // is false if the cache entry should be deleted. |
359 void DoneWritingToEntry(ActiveEntry* entry, bool success); | 403 void DoneWritingToEntry(ActiveEntry* entry, |
404 bool success, | |
405 Transaction* transaction); | |
406 | |
407 // Processes other transactions when a transaction is done writing to the | |
408 // entry, based on success or failure of this transaction. | |
409 void DoneWritingToEntryProcessOtherTransactions(ActiveEntry* entry, | |
410 bool success); | |
360 | 411 |
361 // Called when the transaction has finished reading from this entry. | 412 // Called when the transaction has finished reading from this entry. |
362 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); | 413 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* transaction); |
363 | 414 |
364 // Converts the active writer transaction to a reader so that other | 415 // Converts the active writer/validating transaction to a reader so that other |
365 // transactions can start reading from this entry. | 416 // transactions can start reading from this entry. Returns true if the |
366 void ConvertWriterToReader(ActiveEntry* entry); | 417 // transaction was added to readers. |
Randy Smith (Not in Mondays)
2017/03/07 22:54:30
It's not clear to me what this documentation means
shivanisha
2017/03/08 21:42:12
-- We want to preserve their original mode till we
| |
418 bool ConvertWriterToReader(ActiveEntry* entry, Transaction* transaction); | |
367 | 419 |
368 // Returns the LoadState of the provided pending transaction. | 420 // Returns the LoadState of the provided pending transaction. |
369 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); | 421 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); |
370 | 422 |
371 // Removes the transaction |trans|, from the pending list of an entry | 423 // Removes the transaction |trans|, from the pending list of an entry |
372 // (PendingOp, active or doomed entry). | 424 // (PendingOp, active or doomed entry). |
373 void RemovePendingTransaction(Transaction* trans); | 425 void RemovePendingTransaction(Transaction* trans); |
374 | 426 |
375 // Removes the transaction |trans|, from the pending list of |entry|. | 427 // Removes the transaction |trans|, from the pending list of |entry|. |
376 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, | 428 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, |
377 Transaction* trans); | 429 Transaction* trans); |
378 | 430 |
379 // Removes the transaction |trans|, from the pending list of |pending_op|. | 431 // Removes the transaction |trans|, from the pending list of |pending_op|. |
380 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, | 432 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, |
381 Transaction* trans); | 433 Transaction* trans); |
382 // Resumes processing the pending list of |entry|. | 434 // Resumes processing the pending list of |entry|. This should always be |
435 // called when the response is already written to the cache. | |
383 void ProcessPendingQueue(ActiveEntry* entry); | 436 void ProcessPendingQueue(ActiveEntry* entry); |
384 | 437 |
438 // Invoked when a transaction can start validating the entry when there may be | |
439 // a writer/readers writing/reading to/from the entry. | |
440 void ProcessParallelValidation(ActiveEntry* entry); | |
441 | |
385 // Events (called via PostTask) --------------------------------------------- | 442 // Events (called via PostTask) --------------------------------------------- |
386 | 443 |
387 void OnProcessPendingQueue(ActiveEntry* entry); | 444 void OnProcessPendingQueue(const std::string& key, ActiveEntry* entry); |
445 | |
446 void OnProcessParallelValidation(const std::string& key, ActiveEntry* entry); | |
447 | |
448 // Dooms the entry and restarts all the pending transactions. | |
449 void DoomEntryRestartPendingTransactions(ActiveEntry* entry); | |
450 | |
451 // Destroys the entry and restarts all the pending transactions. | |
452 void DestroyEntryRestartPendingTransactions(ActiveEntry* entry); | |
453 | |
454 // Retrieves all pending transactions from entry in a single list. | |
455 TransactionList GetAllPendingTransactions(ActiveEntry* entry); | |
456 | |
457 // Helper function for restarting all pending transactions by invoking their | |
458 // IO callbacks with ERR_CACHE_RACE. | |
jkarlin
2017/03/07 16:45:41
Is this actually a cache race though? Seems like a
shivanisha
2017/03/08 21:42:12
This error code is used whenever we want the HCT s
| |
459 void RestartPendingTransactions(const TransactionList& list); | |
388 | 460 |
389 // Callbacks ---------------------------------------------------------------- | 461 // Callbacks ---------------------------------------------------------------- |
390 | 462 |
391 // Processes BackendCallback notifications. | 463 // Processes BackendCallback notifications. |
392 void OnIOComplete(int result, PendingOp* entry); | 464 void OnIOComplete(int result, PendingOp* entry); |
393 | 465 |
394 // Helper to conditionally delete |pending_op| if the HttpCache object it | 466 // Helper to conditionally delete |pending_op| if the HttpCache object it |
395 // is meant for has been deleted. | 467 // is meant for has been deleted. |
396 // | 468 // |
397 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might | 469 // 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_; | 507 std::unique_ptr<base::Clock> clock_; |
436 | 508 |
437 base::WeakPtrFactory<HttpCache> weak_factory_; | 509 base::WeakPtrFactory<HttpCache> weak_factory_; |
438 | 510 |
439 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 511 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
440 }; | 512 }; |
441 | 513 |
442 } // namespace net | 514 } // namespace net |
443 | 515 |
444 #endif // NET_HTTP_HTTP_CACHE_H_ | 516 #endif // NET_HTTP_HTTP_CACHE_H_ |
OLD | NEW |