Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: net/http/http_cache_transaction.h

Issue 2519473002: Fixes the cache lock issue. (Closed)
Patch Set: Feedback addressed Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/http/http_cache_shared_writers_unittest.cc ('k') | net/http/http_cache_transaction.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 HttpCache::Transaction, a private class of HttpCache so 5 // This file declares HttpCache::Transaction, a private class of HttpCache so
6 // it should only be included by http_cache.cc 6 // it should only be included by http_cache.cc
7 7
8 #ifndef NET_HTTP_HTTP_CACHE_TRANSACTION_H_ 8 #ifndef NET_HTTP_HTTP_CACHE_TRANSACTION_H_
9 #define NET_HTTP_HTTP_CACHE_TRANSACTION_H_ 9 #define NET_HTTP_HTTP_CACHE_TRANSACTION_H_
10 10
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 int WriteMetadata(IOBuffer* buf, 100 int WriteMetadata(IOBuffer* buf,
101 int buf_len, 101 int buf_len,
102 const CompletionCallback& callback); 102 const CompletionCallback& callback);
103 103
104 // This transaction is being deleted and we are not done writing to the cache. 104 // This transaction is being deleted and we are not done writing to the cache.
105 // We need to indicate that the response data was truncated. Returns true on 105 // We need to indicate that the response data was truncated. Returns true on
106 // success. Keep in mind that this operation may have side effects, such as 106 // success. Keep in mind that this operation may have side effects, such as
107 // deleting the active entry. 107 // deleting the active entry.
108 bool AddTruncatedFlag(); 108 bool AddTruncatedFlag();
109 109
110 HttpCache::ActiveEntry* entry() { return entry_; } 110 HttpCache::ActiveEntry* entry() const { return entry_; }
111 111
112 // Returns the LoadState of the writer transaction of a given ActiveEntry. In 112 // Returns the LoadState of the writer transaction of a given ActiveEntry. In
113 // other words, returns the LoadState of this transaction without asking the 113 // other words, returns the LoadState of this transaction without asking the
114 // http cache, because this transaction should be the one currently writing 114 // http cache, because this transaction should be the one currently writing
115 // to the cache entry. 115 // to the cache entry.
116 LoadState GetWriterLoadState() const; 116 LoadState GetWriterLoadState() const;
117 117
118 const CompletionCallback& io_callback() { return io_callback_; } 118 const CompletionCallback& io_callback() { return io_callback_; }
119 119
120 base::WeakPtr<Transaction> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }
121
120 const NetLogWithSource& net_log() const; 122 const NetLogWithSource& net_log() const;
121 123
122 // Bypasses the cache lock whenever there is lock contention. 124 // Bypasses the cache lock whenever there is lock contention.
123 void BypassLockForTest() { 125 void BypassLockForTest() {
124 bypass_lock_for_test_ = true; 126 bypass_lock_for_test_ = true;
125 } 127 }
126 128
127 // Generates a failure when attempting to conditionalize a network request. 129 // Generates a failure when attempting to conditionalize a network request.
128 void FailConditionalizationForTest() { 130 void FailConditionalizationForTest() {
129 fail_conditionalization_for_test_ = true; 131 fail_conditionalization_for_test_ = true;
(...skipping 26 matching lines...) Expand all
156 void PopulateNetErrorDetails(NetErrorDetails* details) const override; 158 void PopulateNetErrorDetails(NetErrorDetails* details) const override;
157 void SetPriority(RequestPriority priority) override; 159 void SetPriority(RequestPriority priority) override;
158 void SetWebSocketHandshakeStreamCreateHelper( 160 void SetWebSocketHandshakeStreamCreateHelper(
159 WebSocketHandshakeStreamBase::CreateHelper* create_helper) override; 161 WebSocketHandshakeStreamBase::CreateHelper* create_helper) override;
160 void SetBeforeNetworkStartCallback( 162 void SetBeforeNetworkStartCallback(
161 const BeforeNetworkStartCallback& callback) override; 163 const BeforeNetworkStartCallback& callback) override;
162 void SetBeforeHeadersSentCallback( 164 void SetBeforeHeadersSentCallback(
163 const BeforeHeadersSentCallback& callback) override; 165 const BeforeHeadersSentCallback& callback) override;
164 int ResumeNetworkStart() override; 166 int ResumeNetworkStart() override;
165 void GetConnectionAttempts(ConnectionAttempts* out) const override; 167 void GetConnectionAttempts(ConnectionAttempts* out) const override;
168 RequestPriority priority() const { return priority_; }
169
170 bool shared() const { return shared_; }
171
172 // Sets shared_ to true.
173 void SetShared();
174
175 // Sets shared_ to false. Arguments tell if the transaction will continue to
176 // read from the network and whether the transaction will continue to read
177 // from the cache.
178 void ResetShared(bool continue_network_reading = false,
179 bool continue_cache_reading = false);
180
181 // The transaction should now continue without shared writing and take the
182 // ownership of the network transaction object.
183 void ContinueWithoutSharedWriting(
184 std::unique_ptr<HttpTransaction> network_transaction,
185 bool needs_entry);
186
187 // Returns true if the transaction is eligible for reading from the
188 // network and writing to the cache along with other transactions.
189 // It is eligible if the following conditions are true:
190 // - Request’s mode is READ_WRITE. If mode is only READ then this
191 // transaction should wait until the whole response is in the cache.
192 // - Method is GET.
193 // - It is not a range request.
194 // - It is not a no-store data.
195 bool IsEligibleForSharedWriting() const;
196
197 // Any subsequent Read or IO callback should fail with the given result.
198 void SetSharedWritingFailState(int result);
166 199
167 private: 200 private:
168 static const size_t kNumValidationHeaders = 2; 201 static const size_t kNumValidationHeaders = 2;
169 // Helper struct to pair a header name with its value, for 202 // Helper struct to pair a header name with its value, for
170 // headers used to validate cache entries. 203 // headers used to validate cache entries.
171 struct ValidationHeaders { 204 struct ValidationHeaders {
172 ValidationHeaders() : initialized(false) {} 205 ValidationHeaders() : initialized(false) {}
173 206
174 std::string values[kNumValidationHeaders]; 207 std::string values[kNumValidationHeaders];
175 bool initialized; 208 bool initialized;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 STATE_CACHE_WRITE_RESPONSE, 242 STATE_CACHE_WRITE_RESPONSE,
210 STATE_CACHE_WRITE_RESPONSE_COMPLETE, 243 STATE_CACHE_WRITE_RESPONSE_COMPLETE,
211 STATE_TRUNCATE_CACHED_DATA, 244 STATE_TRUNCATE_CACHED_DATA,
212 STATE_TRUNCATE_CACHED_DATA_COMPLETE, 245 STATE_TRUNCATE_CACHED_DATA_COMPLETE,
213 STATE_TRUNCATE_CACHED_METADATA, 246 STATE_TRUNCATE_CACHED_METADATA,
214 STATE_TRUNCATE_CACHED_METADATA_COMPLETE, 247 STATE_TRUNCATE_CACHED_METADATA_COMPLETE,
215 STATE_PARTIAL_HEADERS_RECEIVED, 248 STATE_PARTIAL_HEADERS_RECEIVED,
216 STATE_CACHE_READ_METADATA, 249 STATE_CACHE_READ_METADATA,
217 STATE_CACHE_READ_METADATA_COMPLETE, 250 STATE_CACHE_READ_METADATA_COMPLETE,
218 251
252 // Start states should always be before Read states.
219 // These states are entered from Read/AddTruncatedFlag. 253 // These states are entered from Read/AddTruncatedFlag.
220 STATE_NETWORK_READ, 254 STATE_NETWORK_READ,
221 STATE_NETWORK_READ_COMPLETE, 255 STATE_NETWORK_READ_COMPLETE,
222 STATE_CACHE_READ_DATA,
223 STATE_CACHE_READ_DATA_COMPLETE,
224 STATE_CACHE_WRITE_DATA, 256 STATE_CACHE_WRITE_DATA,
225 STATE_CACHE_WRITE_DATA_COMPLETE, 257 STATE_CACHE_WRITE_DATA_COMPLETE,
226 STATE_CACHE_WRITE_TRUNCATED_RESPONSE, 258 STATE_CACHE_WRITE_TRUNCATED_RESPONSE,
227 STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE 259 STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE,
260
261 // Shared Writing states.
262 STATE_SHARED_NETWORK_READ,
263 STATE_SHARED_NETWORK_READ_COMPLETE,
264 STATE_SHARED_NETWORK_READ_WAIT_COMPLETE,
265 STATE_SHARED_CACHE_WRITE_DATA,
266 STATE_SHARED_CACHE_WRITE_DATA_COMPLETE,
267
268 // Cache read states, also used for shared writing transactions when they
269 // are behind the data already written to the cache.
270 STATE_CACHE_READ_DATA,
271 STATE_CACHE_READ_DATA_COMPLETE,
272
273 // This state is reached for a shared writing transaction when there is a
274 // failure and any subsequent Read or IO callback should fail.
275 STATE_SHARED_READ_WRITE_FAILED
228 }; 276 };
229 277
230 // Used for categorizing validation triggers in histograms. 278 // Used for categorizing validation triggers in histograms.
231 // NOTE: This enumeration is used in histograms, so please do not add entries 279 // NOTE: This enumeration is used in histograms, so please do not add entries
232 // in the middle. 280 // in the middle.
233 enum ValidationCause { 281 enum ValidationCause {
234 VALIDATION_CAUSE_UNDEFINED, 282 VALIDATION_CAUSE_UNDEFINED,
235 VALIDATION_CAUSE_VARY_MISMATCH, 283 VALIDATION_CAUSE_VARY_MISMATCH,
236 VALIDATION_CAUSE_VALIDATE_FLAG, 284 VALIDATION_CAUSE_VALIDATE_FLAG,
237 VALIDATION_CAUSE_STALE, 285 VALIDATION_CAUSE_STALE,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 int DoCacheReadMetadata(); 332 int DoCacheReadMetadata();
285 int DoCacheReadMetadataComplete(int result); 333 int DoCacheReadMetadataComplete(int result);
286 int DoNetworkRead(); 334 int DoNetworkRead();
287 int DoNetworkReadComplete(int result); 335 int DoNetworkReadComplete(int result);
288 int DoCacheReadData(); 336 int DoCacheReadData();
289 int DoCacheReadDataComplete(int result); 337 int DoCacheReadDataComplete(int result);
290 int DoCacheWriteData(int num_bytes); 338 int DoCacheWriteData(int num_bytes);
291 int DoCacheWriteDataComplete(int result); 339 int DoCacheWriteDataComplete(int result);
292 int DoCacheWriteTruncatedResponse(); 340 int DoCacheWriteTruncatedResponse();
293 int DoCacheWriteTruncatedResponseComplete(int result); 341 int DoCacheWriteTruncatedResponseComplete(int result);
342 int DoSharedNetworkRead();
343 int DoSharedNetworkReadComplete(int result);
344 int DoSharedNetworkReadWaitComplete(int result);
345 int DoSharedCacheWriteData(int num_bytes);
346 int DoSharedCacheWriteDataComplete(int result);
347 int DoSharedReadWriteFailed();
294 348
295 // Sets request_ and fields derived from it. 349 // Sets request_ and fields derived from it.
296 void SetRequest(const NetLogWithSource& net_log, 350 void SetRequest(const NetLogWithSource& net_log,
297 const HttpRequestInfo* request); 351 const HttpRequestInfo* request);
298 352
299 // Returns true if the request should be handled exclusively by the network 353 // Returns true if the request should be handled exclusively by the network
300 // layer (skipping the cache entirely). 354 // layer (skipping the cache entirely).
301 bool ShouldPassThrough(); 355 bool ShouldPassThrough();
302 356
303 // Called to begin reading from the cache. Returns network error code. 357 // Called to begin reading from the cache. Returns network error code.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 421
368 // Helper function, should be called with result of WriteResponseInfoToEntry 422 // Helper function, should be called with result of WriteResponseInfoToEntry
369 // (or the result of the callback, when WriteResponseInfoToEntry returns 423 // (or the result of the callback, when WriteResponseInfoToEntry returns
370 // ERR_IO_PENDING). Calls DoneWritingToEntry if |result| is not the right 424 // ERR_IO_PENDING). Calls DoneWritingToEntry if |result| is not the right
371 // number of bytes. It is expected that the state that calls this will 425 // number of bytes. It is expected that the state that calls this will
372 // return whatever net error code this function returns, which currently 426 // return whatever net error code this function returns, which currently
373 // is always "OK". 427 // is always "OK".
374 int OnWriteResponseInfoToEntryComplete(int result); 428 int OnWriteResponseInfoToEntryComplete(int result);
375 429
376 // Called when we are done writing to the cache entry. 430 // Called when we are done writing to the cache entry.
377 void DoneWritingToEntry(bool success); 431 // perform_entry_cleanup will be false if we just want to set the internal
432 // state of the transaction but do not want to invoke any cleanup operation on
433 // entry_ e.g. when it's already done by a SharedWriter callback function.
434 void DoneWritingToEntry(bool success, bool perform_entry_cleanup = true);
378 435
379 // Returns an error to signal the caller that the current read failed. The 436 // Returns an error to signal the caller that the current read failed. The
380 // current operation |result| is also logged. If |restart| is true, the 437 // current operation |result| is also logged. If |restart| is true, the
381 // transaction should be restarted. 438 // transaction should be restarted.
382 int OnCacheReadError(int result, bool restart); 439 int OnCacheReadError(int result, bool restart);
383 440
384 // Called when the cache lock timeout fires. 441 // Called when the cache lock timeout fires.
385 void OnAddToEntryTimeout(base::TimeTicks start_time); 442 void OnAddToEntryTimeout(base::TimeTicks start_time);
386 443
387 // Deletes the current partial cache entry (sparse), and optionally removes 444 // Deletes the current partial cache entry (sparse), and optionally removes
(...skipping 14 matching lines...) Expand all
402 int DoRestartPartialRequest(); 459 int DoRestartPartialRequest();
403 460
404 // Resets the relavant internal state to remove traces of internal processing 461 // Resets the relavant internal state to remove traces of internal processing
405 // related to range requests. Deletes |partial_| if |delete_object| is true. 462 // related to range requests. Deletes |partial_| if |delete_object| is true.
406 void ResetPartialState(bool delete_object); 463 void ResetPartialState(bool delete_object);
407 464
408 // Resets |network_trans_|, which must be non-NULL. Also updates 465 // Resets |network_trans_|, which must be non-NULL. Also updates
409 // |old_network_trans_load_timing_|, which must be NULL when this is called. 466 // |old_network_trans_load_timing_|, which must be NULL when this is called.
410 void ResetNetworkTransaction(); 467 void ResetNetworkTransaction();
411 468
412 // Returns true if we should bother attempting to resume this request if it is 469 void SaveNetworkTransactionInfo(const HttpTransaction* transaction);
413 // aborted while in progress. If |has_data| is true, the size of the stored 470 void SaveSharedNetworkTransactionInfo();
414 // data is considered for the result. 471
415 bool CanResume(bool has_data); 472 // Returns the currently active network transaction.
473 // At any given point, at most one of the following can be non-null:
474 // - network_trans_(network transaction for headers),
475 // - entry_->shared_writers->network_transaction_ (network transaction for
476 // shared writing of response body).
477 HttpTransaction* GetCurrentNetworkTransaction() const;
416 478
417 // Setter for response_ and auth_response_. It updates its cache entry status, 479 // Setter for response_ and auth_response_. It updates its cache entry status,
418 // if needed. 480 // if needed.
419 void SetResponse(const HttpResponseInfo& new_response); 481 void SetResponse(const HttpResponseInfo& new_response);
420 void SetAuthResponse(const HttpResponseInfo& new_response); 482 void SetAuthResponse(const HttpResponseInfo& new_response);
421 483
422 void UpdateCacheEntryStatus( 484 void UpdateCacheEntryStatus(
423 HttpResponseInfo::CacheEntryStatus new_cache_entry_status); 485 HttpResponseInfo::CacheEntryStatus new_cache_entry_status);
424 486
425 // Sets the response.cache_entry_status to the current cache_entry_status_. 487 // Sets the response.cache_entry_status to the current cache_entry_status_.
426 void SyncCacheEntryStatusToResponse(); 488 void SyncCacheEntryStatusToResponse();
427 void RecordHistograms(); 489 void RecordHistograms();
428 490
491 // Checks if the transaction should create a SharedWriters object or not. If
492 // yes, creates one. Also checks if the transaction should continue to be part
493 // of an already created SharedWriters object or not based on its validation
494 // result.
495 void ProcessForSharedWriting();
496
497 // Returns true if the transaction is shared and still in the headers phase.
498 bool validating_shared() const;
499
500 // Destructor helper to remove the transaction from SharedWriters.
501 void RemoveTransactionFromSharedWriters();
502
503 // Helper function.
504 void OnCacheWriteDataComplete(bool was_shared, int* result);
505
429 // Called to signal completion of asynchronous IO. 506 // Called to signal completion of asynchronous IO.
430 void OnIOComplete(int result); 507 void OnIOComplete(int result);
431 508
432 State next_state_; 509 State next_state_;
433 const HttpRequestInfo* request_; 510 const HttpRequestInfo* request_;
434 RequestPriority priority_; 511 RequestPriority priority_;
435 NetLogWithSource net_log_; 512 NetLogWithSource net_log_;
436 std::unique_ptr<HttpRequestInfo> custom_request_; 513 std::unique_ptr<HttpRequestInfo> custom_request_;
437 HttpRequestHeaders request_headers_copy_; 514 HttpRequestHeaders request_headers_copy_;
515
438 // If extra_headers specified a "if-modified-since" or "if-none-match", 516 // If extra_headers specified a "if-modified-since" or "if-none-match",
439 // |external_validation_| contains the value of those headers. 517 // |external_validation_| contains the value of those headers.
440 ValidationHeaders external_validation_; 518 ValidationHeaders external_validation_;
441 base::WeakPtr<HttpCache> cache_; 519 base::WeakPtr<HttpCache> cache_;
442 HttpCache::ActiveEntry* entry_; 520 HttpCache::ActiveEntry* entry_;
443 HttpCache::ActiveEntry* new_entry_; 521 HttpCache::ActiveEntry* new_entry_;
444 std::unique_ptr<HttpTransaction> network_trans_; 522 std::unique_ptr<HttpTransaction> network_trans_;
445 CompletionCallback callback_; // Consumer's callback. 523 CompletionCallback callback_; // Consumer's callback.
446 HttpResponseInfo response_; 524 HttpResponseInfo response_;
447 HttpResponseInfo auth_response_; 525 HttpResponseInfo auth_response_;
448 const HttpResponseInfo* new_response_; 526 const HttpResponseInfo* new_response_;
449 std::string cache_key_; 527 std::string cache_key_;
450 Mode mode_; 528 Mode mode_;
451 bool reading_; // We are already reading. Never reverts to false once set. 529 bool reading_; // We are already reading. Never reverts to false once set.
452 bool invalid_range_; // We may bypass the cache for this request. 530 bool invalid_range_; // We may bypass the cache for this request.
453 bool truncated_; // We don't have all the response data. 531 bool truncated_; // We don't have all the response data.
454 bool is_sparse_; // The data is stored in sparse byte ranges. 532 bool is_sparse_; // The data is stored in sparse byte ranges.
455 bool range_requested_; // The user requested a byte range. 533 bool range_requested_; // The user requested a byte range.
456 bool handling_206_; // We must deal with this 206 response. 534 bool handling_206_; // We must deal with this 206 response.
457 bool cache_pending_; // We are waiting for the HttpCache. 535 bool cache_pending_; // We are waiting for the HttpCache.
458 bool done_reading_; // All available data was read. 536 bool done_reading_; // All available data was read.
459 bool vary_mismatch_; // The request doesn't match the stored vary data. 537 bool vary_mismatch_; // The request doesn't match the stored vary data.
460 bool couldnt_conditionalize_request_; 538 bool couldnt_conditionalize_request_;
461 bool bypass_lock_for_test_; // A test is exercising the cache lock. 539 bool bypass_lock_for_test_; // A test is exercising the cache lock.
462 bool fail_conditionalization_for_test_; // Fail ConditionalizeRequest. 540 bool fail_conditionalization_for_test_; // Fail ConditionalizeRequest.
541
542 // Reads from the network and writes to the cache using a SharedWriters
543 // object.
544 bool shared_;
545
546 // Used for histograms.
547 bool initiate_shared_writing_;
548
549 // Result to be returned to the consumer on Read invocation when state is
550 // STATE_SHARED_READ_WRITE_FAILED.
551 int shared_read_write_failure_result_;
552
463 scoped_refptr<IOBuffer> read_buf_; 553 scoped_refptr<IOBuffer> read_buf_;
464 int io_buf_len_; 554 int io_buf_len_;
465 int read_offset_; 555 int read_offset_;
466 int effective_load_flags_; 556 int effective_load_flags_;
467 int write_len_; 557 int write_len_;
468 std::unique_ptr<PartialData> partial_; // We are dealing with range requests. 558 std::unique_ptr<PartialData> partial_; // We are dealing with range requests.
469 CompletionCallback io_callback_; 559 CompletionCallback io_callback_;
470 560
471 // Members used to track data for histograms. 561 // Members used to track data for histograms.
472 // This cache_entry_status_ takes precedence over 562 // This cache_entry_status_ takes precedence over
(...skipping 23 matching lines...) Expand all
496 // The helper object to use to create WebSocketHandshakeStreamBase 586 // The helper object to use to create WebSocketHandshakeStreamBase
497 // objects. Only relevant when establishing a WebSocket connection. 587 // objects. Only relevant when establishing a WebSocket connection.
498 // This is passed to the underlying network transaction. It is stored here in 588 // This is passed to the underlying network transaction. It is stored here in
499 // case the transaction does not exist yet. 589 // case the transaction does not exist yet.
500 WebSocketHandshakeStreamBase::CreateHelper* 590 WebSocketHandshakeStreamBase::CreateHelper*
501 websocket_handshake_stream_base_create_helper_; 591 websocket_handshake_stream_base_create_helper_;
502 592
503 BeforeNetworkStartCallback before_network_start_callback_; 593 BeforeNetworkStartCallback before_network_start_callback_;
504 BeforeHeadersSentCallback before_headers_sent_callback_; 594 BeforeHeadersSentCallback before_headers_sent_callback_;
505 595
596 bool have_full_request_headers_;
597 HttpRequestHeaders full_request_headers_;
598
506 base::WeakPtrFactory<Transaction> weak_factory_; 599 base::WeakPtrFactory<Transaction> weak_factory_;
507 600
508 DISALLOW_COPY_AND_ASSIGN(Transaction); 601 DISALLOW_COPY_AND_ASSIGN(Transaction);
509 }; 602 };
510 603
511 } // namespace net 604 } // namespace net
512 605
513 #endif // NET_HTTP_HTTP_CACHE_TRANSACTION_H_ 606 #endif // NET_HTTP_HTTP_CACHE_TRANSACTION_H_
OLDNEW
« no previous file with comments | « net/http/http_cache_shared_writers_unittest.cc ('k') | net/http/http_cache_transaction.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698