Chromium Code Reviews| 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. |
| 11 // | 11 // |
| 12 // See HttpTransactionFactory and HttpTransaction for more details. | 12 // See HttpTransactionFactory and HttpTransaction for more details. |
| 13 | 13 |
| 14 #ifndef NET_HTTP_HTTP_CACHE_H_ | 14 #ifndef NET_HTTP_HTTP_CACHE_H_ |
| 15 #define NET_HTTP_HTTP_CACHE_H_ | 15 #define NET_HTTP_HTTP_CACHE_H_ |
| 16 | 16 |
| 17 #include <list> | 17 #include <list> |
| 18 #include <map> | 18 #include <map> |
| 19 #include <memory> | 19 #include <memory> |
| 20 #include <string> | 20 #include <string> |
| 21 #include <unordered_map> | 21 #include <unordered_map> |
| 22 #include <unordered_set> | |
| 22 | 23 |
| 23 #include "base/files/file_path.h" | 24 #include "base/files/file_path.h" |
| 24 #include "base/macros.h" | 25 #include "base/macros.h" |
| 25 #include "base/memory/weak_ptr.h" | 26 #include "base/memory/weak_ptr.h" |
| 26 #include "base/threading/non_thread_safe.h" | 27 #include "base/threading/non_thread_safe.h" |
| 27 #include "base/time/clock.h" | 28 #include "base/time/clock.h" |
| 28 #include "base/time/time.h" | 29 #include "base/time/time.h" |
| 29 #include "net/base/cache_type.h" | 30 #include "net/base/cache_type.h" |
| 30 #include "net/base/completion_callback.h" | 31 #include "net/base/completion_callback.h" |
| 31 #include "net/base/load_states.h" | 32 #include "net/base/load_states.h" |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 } | 196 } |
| 196 | 197 |
| 197 // Causes all transactions created after this point to generate a failure | 198 // Causes all transactions created after this point to generate a failure |
| 198 // when attempting to conditionalize a network request. | 199 // when attempting to conditionalize a network request. |
| 199 void FailConditionalizationForTest() { | 200 void FailConditionalizationForTest() { |
| 200 fail_conditionalization_for_test_ = true; | 201 fail_conditionalization_for_test_ = true; |
| 201 } | 202 } |
| 202 | 203 |
| 203 // HttpTransactionFactory implementation: | 204 // HttpTransactionFactory implementation: |
| 204 int CreateTransaction(RequestPriority priority, | 205 int CreateTransaction(RequestPriority priority, |
| 205 std::unique_ptr<HttpTransaction>* trans) override; | 206 std::unique_ptr<HttpTransaction>* transaction) override; |
| 206 HttpCache* GetCache() override; | 207 HttpCache* GetCache() override; |
| 207 HttpNetworkSession* GetSession() override; | 208 HttpNetworkSession* GetSession() override; |
| 208 | 209 |
| 209 base::WeakPtr<HttpCache> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } | 210 base::WeakPtr<HttpCache> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } |
| 210 | 211 |
| 211 // Resets the network layer to allow for tests that probe | 212 // Resets the network layer to allow for tests that probe |
| 212 // network changes (e.g. host unreachable). The old network layer is | 213 // network changes (e.g. host unreachable). The old network layer is |
| 213 // returned to allow for filter patterns that only intercept | 214 // returned to allow for filter patterns that only intercept |
| 214 // some creation requests. Note ownership exchange. | 215 // some creation requests. Note ownership exchange. |
| 215 std::unique_ptr<HttpTransactionFactory> | 216 std::unique_ptr<HttpTransactionFactory> |
| 216 SetHttpNetworkTransactionFactoryForTesting( | 217 SetHttpNetworkTransactionFactoryForTesting( |
| 217 std::unique_ptr<HttpTransactionFactory> new_network_layer); | 218 std::unique_ptr<HttpTransactionFactory> new_network_layer); |
| 218 | 219 |
| 219 private: | 220 private: |
| 220 // Types -------------------------------------------------------------------- | 221 // Types -------------------------------------------------------------------- |
| 221 | 222 |
| 222 // Disk cache entry data indices. | 223 // Disk cache entry data indices. |
| 223 enum { | 224 enum { |
| 224 kResponseInfoIndex = 0, | 225 kResponseInfoIndex = 0, |
| 225 kResponseContentIndex, | 226 kResponseContentIndex, |
| 226 kMetadataIndex, | 227 kMetadataIndex, |
| 227 | 228 |
| 228 // Must remain at the end of the enum. | 229 // Must remain at the end of the enum. |
| 229 kNumCacheEntryDataIndices | 230 kNumCacheEntryDataIndices |
| 230 }; | 231 }; |
| 231 | 232 |
| 233 class DataAccess; | |
| 234 class SharedWriters; | |
| 235 class Transaction; | |
| 236 typedef std::list<Transaction*> TransactionList; | |
| 237 typedef std::unordered_set<Transaction*> TransactionSet; | |
|
jkarlin
2017/02/03 18:26:19
This and above should be using instead of typedef
shivanisha
2017/02/06 21:14:10
done.
| |
| 232 class MetadataWriter; | 238 class MetadataWriter; |
| 233 class QuicServerInfoFactoryAdaptor; | 239 class QuicServerInfoFactoryAdaptor; |
| 234 class Transaction; | |
| 235 class WorkItem; | 240 class WorkItem; |
| 236 friend class Transaction; | |
| 237 friend class ViewCacheHelper; | 241 friend class ViewCacheHelper; |
| 238 struct PendingOp; // Info for an entry under construction. | 242 struct PendingOp; // Info for an entry under construction. |
| 239 | 243 |
| 240 typedef std::list<Transaction*> TransactionList; | |
| 241 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; | 244 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; |
| 242 | 245 |
| 243 struct ActiveEntry { | 246 struct ActiveEntry { |
| 244 explicit ActiveEntry(disk_cache::Entry* entry); | 247 explicit ActiveEntry(disk_cache::Entry* entry); |
| 245 ~ActiveEntry(); | 248 ~ActiveEntry(); |
| 246 | 249 |
| 247 disk_cache::Entry* disk_entry; | 250 disk_cache::Entry* disk_entry; |
| 251 // Either writer or shared_writers will be non-null at any point in time. | |
| 248 Transaction* writer; | 252 Transaction* writer; |
| 249 TransactionList readers; | 253 std::unique_ptr<SharedWriters> shared_writers; |
| 254 TransactionSet readers; | |
| 250 TransactionList pending_queue; | 255 TransactionList pending_queue; |
| 251 bool will_process_pending_queue; | 256 bool will_process_pending_queue; |
| 252 bool doomed; | 257 bool doomed; |
| 253 }; | 258 }; |
| 254 | 259 |
| 255 using ActiveEntriesMap = | 260 using ActiveEntriesMap = |
| 256 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; | 261 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; |
| 257 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; | 262 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; |
| 258 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; | 263 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; |
| 259 using PlaybackCacheMap = std::unordered_map<std::string, int>; | 264 using PlaybackCacheMap = std::unordered_map<std::string, int>; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 // Closes a previously doomed entry. | 300 // Closes a previously doomed entry. |
| 296 void FinalizeDoomedEntry(ActiveEntry* entry); | 301 void FinalizeDoomedEntry(ActiveEntry* entry); |
| 297 | 302 |
| 298 // Returns an entry that is currently in use and not doomed, or NULL. | 303 // Returns an entry that is currently in use and not doomed, or NULL. |
| 299 ActiveEntry* FindActiveEntry(const std::string& key); | 304 ActiveEntry* FindActiveEntry(const std::string& key); |
| 300 | 305 |
| 301 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk | 306 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk |
| 302 // cache entry. | 307 // cache entry. |
| 303 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); | 308 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); |
| 304 | 309 |
| 305 // Deletes an ActiveEntry. | 310 // Deletes an ActiveEntry. Expects all of the transaction pointers to be |
| 311 // null and containers to be empty: writer, shared_writers, pending_queue | |
|
jkarlin
2017/02/03 18:26:19
s/null/nullptr/
shivanisha
2017/02/06 21:14:10
done.
| |
| 312 // and readers. | |
| 306 void DeactivateEntry(ActiveEntry* entry); | 313 void DeactivateEntry(ActiveEntry* entry); |
| 307 | 314 |
| 308 // Deletes an ActiveEntry using an exhaustive search. | 315 // Deletes an ActiveEntry using an exhaustive search. |
| 309 void SlowDeactivateEntry(ActiveEntry* entry); | 316 void SlowDeactivateEntry(ActiveEntry* entry); |
| 310 | 317 |
| 311 // Returns the PendingOp for the desired |key|. If an entry is not under | 318 // Returns the PendingOp for the desired |key|. If an entry is not under |
| 312 // construction already, a new PendingOp structure is created. | 319 // construction already, a new PendingOp structure is created. |
| 313 PendingOp* GetPendingOp(const std::string& key); | 320 PendingOp* GetPendingOp(const std::string& key); |
| 314 | 321 |
| 315 // Deletes a PendingOp. | 322 // Deletes a PendingOp. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might | 392 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might |
| 386 // be possible to simplify it using either base::Owned() or base::Passed() | 393 // be possible to simplify it using either base::Owned() or base::Passed() |
| 387 // with the callback. | 394 // with the callback. |
| 388 static void OnPendingOpComplete(const base::WeakPtr<HttpCache>& cache, | 395 static void OnPendingOpComplete(const base::WeakPtr<HttpCache>& cache, |
| 389 PendingOp* pending_op, | 396 PendingOp* pending_op, |
| 390 int result); | 397 int result); |
| 391 | 398 |
| 392 // Processes the backend creation notification. | 399 // Processes the backend creation notification. |
| 393 void OnBackendCreated(int result, PendingOp* pending_op); | 400 void OnBackendCreated(int result, PendingOp* pending_op); |
| 394 | 401 |
| 402 // Destroys the entry and any pending queue transactions' callbacks are | |
| 403 // invoked with ERR_CACHE_RACE that restarts their state machine. | |
| 404 void DestroyEntryRestartPendingQueue(ActiveEntry* entry); | |
| 405 | |
| 406 // Dooms the entry and any pending queue transactions' callbacks are | |
| 407 // invoked with ERR_CACHE_RACE that restarts their state machine. | |
| 408 void DoomEntryRestartPendingQueue(const std::string& key, ActiveEntry* entry); | |
| 409 | |
| 410 // Writes response info to entry. | |
| 411 int WriteResponseInfo(ActiveEntry* entry, | |
| 412 const HttpResponseInfo* response, | |
| 413 CompletionCallback& callback, | |
| 414 bool truncated, | |
| 415 int* io_buf_len); | |
| 416 | |
| 417 // Checks if the response is completed based on content length. | |
| 418 bool IsResponseCompleted(const ActiveEntry* entry, | |
| 419 const HttpResponseInfo* response); | |
| 420 | |
| 421 // Returns true if we should bother attempting to resume this request if it is | |
| 422 // aborted while in progress. If |has_data| is true, the size of the stored | |
| 423 // data is considered for the result. | |
| 424 bool CanResumeEntry(bool has_data, | |
| 425 const std::string& method, | |
| 426 const HttpResponseInfo* response, | |
| 427 ActiveEntry* entry); | |
| 428 | |
| 429 // Creates a SharedWriters object. The ownership of the network_transaction is | |
| 430 // transferred to a new DataAccess object owned by the SharedWriters object. | |
| 431 void CreateSharedWriters(Transaction* cacheTransaction, | |
| 432 std::unique_ptr<HttpTransaction> network_transaction, | |
| 433 RequestPriority priority); | |
| 434 | |
| 435 // Response data completely written to the cache successfully, or | |
| 436 // Response data could not be written to the cache successfully, or | |
| 437 // Response data could not be read from the network successfully. | |
| 438 // Success is true if the entry was marked as truncated successfully. | |
| 439 // destroyed is set to true when the calling SharedWiters object is reset. | |
| 440 void ResponseDoneSharedWriters(ActiveEntry* entry, | |
| 441 bool success, | |
| 442 bool* destroyed); | |
| 443 | |
| 444 // Resets SharedWriters if entry contains an empty SharedWriters object and | |
| 445 // calls ProcessPendingQueue. | |
| 446 void ResetSharedWritersProcessPendingQueue(ActiveEntry* entry); | |
| 447 void ResetSharedWriters(ActiveEntry* entry); | |
| 448 | |
| 449 void RemovedSharedWriterTransaction(Transaction* transaction, | |
| 450 ActiveEntry* entry); | |
| 451 | |
| 395 // Variables ---------------------------------------------------------------- | 452 // Variables ---------------------------------------------------------------- |
| 396 | 453 |
| 397 NetLog* net_log_; | 454 NetLog* net_log_; |
| 398 | 455 |
| 399 // Used when lazily constructing the disk_cache_. | 456 // Used when lazily constructing the disk_cache_. |
| 400 std::unique_ptr<BackendFactory> backend_factory_; | 457 std::unique_ptr<BackendFactory> backend_factory_; |
| 401 bool building_backend_; | 458 bool building_backend_; |
| 402 bool bypass_lock_for_test_; | 459 bool bypass_lock_for_test_; |
| 403 bool fail_conditionalization_for_test_; | 460 bool fail_conditionalization_for_test_; |
| 404 | 461 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 423 std::unique_ptr<base::Clock> clock_; | 480 std::unique_ptr<base::Clock> clock_; |
| 424 | 481 |
| 425 base::WeakPtrFactory<HttpCache> weak_factory_; | 482 base::WeakPtrFactory<HttpCache> weak_factory_; |
| 426 | 483 |
| 427 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 484 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
| 428 }; | 485 }; |
| 429 | 486 |
| 430 } // namespace net | 487 } // namespace net |
| 431 | 488 |
| 432 #endif // NET_HTTP_HTTP_CACHE_H_ | 489 #endif // NET_HTTP_HTTP_CACHE_H_ |
| OLD | NEW |