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 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 READ_WRITE = READ | WRITE, | 73 READ_WRITE = READ | WRITE, |
74 UPDATE = READ_META | WRITE, // READ_WRITE & ~READ_DATA | 74 UPDATE = READ_META | WRITE, // READ_WRITE & ~READ_DATA |
75 }; | 75 }; |
76 | 76 |
77 Transaction(RequestPriority priority, | 77 Transaction(RequestPriority priority, |
78 HttpCache* cache); | 78 HttpCache* cache); |
79 ~Transaction() override; | 79 ~Transaction() override; |
80 | 80 |
81 Mode mode() const { return mode_; } | 81 Mode mode() const { return mode_; } |
82 | 82 |
83 std::string& method() { return method_; } | |
84 | |
83 const std::string& key() const { return cache_key_; } | 85 const std::string& key() const { return cache_key_; } |
84 | 86 |
85 // Writes |buf_len| bytes of meta-data from the provided buffer |buf|. to the | 87 // Writes |buf_len| bytes of meta-data from the provided buffer |buf|. to the |
86 // HTTP cache entry that backs this transaction (if any). | 88 // HTTP cache entry that backs this transaction (if any). |
87 // Returns the number of bytes actually written, or a net error code. If the | 89 // Returns the number of bytes actually written, or a net error code. If the |
88 // operation cannot complete immediately, returns ERR_IO_PENDING, grabs a | 90 // operation cannot complete immediately, returns ERR_IO_PENDING, grabs a |
89 // reference to the buffer (until completion), and notifies the caller using | 91 // reference to the buffer (until completion), and notifies the caller using |
90 // the provided |callback| when the operation finishes. | 92 // the provided |callback| when the operation finishes. |
91 // | 93 // |
92 // The first time this method is called for a given transaction, previous | 94 // The first time this method is called for a given transaction, previous |
93 // meta-data will be overwritten with the provided data, and subsequent | 95 // meta-data will be overwritten with the provided data, and subsequent |
94 // invocations will keep appending to the cached entry. | 96 // invocations will keep appending to the cached entry. |
95 // | 97 // |
96 // In order to guarantee that the metadata is set to the correct entry, the | 98 // In order to guarantee that the metadata is set to the correct entry, the |
97 // response (or response info) must be evaluated by the caller, for instance | 99 // response (or response info) must be evaluated by the caller, for instance |
98 // to make sure that the response_time is as expected, before calling this | 100 // to make sure that the response_time is as expected, before calling this |
99 // method. | 101 // method. |
100 int WriteMetadata(IOBuffer* buf, | 102 int WriteMetadata(IOBuffer* buf, |
101 int buf_len, | 103 int buf_len, |
102 const CompletionCallback& callback); | 104 const CompletionCallback& callback); |
103 | 105 |
104 // This transaction is being deleted and we are not done writing to the cache. | 106 // 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 | 107 // 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 | 108 // success. Keep in mind that this operation may have side effects, such as |
107 // deleting the active entry. | 109 // deleting the active entry. This also returns success if the response was |
108 bool AddTruncatedFlag(); | 110 // completely written, |*is_truncated| will be set if it was actually |
jkarlin
2017/06/08 16:14:10
It'd be cleaner if is_truncated were assigned a va
shivanisha
2017/06/08 18:26:02
Done
| |
111 // truncated. | |
112 bool AddTruncatedFlag(bool* is_truncated); | |
jkarlin
2017/06/08 16:14:09
optional nit: rename is_truncated to did_truncate
shivanisha
2017/06/08 18:26:02
done
| |
109 | 113 |
110 HttpCache::ActiveEntry* entry() { return entry_; } | 114 HttpCache::ActiveEntry* entry() { return entry_; } |
111 | 115 |
112 // Returns the LoadState of the writer transaction of a given ActiveEntry. In | 116 // Returns the LoadState of the writer transaction of a given ActiveEntry. In |
113 // other words, returns the LoadState of this transaction without asking the | 117 // other words, returns the LoadState of this transaction without asking the |
114 // http cache, because this transaction should be the one currently writing | 118 // http cache, because this transaction should be the one currently writing |
115 // to the cache entry. | 119 // to the cache entry. |
116 LoadState GetWriterLoadState() const; | 120 LoadState GetWriterLoadState() const; |
117 | 121 |
118 const CompletionCallback& io_callback() { return io_callback_; } | 122 const CompletionCallback& io_callback() { return io_callback_; } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 void SetPriority(RequestPriority priority) override; | 161 void SetPriority(RequestPriority priority) override; |
158 void SetWebSocketHandshakeStreamCreateHelper( | 162 void SetWebSocketHandshakeStreamCreateHelper( |
159 WebSocketHandshakeStreamBase::CreateHelper* create_helper) override; | 163 WebSocketHandshakeStreamBase::CreateHelper* create_helper) override; |
160 void SetBeforeNetworkStartCallback( | 164 void SetBeforeNetworkStartCallback( |
161 const BeforeNetworkStartCallback& callback) override; | 165 const BeforeNetworkStartCallback& callback) override; |
162 void SetBeforeHeadersSentCallback( | 166 void SetBeforeHeadersSentCallback( |
163 const BeforeHeadersSentCallback& callback) override; | 167 const BeforeHeadersSentCallback& callback) override; |
164 int ResumeNetworkStart() override; | 168 int ResumeNetworkStart() override; |
165 void GetConnectionAttempts(ConnectionAttempts* out) const override; | 169 void GetConnectionAttempts(ConnectionAttempts* out) const override; |
166 | 170 |
171 // Invoked when parallel validation cannot proceed due to response failure | |
172 // and this transaction needs to be restarted. | |
173 void SetValidatingCannotProceed(); | |
174 | |
167 // Returns the estimate of dynamically allocated memory in bytes. | 175 // Returns the estimate of dynamically allocated memory in bytes. |
168 size_t EstimateMemoryUsage() const; | 176 size_t EstimateMemoryUsage() const; |
169 | 177 |
170 private: | 178 private: |
171 static const size_t kNumValidationHeaders = 2; | 179 static const size_t kNumValidationHeaders = 2; |
172 // Helper struct to pair a header name with its value, for | 180 // Helper struct to pair a header name with its value, for |
173 // headers used to validate cache entries. | 181 // headers used to validate cache entries. |
174 struct ValidationHeaders { | 182 struct ValidationHeaders { |
175 ValidationHeaders() : initialized(false) {} | 183 ValidationHeaders() : initialized(false) {} |
176 | 184 |
177 std::string values[kNumValidationHeaders]; | 185 std::string values[kNumValidationHeaders]; |
178 bool initialized; | 186 bool initialized; |
179 }; | 187 }; |
180 | 188 |
189 // A snapshot of pieces of the transaction before entering the state machine | |
190 // so that the state can be restored when restarting the state machine. | |
191 struct RestartInfo { | |
192 Mode mode = NONE; | |
193 HttpResponseInfo::CacheEntryStatus cache_entry_status = | |
194 HttpResponseInfo::CacheEntryStatus::ENTRY_UNDEFINED; | |
195 }; | |
196 | |
181 enum State { | 197 enum State { |
182 STATE_UNSET, | 198 STATE_UNSET, |
183 | 199 |
184 // Normally, states are traversed in approximately this order. | 200 // Normally, states are traversed in approximately this order. |
185 STATE_NONE, | 201 STATE_NONE, |
186 STATE_GET_BACKEND, | 202 STATE_GET_BACKEND, |
187 STATE_GET_BACKEND_COMPLETE, | 203 STATE_GET_BACKEND_COMPLETE, |
188 STATE_INIT_ENTRY, | 204 STATE_INIT_ENTRY, |
189 STATE_OPEN_ENTRY, | 205 STATE_OPEN_ENTRY, |
190 STATE_OPEN_ENTRY_COMPLETE, | 206 STATE_OPEN_ENTRY_COMPLETE, |
(...skipping 22 matching lines...) Expand all Loading... | |
213 STATE_OVERWRITE_CACHED_RESPONSE, | 229 STATE_OVERWRITE_CACHED_RESPONSE, |
214 STATE_CACHE_WRITE_RESPONSE, | 230 STATE_CACHE_WRITE_RESPONSE, |
215 STATE_CACHE_WRITE_RESPONSE_COMPLETE, | 231 STATE_CACHE_WRITE_RESPONSE_COMPLETE, |
216 STATE_TRUNCATE_CACHED_DATA, | 232 STATE_TRUNCATE_CACHED_DATA, |
217 STATE_TRUNCATE_CACHED_DATA_COMPLETE, | 233 STATE_TRUNCATE_CACHED_DATA_COMPLETE, |
218 STATE_TRUNCATE_CACHED_METADATA, | 234 STATE_TRUNCATE_CACHED_METADATA, |
219 STATE_TRUNCATE_CACHED_METADATA_COMPLETE, | 235 STATE_TRUNCATE_CACHED_METADATA_COMPLETE, |
220 STATE_PARTIAL_HEADERS_RECEIVED, | 236 STATE_PARTIAL_HEADERS_RECEIVED, |
221 STATE_CACHE_READ_METADATA, | 237 STATE_CACHE_READ_METADATA, |
222 STATE_CACHE_READ_METADATA_COMPLETE, | 238 STATE_CACHE_READ_METADATA_COMPLETE, |
239 STATE_HEADERS_PHASE_CANNOT_PROCEED, | |
240 STATE_FINISH_HEADERS, | |
241 STATE_FINISH_HEADERS_COMPLETE, | |
223 | 242 |
224 // These states are entered from Read/AddTruncatedFlag. | 243 // These states are entered from Read/AddTruncatedFlag. |
225 STATE_NETWORK_READ, | 244 STATE_NETWORK_READ, |
226 STATE_NETWORK_READ_COMPLETE, | 245 STATE_NETWORK_READ_COMPLETE, |
227 STATE_CACHE_READ_DATA, | 246 STATE_CACHE_READ_DATA, |
228 STATE_CACHE_READ_DATA_COMPLETE, | 247 STATE_CACHE_READ_DATA_COMPLETE, |
229 STATE_CACHE_WRITE_DATA, | 248 STATE_CACHE_WRITE_DATA, |
230 STATE_CACHE_WRITE_DATA_COMPLETE, | 249 STATE_CACHE_WRITE_DATA_COMPLETE, |
231 STATE_CACHE_WRITE_TRUNCATED_RESPONSE, | 250 STATE_CACHE_WRITE_TRUNCATED_RESPONSE, |
232 STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE | 251 STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 int DoOverwriteCachedResponse(); | 300 int DoOverwriteCachedResponse(); |
282 int DoCacheWriteResponse(); | 301 int DoCacheWriteResponse(); |
283 int DoCacheWriteResponseComplete(int result); | 302 int DoCacheWriteResponseComplete(int result); |
284 int DoTruncateCachedData(); | 303 int DoTruncateCachedData(); |
285 int DoTruncateCachedDataComplete(int result); | 304 int DoTruncateCachedDataComplete(int result); |
286 int DoTruncateCachedMetadata(); | 305 int DoTruncateCachedMetadata(); |
287 int DoTruncateCachedMetadataComplete(int result); | 306 int DoTruncateCachedMetadataComplete(int result); |
288 int DoPartialHeadersReceived(); | 307 int DoPartialHeadersReceived(); |
289 int DoCacheReadMetadata(); | 308 int DoCacheReadMetadata(); |
290 int DoCacheReadMetadataComplete(int result); | 309 int DoCacheReadMetadataComplete(int result); |
310 int DoHeadersPhaseCannotProceed(); | |
311 int DoFinishHeaders(int result); | |
312 int DoFinishHeadersComplete(int result); | |
291 int DoNetworkRead(); | 313 int DoNetworkRead(); |
292 int DoNetworkReadComplete(int result); | 314 int DoNetworkReadComplete(int result); |
293 int DoCacheReadData(); | 315 int DoCacheReadData(); |
294 int DoCacheReadDataComplete(int result); | 316 int DoCacheReadDataComplete(int result); |
295 int DoCacheWriteData(int num_bytes); | 317 int DoCacheWriteData(int num_bytes); |
296 int DoCacheWriteDataComplete(int result); | 318 int DoCacheWriteDataComplete(int result); |
297 int DoCacheWriteTruncatedResponse(); | 319 int DoCacheWriteTruncatedResponse(); |
298 int DoCacheWriteTruncatedResponseComplete(int result); | 320 int DoCacheWriteTruncatedResponseComplete(int result); |
299 | 321 |
300 // Sets request_ and fields derived from it. | 322 // Sets request_ and fields derived from it. |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
423 void SetResponse(const HttpResponseInfo& new_response); | 445 void SetResponse(const HttpResponseInfo& new_response); |
424 void SetAuthResponse(const HttpResponseInfo& new_response); | 446 void SetAuthResponse(const HttpResponseInfo& new_response); |
425 | 447 |
426 void UpdateCacheEntryStatus( | 448 void UpdateCacheEntryStatus( |
427 HttpResponseInfo::CacheEntryStatus new_cache_entry_status); | 449 HttpResponseInfo::CacheEntryStatus new_cache_entry_status); |
428 | 450 |
429 // Sets the response.cache_entry_status to the current cache_entry_status_. | 451 // Sets the response.cache_entry_status to the current cache_entry_status_. |
430 void SyncCacheEntryStatusToResponse(); | 452 void SyncCacheEntryStatusToResponse(); |
431 void RecordHistograms(); | 453 void RecordHistograms(); |
432 | 454 |
433 // Called to signal completion of asynchronous IO. | 455 // Called to signal completion of asynchronous IO. Note that this callback is |
456 // used in the conventional sense where one layer calls the callback of the | |
457 // layer above it e.g. this callback gets called from the network transaction | |
458 // layer. In addition, it is also used for HttpCache layer to let this | |
459 // transaction know when it is out of a queued state in ActiveEntry and can | |
460 // continue its processing. | |
434 void OnIOComplete(int result); | 461 void OnIOComplete(int result); |
435 | 462 |
436 // When in a DoLoop, use this to set the next state as it verifies that the | 463 // When in a DoLoop, use this to set the next state as it verifies that the |
437 // state isn't set twice. | 464 // state isn't set twice. |
438 void TransitionToState(State state); | 465 void TransitionToState(State state); |
439 | 466 |
440 State next_state_; | 467 State next_state_; |
441 const HttpRequestInfo* request_; | 468 const HttpRequestInfo* request_; |
469 std::string method_; | |
442 RequestPriority priority_; | 470 RequestPriority priority_; |
443 NetLogWithSource net_log_; | 471 NetLogWithSource net_log_; |
444 std::unique_ptr<HttpRequestInfo> custom_request_; | 472 std::unique_ptr<HttpRequestInfo> custom_request_; |
445 HttpRequestHeaders request_headers_copy_; | 473 HttpRequestHeaders request_headers_copy_; |
446 // If extra_headers specified a "if-modified-since" or "if-none-match", | 474 // If extra_headers specified a "if-modified-since" or "if-none-match", |
447 // |external_validation_| contains the value of those headers. | 475 // |external_validation_| contains the value of those headers. |
448 ValidationHeaders external_validation_; | 476 ValidationHeaders external_validation_; |
449 base::WeakPtr<HttpCache> cache_; | 477 base::WeakPtr<HttpCache> cache_; |
450 HttpCache::ActiveEntry* entry_; | 478 HttpCache::ActiveEntry* entry_; |
451 HttpCache::ActiveEntry* new_entry_; | 479 HttpCache::ActiveEntry* new_entry_; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
507 // case the transaction does not exist yet. | 535 // case the transaction does not exist yet. |
508 WebSocketHandshakeStreamBase::CreateHelper* | 536 WebSocketHandshakeStreamBase::CreateHelper* |
509 websocket_handshake_stream_base_create_helper_; | 537 websocket_handshake_stream_base_create_helper_; |
510 | 538 |
511 BeforeNetworkStartCallback before_network_start_callback_; | 539 BeforeNetworkStartCallback before_network_start_callback_; |
512 BeforeHeadersSentCallback before_headers_sent_callback_; | 540 BeforeHeadersSentCallback before_headers_sent_callback_; |
513 | 541 |
514 // True if the Transaction is currently processing the DoLoop. | 542 // True if the Transaction is currently processing the DoLoop. |
515 bool in_do_loop_; | 543 bool in_do_loop_; |
516 | 544 |
545 // Used to restore some members when the state machine is restarted after it | |
546 // has already been added to an entry e.g after |this| has completed | |
547 // validation and the writer transaction fails to completely write the | |
548 // response to the cache. | |
549 RestartInfo restart_info_; | |
550 | |
517 base::WeakPtrFactory<Transaction> weak_factory_; | 551 base::WeakPtrFactory<Transaction> weak_factory_; |
518 | 552 |
519 DISALLOW_COPY_AND_ASSIGN(Transaction); | 553 DISALLOW_COPY_AND_ASSIGN(Transaction); |
520 }; | 554 }; |
521 | 555 |
522 } // namespace net | 556 } // namespace net |
523 | 557 |
524 #endif // NET_HTTP_HTTP_CACHE_TRANSACTION_H_ | 558 #endif // NET_HTTP_HTTP_CACHE_TRANSACTION_H_ |
OLD | NEW |