blob: 27baa2394f15f34454f643a02e032de9d60a2b81 [file] [log] [blame]
[email protected]93fe75162012-02-09 21:51:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]983a1da2009-11-11 19:59:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file declares HttpCache::Transaction, a private class of HttpCache so
6// it should only be included by http_cache.cc
7
8#ifndef NET_HTTP_HTTP_CACHE_TRANSACTION_H_
9#define NET_HTTP_HTTP_CACHE_TRANSACTION_H_
10
ricead44703672015-06-18 05:47:5711#include <stddef.h>
sclittlefb249892015-09-10 21:33:2212#include <stdint.h>
ricead44703672015-06-18 05:47:5713
danakj1fd259a02016-04-16 03:17:0914#include <memory>
[email protected]13c8a092010-07-29 06:15:4415#include <string>
16
Avi Drissman13fc8932015-12-20 04:40:4617#include "base/macros.h"
ricead44703672015-06-18 05:47:5718#include "base/memory/ref_counted.h"
ricead44703672015-06-18 05:47:5719#include "base/memory/weak_ptr.h"
[email protected]66e96c42013-06-28 15:20:3120#include "base/time/time.h"
Bence Béky046f8c82018-06-12 02:26:0421#include "net/base/completion_once_callback.h"
Bence Béky61f756c2018-04-25 14:17:5322#include "net/base/completion_repeating_callback.h"
ricead44703672015-06-18 05:47:5723#include "net/base/io_buffer.h"
ttuttled9dbc652015-09-29 20:00:5924#include "net/base/ip_endpoint.h"
ricead44703672015-06-18 05:47:5725#include "net/base/load_states.h"
zhongyi48704c182015-12-07 07:52:0226#include "net/base/net_error_details.h"
[email protected]262eec82013-03-19 21:01:3627#include "net/base/request_priority.h"
[email protected]983a1da2009-11-11 19:59:2328#include "net/http/http_cache.h"
[email protected]5aa20132011-04-27 23:11:3429#include "net/http/http_request_headers.h"
ricea64c07d792014-10-08 03:37:0030#include "net/http/http_response_headers.h"
[email protected]66e96c42013-06-28 15:20:3131#include "net/http/http_response_info.h"
[email protected]983a1da2009-11-11 19:59:2332#include "net/http/http_transaction.h"
ricead44703672015-06-18 05:47:5733#include "net/http/partial_data.h"
mikecironef22f9812016-10-04 03:40:1934#include "net/log/net_log_with_source.h"
ttuttle1f2d7e92015-04-28 16:17:4735#include "net/socket/connection_attempts.h"
ricead44703672015-06-18 05:47:5736#include "net/websockets/websocket_handshake_stream_base.h"
[email protected]983a1da2009-11-11 19:59:2337
38namespace net {
39
[email protected]983a1da2009-11-11 19:59:2340class PartialData;
[email protected]82918cc2010-08-25 17:24:5041struct HttpRequestInfo;
[email protected]3b23a222013-05-15 21:33:2542struct LoadTimingInfo;
svaldez7872fd02015-11-19 21:10:5443class SSLPrivateKey;
[email protected]983a1da2009-11-11 19:59:2344
45// This is the transaction that is returned by the HttpCache transaction
46// factory.
shivanishac6582e12017-07-14 22:18:1947class NET_EXPORT_PRIVATE HttpCache::Transaction : public HttpTransaction {
[email protected]983a1da2009-11-11 19:59:2348 public:
[email protected]983a1da2009-11-11 19:59:2349 // The transaction has the following modes, which apply to how it may access
50 // its cache entry.
51 //
52 // o If the mode of the transaction is NONE, then it is in "pass through"
53 // mode and all methods just forward to the inner network transaction.
54 //
55 // o If the mode of the transaction is only READ, then it may only read from
56 // the cache entry.
57 //
58 // o If the mode of the transaction is only WRITE, then it may only write to
59 // the cache entry.
60 //
61 // o If the mode of the transaction is READ_WRITE, then the transaction may
62 // optionally modify the cache entry (e.g., possibly corresponding to
63 // cache validation).
64 //
65 // o If the mode of the transaction is UPDATE, then the transaction may
66 // update existing cache entries, but will never create a new entry or
67 // respond using the entry read from the cache.
68 enum Mode {
69 NONE = 0,
70 READ_META = 1 << 0,
71 READ_DATA = 1 << 1,
72 READ = READ_META | READ_DATA,
73 WRITE = 1 << 2,
74 READ_WRITE = READ | WRITE,
75 UPDATE = READ_META | WRITE, // READ_WRITE & ~READ_DATA
76 };
77
Yao Xiaoa08ecc82019-09-19 20:05:1678 // This enum backs a histogram. Please keep enums.xml up to date with any
79 // changes, and new entries should be appended at the end. Never re-arrange /
80 // re-use values.
81 enum class NetworkIsolationKeyPresent {
82 kNotPresentCacheableRequest = 0,
83 kNotPresentNonCacheableRequest = 1,
84 kPresent = 2,
85 kMaxValue = kPresent,
86 };
87
[email protected]262eec82013-03-19 21:01:3688 Transaction(RequestPriority priority,
[email protected]027bd85a2013-12-27 22:39:1089 HttpCache* cache);
dchengb03027d2014-10-21 12:00:2090 ~Transaction() override;
[email protected]d100e44f2011-01-26 22:47:1191
Shivani Sharmac18f9762017-10-23 16:43:2392 // Virtual so it can be extended for testing.
93 virtual Mode mode() const;
[email protected]983a1da2009-11-11 19:59:2394
shivanisha8061c4202017-06-13 23:35:5295 std::string& method() { return method_; }
96
[email protected]983a1da2009-11-11 19:59:2397 const std::string& key() const { return cache_key_; }
98
[email protected]767c2c9d2012-05-09 23:44:2799 HttpCache::ActiveEntry* entry() { return entry_; }
100
[email protected]fbf50472010-07-15 22:53:53101 // Returns the LoadState of the writer transaction of a given ActiveEntry. In
102 // other words, returns the LoadState of this transaction without asking the
103 // http cache, because this transaction should be the one currently writing
104 // to the cache entry.
105 LoadState GetWriterLoadState() const;
106
Bence Békya4a50932018-08-10 13:39:41107 const CompletionRepeatingCallback& io_callback() { return io_callback_; }
[email protected]7e6a4f32010-02-11 21:06:24108
Steven Binglerb6917f32019-02-09 00:37:24109 void SetIOCallBackForTest(CompletionRepeatingCallback cb) {
110 io_callback_ = cb;
111 }
112
tfarina42834112016-09-22 13:38:20113 const NetLogWithSource& net_log() const;
[email protected]f6f1bebc2011-01-07 03:04:54114
[email protected]8aacaf382014-06-24 05:33:41115 // Bypasses the cache lock whenever there is lock contention.
116 void BypassLockForTest() {
117 bypass_lock_for_test_ = true;
118 }
119
shivanishabc3b71b2017-06-24 07:21:14120 void BypassLockAfterHeadersForTest() {
121 bypass_lock_after_headers_for_test_ = true;
122 }
123
rvargas43dc8fd2015-01-07 23:03:25124 // Generates a failure when attempting to conditionalize a network request.
125 void FailConditionalizationForTest() {
126 fail_conditionalization_for_test_ = true;
127 }
128
[email protected]d100e44f2011-01-26 22:47:11129 // HttpTransaction methods:
dchengb03027d2014-10-21 12:00:20130 int Start(const HttpRequestInfo* request_info,
Bence Béky046f8c82018-06-12 02:26:04131 CompletionOnceCallback callback,
tfarina42834112016-09-22 13:38:20132 const NetLogWithSource& net_log) override;
Bence Béky046f8c82018-06-12 02:26:04133 int RestartIgnoringLastError(CompletionOnceCallback callback) override;
mattm436ccfe2017-06-19 20:24:08134 int RestartWithCertificate(scoped_refptr<X509Certificate> client_cert,
135 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04136 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:20137 int RestartWithAuth(const AuthCredentials& credentials,
Bence Béky046f8c82018-06-12 02:26:04138 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:20139 bool IsReadyToRestartForAuth() override;
140 int Read(IOBuffer* buf,
141 int buf_len,
Bence Béky046f8c82018-06-12 02:26:04142 CompletionOnceCallback callback) override;
dchengb03027d2014-10-21 12:00:20143 void StopCaching() override;
sclittle4de1bab92015-09-22 21:28:24144 int64_t GetTotalReceivedBytes() const override;
sclittlefb249892015-09-10 21:33:22145 int64_t GetTotalSentBytes() const override;
dchengb03027d2014-10-21 12:00:20146 void DoneReading() override;
147 const HttpResponseInfo* GetResponseInfo() const override;
148 LoadState GetLoadState() const override;
dchengb03027d2014-10-21 12:00:20149 void SetQuicServerInfo(QuicServerInfo* quic_server_info) override;
150 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
ttuttled9dbc652015-09-29 20:00:59151 bool GetRemoteEndpoint(IPEndPoint* endpoint) const override;
zhongyi48704c182015-12-07 07:52:02152 void PopulateNetErrorDetails(NetErrorDetails* details) const override;
dchengb03027d2014-10-21 12:00:20153 void SetPriority(RequestPriority priority) override;
154 void SetWebSocketHandshakeStreamCreateHelper(
ttuttle859dc7a2015-04-23 19:42:29155 WebSocketHandshakeStreamBase::CreateHelper* create_helper) override;
dchengb03027d2014-10-21 12:00:20156 void SetBeforeNetworkStartCallback(
mostynbba063d6032014-10-09 11:01:13157 const BeforeNetworkStartCallback& callback) override;
ryansturm49a8cb12016-06-15 16:51:09158 void SetBeforeHeadersSentCallback(
159 const BeforeHeadersSentCallback& callback) override;
Andrey Kosyakov2e893e62017-08-31 17:00:52160 void SetRequestHeadersCallback(RequestHeadersCallback callback) override;
161 void SetResponseHeadersCallback(ResponseHeadersCallback callback) override;
dchengb03027d2014-10-21 12:00:20162 int ResumeNetworkStart() override;
ttuttle1f2d7e92015-04-28 16:17:47163 void GetConnectionAttempts(ConnectionAttempts* out) const override;
[email protected]d100e44f2011-01-26 22:47:11164
shivanisha8061c4202017-06-13 23:35:52165 // Invoked when parallel validation cannot proceed due to response failure
166 // and this transaction needs to be restarted.
167 void SetValidatingCannotProceed();
168
shivanisha36a816e2017-07-05 22:27:45169 // Invoked to remove the association between a transaction waiting to be
170 // added to an entry and the entry.
171 void ResetCachePendingState() { cache_pending_ = false; }
172
xunjielia0166f42017-02-23 17:44:57173 // Returns the estimate of dynamically allocated memory in bytes.
174 size_t EstimateMemoryUsage() const;
175
shivanishac6582e12017-07-14 22:18:19176 RequestPriority priority() const { return priority_; }
177 PartialData* partial() { return partial_.get(); }
Shivani Sharmac18f9762017-10-23 16:43:23178 bool is_truncated() { return truncated_; }
179
180 // Invoked when this writer transaction is about to be removed from entry.
181 // If result is an error code, a future Read should fail with |result|.
182 void WriterAboutToBeRemovedFromEntry(int result);
shivanishac6582e12017-07-14 22:18:19183
Shivani Sharmaedd8dd282017-10-30 14:04:45184 // Invoked when this transaction is about to become a reader because the cache
185 // entry has finished writing.
186 void WriteModeTransactionAboutToBecomeReader();
187
Shivani Sharma8170b772017-12-08 15:59:42188 // Invoked when HttpCache decides whether this transaction should join
189 // parallel writing or create a new writers object. This is then used
190 // for logging metrics. Can be called repeatedly, but doesn't change once the
191 // value has been set to something other than PARALLEL_WRITING_NONE.
192 void MaybeSetParallelWritingPatternForMetrics(ParallelWritingPattern pattern);
193
[email protected]983a1da2009-11-11 19:59:23194 private:
195 static const size_t kNumValidationHeaders = 2;
196 // Helper struct to pair a header name with its value, for
197 // headers used to validate cache entries.
198 struct ValidationHeaders {
199 ValidationHeaders() : initialized(false) {}
200
201 std::string values[kNumValidationHeaders];
shivanisha54d45042017-06-15 16:52:44202 void Reset() {
203 initialized = false;
204 for (auto& value : values)
205 value.clear();
206 }
[email protected]983a1da2009-11-11 19:59:23207 bool initialized;
208 };
209
Shivani Sharmac18f9762017-10-23 16:43:23210 struct NetworkTransactionInfo {
211 NetworkTransactionInfo();
212 ~NetworkTransactionInfo();
213
214 // Load timing information for the last network request, if any. Set in the
215 // 304 and 206 response cases, as the network transaction may be destroyed
216 // before the caller requests load timing information.
217 std::unique_ptr<LoadTimingInfo> old_network_trans_load_timing;
218 int64_t total_received_bytes = 0;
219 int64_t total_sent_bytes = 0;
220 ConnectionAttempts old_connection_attempts;
221 IPEndPoint old_remote_endpoint;
Shivani Sharmac18f9762017-10-23 16:43:23222
223 DISALLOW_COPY_AND_ASSIGN(NetworkTransactionInfo);
224 };
225
[email protected]ab3e58a2009-12-04 00:55:17226 enum State {
jkarlinfa6b5af2017-03-22 16:08:59227 STATE_UNSET,
228
hubbe7b8cfb92015-07-13 21:11:55229 // Normally, states are traversed in approximately this order.
[email protected]ab3e58a2009-12-04 00:55:17230 STATE_NONE,
[email protected]46773162010-05-07 22:31:20231 STATE_GET_BACKEND,
232 STATE_GET_BACKEND_COMPLETE,
[email protected]b68f1b252009-12-05 01:37:18233 STATE_INIT_ENTRY,
Steven Bingler861855dc2019-03-22 20:53:46234 STATE_OPEN_OR_CREATE_ENTRY,
235 STATE_OPEN_OR_CREATE_ENTRY_COMPLETE,
[email protected]ab3e58a2009-12-04 00:55:17236 STATE_DOOM_ENTRY,
237 STATE_DOOM_ENTRY_COMPLETE,
hubbe7b8cfb92015-07-13 21:11:55238 STATE_CREATE_ENTRY,
239 STATE_CREATE_ENTRY_COMPLETE,
[email protected]ab3e58a2009-12-04 00:55:17240 STATE_ADD_TO_ENTRY,
[email protected]7e6a4f32010-02-11 21:06:24241 STATE_ADD_TO_ENTRY_COMPLETE,
shivanisha36a816e2017-07-05 22:27:45242 STATE_DONE_HEADERS_ADD_TO_ENTRY_COMPLETE,
hubbe7b8cfb92015-07-13 21:11:55243 STATE_CACHE_READ_RESPONSE,
244 STATE_CACHE_READ_RESPONSE_COMPLETE,
Dominic Farolinoc78b7632019-08-29 07:56:07245 STATE_WRITE_UPDATED_PREFETCH_RESPONSE,
246 STATE_WRITE_UPDATED_PREFETCH_RESPONSE_COMPLETE,
hubbe7b8cfb92015-07-13 21:11:55247 STATE_CACHE_DISPATCH_VALIDATION,
248 STATE_CACHE_QUERY_DATA,
249 STATE_CACHE_QUERY_DATA_COMPLETE,
[email protected]034740a2010-06-11 17:16:48250 STATE_START_PARTIAL_CACHE_VALIDATION,
251 STATE_COMPLETE_PARTIAL_CACHE_VALIDATION,
Dave Tapuska57b4f9d2018-06-12 01:55:05252 STATE_CACHE_UPDATE_STALE_WHILE_REVALIDATE_TIMEOUT,
253 STATE_CACHE_UPDATE_STALE_WHILE_REVALIDATE_TIMEOUT_COMPLETE,
254 STATE_SETUP_ENTRY_FOR_READ,
hubbe7b8cfb92015-07-13 21:11:55255 STATE_SEND_REQUEST,
256 STATE_SEND_REQUEST_COMPLETE,
257 STATE_SUCCESSFUL_SEND_REQUEST,
[email protected]b79d7ab2009-12-17 18:28:08258 STATE_UPDATE_CACHED_RESPONSE,
hubbe60834542015-07-15 17:41:58259 STATE_CACHE_WRITE_UPDATED_RESPONSE,
260 STATE_CACHE_WRITE_UPDATED_RESPONSE_COMPLETE,
[email protected]b79d7ab2009-12-17 18:28:08261 STATE_UPDATE_CACHED_RESPONSE_COMPLETE,
262 STATE_OVERWRITE_CACHED_RESPONSE,
hubbe7b8cfb92015-07-13 21:11:55263 STATE_CACHE_WRITE_RESPONSE,
hubbe7b8cfb92015-07-13 21:11:55264 STATE_CACHE_WRITE_RESPONSE_COMPLETE,
[email protected]b79d7ab2009-12-17 18:28:08265 STATE_TRUNCATE_CACHED_DATA,
266 STATE_TRUNCATE_CACHED_DATA_COMPLETE,
[email protected]02e7a012010-05-10 23:06:33267 STATE_TRUNCATE_CACHED_METADATA,
268 STATE_TRUNCATE_CACHED_METADATA_COMPLETE,
[email protected]b79d7ab2009-12-17 18:28:08269 STATE_PARTIAL_HEADERS_RECEIVED,
shivanisha8061c4202017-06-13 23:35:52270 STATE_HEADERS_PHASE_CANNOT_PROCEED,
271 STATE_FINISH_HEADERS,
272 STATE_FINISH_HEADERS_COMPLETE,
hubbe7b8cfb92015-07-13 21:11:55273
Shivani Sharmac18f9762017-10-23 16:43:23274 // These states are entered from Read.
275 STATE_NETWORK_READ_CACHE_WRITE,
276 STATE_NETWORK_READ_CACHE_WRITE_COMPLETE,
[email protected]ab3e58a2009-12-04 00:55:17277 STATE_CACHE_READ_DATA,
278 STATE_CACHE_READ_DATA_COMPLETE,
Shivani Sharmac18f9762017-10-23 16:43:23279 // These states are entered if the request should be handled exclusively
280 // by the network layer (skipping the cache entirely).
281 STATE_NETWORK_READ,
282 STATE_NETWORK_READ_COMPLETE,
[email protected]ab3e58a2009-12-04 00:55:17283 };
284
jkarlin9e50a0f2016-05-13 18:05:20285 // Used for categorizing validation triggers in histograms.
286 // NOTE: This enumeration is used in histograms, so please do not add entries
287 // in the middle.
288 enum ValidationCause {
289 VALIDATION_CAUSE_UNDEFINED,
290 VALIDATION_CAUSE_VARY_MISMATCH,
291 VALIDATION_CAUSE_VALIDATE_FLAG,
292 VALIDATION_CAUSE_STALE,
293 VALIDATION_CAUSE_ZERO_FRESHNESS,
294 VALIDATION_CAUSE_MAX
295 };
296
Maks Orlovichd036d7022017-11-17 15:51:18297 enum MemoryEntryDataHints {
298 // If this hint is set, the caching headers indicate we can't do anything
299 // with this entry (unless we are ignoring them thanks to a loadflag),
300 // i.e. it's expired and has nothing that permits validations.
301 HINT_UNUSABLE_PER_CACHING_HEADERS = (1 << 0),
302 };
303
hubbed5a21322015-07-15 17:51:18304 // Runs the state transition loop. Resets and calls |callback_| on exit,
305 // unless the return value is ERR_IO_PENDING.
[email protected]ab3e58a2009-12-04 00:55:17306 int DoLoop(int result);
307
[email protected]b68f1b252009-12-05 01:37:18308 // Each of these methods corresponds to a State value. If there is an
309 // argument, the value corresponds to the return of the previous state or
310 // corresponding callback.
[email protected]46773162010-05-07 22:31:20311 int DoGetBackend();
312 int DoGetBackendComplete(int result);
[email protected]b68f1b252009-12-05 01:37:18313 int DoInitEntry();
Steven Bingler861855dc2019-03-22 20:53:46314 int DoOpenOrCreateEntry();
315 int DoOpenOrCreateEntryComplete(int result);
[email protected]b68f1b252009-12-05 01:37:18316 int DoDoomEntry();
[email protected]7d7ad6e42010-01-14 01:30:53317 int DoDoomEntryComplete(int result);
hubbe7b8cfb92015-07-13 21:11:55318 int DoCreateEntry();
319 int DoCreateEntryComplete(int result);
[email protected]b68f1b252009-12-05 01:37:18320 int DoAddToEntry();
[email protected]7e6a4f32010-02-11 21:06:24321 int DoAddToEntryComplete(int result);
shivanisha36a816e2017-07-05 22:27:45322 int DoDoneHeadersAddToEntryComplete(int result);
hubbe7b8cfb92015-07-13 21:11:55323 int DoCacheReadResponse();
324 int DoCacheReadResponseComplete(int result);
Dominic Farolinoc78b7632019-08-29 07:56:07325 int DoCacheWriteUpdatedPrefetchResponse(int result);
326 int DoCacheWriteUpdatedPrefetchResponseComplete(int result);
hubbe7b8cfb92015-07-13 21:11:55327 int DoCacheDispatchValidation();
328 int DoCacheQueryData();
329 int DoCacheQueryDataComplete(int result);
Dave Tapuska57b4f9d2018-06-12 01:55:05330 int DoCacheUpdateStaleWhileRevalidateTimeout();
331 int DoCacheUpdateStaleWhileRevalidateTimeoutComplete(int result);
332 int DoSetupEntryForRead();
[email protected]034740a2010-06-11 17:16:48333 int DoStartPartialCacheValidation();
334 int DoCompletePartialCacheValidation(int result);
hubbe7b8cfb92015-07-13 21:11:55335 int DoSendRequest();
336 int DoSendRequestComplete(int result);
337 int DoSuccessfulSendRequest();
[email protected]b79d7ab2009-12-17 18:28:08338 int DoUpdateCachedResponse();
hubbe60834542015-07-15 17:41:58339 int DoCacheWriteUpdatedResponse();
340 int DoCacheWriteUpdatedResponseComplete(int result);
[email protected]b79d7ab2009-12-17 18:28:08341 int DoUpdateCachedResponseComplete(int result);
342 int DoOverwriteCachedResponse();
hubbe7b8cfb92015-07-13 21:11:55343 int DoCacheWriteResponse();
hubbe7b8cfb92015-07-13 21:11:55344 int DoCacheWriteResponseComplete(int result);
[email protected]b79d7ab2009-12-17 18:28:08345 int DoTruncateCachedData();
346 int DoTruncateCachedDataComplete(int result);
[email protected]02e7a012010-05-10 23:06:33347 int DoTruncateCachedMetadata();
348 int DoTruncateCachedMetadataComplete(int result);
[email protected]b79d7ab2009-12-17 18:28:08349 int DoPartialHeadersReceived();
shivanishabc3b71b2017-06-24 07:21:14350 int DoHeadersPhaseCannotProceed(int result);
shivanisha8061c4202017-06-13 23:35:52351 int DoFinishHeaders(int result);
352 int DoFinishHeadersComplete(int result);
Shivani Sharmac18f9762017-10-23 16:43:23353 int DoNetworkReadCacheWrite();
354 int DoNetworkReadCacheWriteComplete(int result);
[email protected]b79d7ab2009-12-17 18:28:08355 int DoCacheReadData();
356 int DoCacheReadDataComplete(int result);
Shivani Sharmac18f9762017-10-23 16:43:23357 int DoNetworkRead();
358 int DoNetworkReadComplete(int result);
[email protected]ab3e58a2009-12-04 00:55:17359
shivanishabc3b71b2017-06-24 07:21:14360 // Adds time out handling while waiting to be added to entry or after headers
361 // phase is complete.
362 void AddCacheLockTimeoutHandler(ActiveEntry* entry);
363
[email protected]983a1da2009-11-11 19:59:23364 // Sets request_ and fields derived from it.
shivanisha54d45042017-06-15 16:52:44365 void SetRequest(const NetLogWithSource& net_log);
[email protected]983a1da2009-11-11 19:59:23366
367 // Returns true if the request should be handled exclusively by the network
368 // layer (skipping the cache entirely).
369 bool ShouldPassThrough();
370
371 // Called to begin reading from the cache. Returns network error code.
372 int BeginCacheRead();
373
374 // Called to begin validating the cache entry. Returns network error code.
375 int BeginCacheValidation();
376
377 // Called to begin validating an entry that stores partial content. Returns
378 // a network error code.
379 int BeginPartialCacheValidation();
380
381 // Validates the entry headers against the requested range and continues with
382 // the validation of the rest of the entry. Returns a network error code.
[email protected]4a620712011-07-22 17:41:09383 int ValidateEntryHeadersAndContinue();
[email protected]983a1da2009-11-11 19:59:23384
[email protected]983a1da2009-11-11 19:59:23385 // Called to start requests which were given an "if-modified-since" or
386 // "if-none-match" validation header by the caller (NOT when the request was
387 // conditionalized internally in response to LOAD_VALIDATE_CACHE).
388 // Returns a network error code.
389 int BeginExternallyConditionalizedRequest();
390
[email protected]983a1da2009-11-11 19:59:23391 // Called to restart a network transaction after an error. Returns network
392 // error code.
393 int RestartNetworkRequest();
394
395 // Called to restart a network transaction with a client certificate.
396 // Returns network error code.
mattm436ccfe2017-06-19 20:24:08397 int RestartNetworkRequestWithCertificate(
398 scoped_refptr<X509Certificate> client_cert,
399 scoped_refptr<SSLPrivateKey> client_private_key);
[email protected]983a1da2009-11-11 19:59:23400
401 // Called to restart a network transaction with authentication credentials.
402 // Returns network error code.
[email protected]f3cf9802011-10-28 18:44:58403 int RestartNetworkRequestWithAuth(const AuthCredentials& credentials);
[email protected]983a1da2009-11-11 19:59:23404
Dave Tapuska57b4f9d2018-06-12 01:55:05405 // Called to determine if we need to validate the cache entry before using it,
406 // and whether the validation should be synchronous or asynchronous.
407 ValidationType RequiresValidation();
[email protected]983a1da2009-11-11 19:59:23408
409 // Called to make the request conditional (to ask the server if the cached
410 // copy is valid). Returns true if able to make the request conditional.
411 bool ConditionalizeRequest();
412
Maks Orlovichd036d7022017-11-17 15:51:18413 // Determines if saved response permits conditionalization, and extracts
414 // etag/last-modified values. Only depends on |response_.headers|.
415 // |*etag_value| and |*last_modified_value| will be set if true is returned,
416 // but may also be modified in other cases.
417 bool IsResponseConditionalizable(std::string* etag_value,
418 std::string* last_modified_value) const;
419
Steven Bingler861855dc2019-03-22 20:53:46420 // Returns true if |method_| indicates that we should only try to open an
421 // entry and not attempt to create.
422 bool ShouldOpenOnlyMethods() const;
423
Maks Orlovichd036d7022017-11-17 15:51:18424 // Returns true if the resource info MemoryEntryDataHints bit flags in
425 // |in_memory_info| and the current request & load flags suggest that
426 // the cache entry in question is not actually usable for HTTP
427 // (i.e. already expired, and nothing is forcing us to disregard that).
428 bool MaybeRejectBasedOnEntryInMemoryData(uint8_t in_memory_info);
429
430 // Returns true if response_ is such that, if saved to cache, it would only
431 // be usable if load flags asked us to ignore caching headers.
432 // (return value of false makes no statement as to suitability of the entry).
433 bool ComputeUnusablePerCachingHeaders();
434
[email protected]a189bce2009-12-01 01:59:12435 // Makes sure that a 206 response is expected. Returns true on success.
[email protected]cbf91ef22011-07-19 18:10:06436 // On success, handling_206_ will be set to true if we are processing a
[email protected]a189bce2009-12-01 01:59:12437 // partial entry.
[email protected]cbf91ef22011-07-19 18:10:06438 bool ValidatePartialResponse();
[email protected]983a1da2009-11-11 19:59:23439
440 // Handles a response validation error by bypassing the cache.
441 void IgnoreRangeRequest();
442
rvargasee8204a2015-02-11 21:28:11443 // Fixes the response headers to match expectations for a HEAD request.
[email protected]b53a4122014-07-31 00:06:46444 void FixHeadersForHead();
445
[email protected]983a1da2009-11-11 19:59:23446 // Called to write data to the cache entry. If the write fails, then the
447 // cache entry is destroyed. Future calls to this function will just do
448 // nothing without side-effect. Returns a network error code.
Bence Békya4a50932018-08-10 13:39:41449 int WriteToEntry(int index,
450 int offset,
451 IOBuffer* data,
452 int data_len,
453 CompletionOnceCallback callback);
[email protected]983a1da2009-11-11 19:59:23454
Dominic Farolinoc78b7632019-08-29 07:56:07455 // Called to write a response to the cache entry. |truncated| indicates if the
[email protected]983a1da2009-11-11 19:59:23456 // entry should be marked as incomplete.
Dominic Farolinoc78b7632019-08-29 07:56:07457 int WriteResponseInfoToEntry(const HttpResponseInfo& response,
458 bool truncated);
[email protected]983a1da2009-11-11 19:59:23459
hubbe60834542015-07-15 17:41:58460 // Helper function, should be called with result of WriteResponseInfoToEntry
461 // (or the result of the callback, when WriteResponseInfoToEntry returns
Shivani Sharmac18f9762017-10-23 16:43:23462 // ERR_IO_PENDING). Calls DoneWithEntry if |result| is not the right
hubbe60834542015-07-15 17:41:58463 // number of bytes. It is expected that the state that calls this will
464 // return whatever net error code this function returns, which currently
465 // is always "OK".
466 int OnWriteResponseInfoToEntryComplete(int result);
467
Shivani Sharmac18f9762017-10-23 16:43:23468 // Configures the transaction to read from the network and stop writing to the
469 // entry. It will release the entry if possible. Returns true if caching could
470 // be stopped successfully. It will not be stopped if there are multiple
471 // transactions writing to the cache simultaneously.
472 bool StopCachingImpl(bool success);
473
474 // Informs the HttpCache that this transaction is done with the entry and
475 // changes the mode to NONE. Set |entry_is_complete| to false if the
476 // transaction has not yet finished fully writing or reading the request
477 // to/from the entry. If |entry_is_complete| is false the result may be either
478 // a truncated or a doomed entry based on whether the stored response can be
479 // resumed or not.
480 void DoneWithEntry(bool did_finish);
[email protected]983a1da2009-11-11 19:59:23481
[email protected]93fe75162012-02-09 21:51:31482 // Returns an error to signal the caller that the current read failed. The
[email protected]40caa4c2012-03-20 20:42:58483 // current operation |result| is also logged. If |restart| is true, the
484 // transaction should be restarted.
485 int OnCacheReadError(int result, bool restart);
[email protected]93fe75162012-02-09 21:51:31486
[email protected]8aacaf382014-06-24 05:33:41487 // Called when the cache lock timeout fires.
shivanishabc3b71b2017-06-24 07:21:14488 void OnCacheLockTimeout(base::TimeTicks start_time);
[email protected]8aacaf382014-06-24 05:33:41489
[email protected]983a1da2009-11-11 19:59:23490 // Deletes the current partial cache entry (sparse), and optionally removes
491 // the control object (partial_).
492 void DoomPartialEntry(bool delete_object);
493
[email protected]983a1da2009-11-11 19:59:23494 // Performs the needed work after receiving data from the network, when
495 // working with range requests.
496 int DoPartialNetworkReadCompleted(int result);
497
[email protected]983a1da2009-11-11 19:59:23498 // Performs the needed work after receiving data from the cache, when
499 // working with range requests.
500 int DoPartialCacheReadCompleted(int result);
501
[email protected]9f03cb7a2012-07-30 23:15:20502 // Restarts this transaction after deleting the cached data. It is meant to
503 // be used when the current request cannot be fulfilled due to conflicts
504 // between the byte range request and the cached entry.
505 int DoRestartPartialRequest();
506
rvargasb4991562015-01-07 21:59:57507 // Resets the relavant internal state to remove traces of internal processing
508 // related to range requests. Deletes |partial_| if |delete_object| is true.
509 void ResetPartialState(bool delete_object);
510
[email protected]b8982bf72013-09-11 00:36:55511 // Resets |network_trans_|, which must be non-NULL. Also updates
512 // |old_network_trans_load_timing_|, which must be NULL when this is called.
513 void ResetNetworkTransaction();
514
Shivani Sharmac18f9762017-10-23 16:43:23515 // Returns the currently active network transaction.
516 const HttpTransaction* network_transaction() const;
517 HttpTransaction* network_transaction();
518
Shivani Sharmaedd8dd282017-10-30 14:04:45519 // Returns the network transaction from |this| or from writers only if it was
520 // moved from |this| to writers. This is so that statistics of the network
521 // transaction are not attributed to any other writer member.
522 const HttpTransaction* GetOwnedOrMovedNetworkTransaction() const;
523
jamartin19ff6f1b2016-07-26 23:37:41524 // Returns true if we should bother attempting to resume this request if it is
525 // aborted while in progress. If |has_data| is true, the size of the stored
[email protected]8a301142011-04-13 18:33:40526 // data is considered for the result.
527 bool CanResume(bool has_data);
528
jamartin19ff6f1b2016-07-26 23:37:41529 // Setter for response_ and auth_response_. It updates its cache entry status,
530 // if needed.
531 void SetResponse(const HttpResponseInfo& new_response);
532 void SetAuthResponse(const HttpResponseInfo& new_response);
533
534 void UpdateCacheEntryStatus(
535 HttpResponseInfo::CacheEntryStatus new_cache_entry_status);
536
537 // Sets the response.cache_entry_status to the current cache_entry_status_.
538 void SyncCacheEntryStatusToResponse();
Shivani Sharmac18f9762017-10-23 16:43:23539
540 // Logs histograms for this transaction. It is invoked when the transaction is
541 // either complete or is done writing to entry and will continue in
542 // network-only mode.
[email protected]6e143962012-08-09 21:34:33543 void RecordHistograms();
[email protected]5a07c192012-07-30 20:18:22544
Shivani Sharmac18f9762017-10-23 16:43:23545 // Returns true if this transaction is a member of entry_->writers.
546 bool InWriters() const;
547
shivanisha8061c4202017-06-13 23:35:52548 // Called to signal completion of asynchronous IO. Note that this callback is
549 // used in the conventional sense where one layer calls the callback of the
550 // layer above it e.g. this callback gets called from the network transaction
551 // layer. In addition, it is also used for HttpCache layer to let this
552 // transaction know when it is out of a queued state in ActiveEntry and can
553 // continue its processing.
[email protected]027bd85a2013-12-27 22:39:10554 void OnIOComplete(int result);
555
jkarlin5de449fd2017-03-22 19:31:43556 // When in a DoLoop, use this to set the next state as it verifies that the
557 // state isn't set twice.
558 void TransitionToState(State state);
559
Shivani Sharmac18f9762017-10-23 16:43:23560 // Helper function to decide the next reading state.
561 int TransitionToReadingState();
562
563 // Saves network transaction info using |transaction|.
564 void SaveNetworkTransactionInfo(const HttpTransaction& transaction);
565
Shawn Pickett0b470712019-08-15 00:43:45566 // Disables caching for media content when running on battery.
567 bool ShouldDisableMediaCaching(const HttpResponseHeaders* headers) const;
568
[email protected]ab3e58a2009-12-04 00:55:17569 State next_state_;
shivanisha54d45042017-06-15 16:52:44570
571 // Initial request with which Start() was invoked.
572 const HttpRequestInfo* initial_request_;
573
[email protected]983a1da2009-11-11 19:59:23574 const HttpRequestInfo* request_;
shivanisha54d45042017-06-15 16:52:44575
shivanisha8061c4202017-06-13 23:35:52576 std::string method_;
[email protected]262eec82013-03-19 21:01:36577 RequestPriority priority_;
tfarina42834112016-09-22 13:38:20578 NetLogWithSource net_log_;
danakj1fd259a02016-04-16 03:17:09579 std::unique_ptr<HttpRequestInfo> custom_request_;
[email protected]5aa20132011-04-27 23:11:34580 HttpRequestHeaders request_headers_copy_;
[email protected]983a1da2009-11-11 19:59:23581 // If extra_headers specified a "if-modified-since" or "if-none-match",
582 // |external_validation_| contains the value of those headers.
583 ValidationHeaders external_validation_;
584 base::WeakPtr<HttpCache> cache_;
585 HttpCache::ActiveEntry* entry_;
[email protected]b68f1b252009-12-05 01:37:18586 HttpCache::ActiveEntry* new_entry_;
danakj1fd259a02016-04-16 03:17:09587 std::unique_ptr<HttpTransaction> network_trans_;
Bence Béky046f8c82018-06-12 02:26:04588 CompletionOnceCallback callback_; // Consumer's callback.
[email protected]983a1da2009-11-11 19:59:23589 HttpResponseInfo response_;
590 HttpResponseInfo auth_response_;
Dominic Farolinoc78b7632019-08-29 07:56:07591
592 // This is only populated when we want to modify a prefetch request in some
593 // way for future transactions, while leaving it untouched for the current
594 // one. DoCacheReadResponseComplete() sets this to a copy of |response_|,
595 // and modifies the members for future transactions. Then,
596 // WriteResponseInfoToEntry() writes |updated_prefetch_response_| to the cache
597 // entry if it is populated, or |response_| otherwise. Finally,
598 // WriteResponseInfoToEntry() resets this to base::nullopt.
599 std::unique_ptr<HttpResponseInfo> updated_prefetch_response_;
600
[email protected]b79d7ab2009-12-17 18:28:08601 const HttpResponseInfo* new_response_;
[email protected]983a1da2009-11-11 19:59:23602 std::string cache_key_;
603 Mode mode_;
[email protected]d7358ba2014-01-04 01:39:49604 bool reading_; // We are already reading. Never reverts to false once set.
[email protected]983a1da2009-11-11 19:59:23605 bool invalid_range_; // We may bypass the cache for this request.
[email protected]983a1da2009-11-11 19:59:23606 bool truncated_; // We don't have all the response data.
[email protected]06c351a2010-12-03 19:11:29607 bool is_sparse_; // The data is stored in sparse byte ranges.
[email protected]4a620712011-07-22 17:41:09608 bool range_requested_; // The user requested a byte range.
[email protected]cbf91ef22011-07-19 18:10:06609 bool handling_206_; // We must deal with this 206 response.
[email protected]7d7ad6e42010-01-14 01:30:53610 bool cache_pending_; // We are waiting for the HttpCache.
shivanisha36a816e2017-07-05 22:27:45611
612 // Headers have been received from the network and it's not a match with the
613 // existing entry.
614 bool done_headers_create_new_entry_;
615
[email protected]9bb9f992013-01-11 01:43:16616 bool vary_mismatch_; // The request doesn't match the stored vary data.
[email protected]2ef5d00e2013-03-23 16:17:27617 bool couldnt_conditionalize_request_;
[email protected]8aacaf382014-06-24 05:33:41618 bool bypass_lock_for_test_; // A test is exercising the cache lock.
shivanishabc3b71b2017-06-24 07:21:14619 bool bypass_lock_after_headers_for_test_; // A test is exercising the cache
620 // lock.
rvargas43dc8fd2015-01-07 23:03:25621 bool fail_conditionalization_for_test_; // Fail ConditionalizeRequest.
[email protected]983a1da2009-11-11 19:59:23622 scoped_refptr<IOBuffer> read_buf_;
Shivani Sharma67e27fe02019-08-12 19:34:27623
624 // Length of the buffer passed in Read().
625 int read_buf_len_;
626
[email protected]50482942009-12-21 18:57:01627 int io_buf_len_;
[email protected]983a1da2009-11-11 19:59:23628 int read_offset_;
629 int effective_load_flags_;
danakj1fd259a02016-04-16 03:17:09630 std::unique_ptr<PartialData> partial_; // We are dealing with range requests.
Bence Béky61f756c2018-04-25 14:17:53631 CompletionRepeatingCallback io_callback_;
[email protected]6e143962012-08-09 21:34:33632
Shivani Sharmac18f9762017-10-23 16:43:23633 // Error code to be returned from a subsequent Read call if shared writing
634 // failed in a separate transaction.
635 int shared_writing_error_;
636
[email protected]6e143962012-08-09 21:34:33637 // Members used to track data for histograms.
jamartin19ff6f1b2016-07-26 23:37:41638 // This cache_entry_status_ takes precedence over
639 // response_.cache_entry_status. In fact, response_.cache_entry_status must be
640 // kept in sync with cache_entry_status_ (via SetResponse and
641 // UpdateCacheEntryStatus).
642 HttpResponseInfo::CacheEntryStatus cache_entry_status_;
jkarlin9e50a0f2016-05-13 18:05:20643 ValidationCause validation_cause_;
[email protected]6e143962012-08-09 21:34:33644 base::TimeTicks entry_lock_waiting_since_;
645 base::TimeTicks first_cache_access_since_;
646 base::TimeTicks send_request_since_;
Andrew Comminos1f2ff1cc2018-12-14 05:22:38647 base::TimeTicks read_headers_since_;
jkarlin91ae3602016-05-25 14:05:38648 base::Time open_entry_last_used_;
Maks Orlovichd036d7022017-11-17 15:51:18649 bool cant_conditionalize_zero_freshness_from_memhint_;
Shivani Sharmac18f9762017-10-23 16:43:23650 bool recorded_histograms_;
Shivani Sharma8170b772017-12-08 15:59:42651 ParallelWritingPattern parallel_writing_pattern_;
[email protected]6e143962012-08-09 21:34:33652
Shivani Sharmac18f9762017-10-23 16:43:23653 NetworkTransactionInfo network_transaction_info_;
ttuttle1f2d7e92015-04-28 16:17:47654
Shivani Sharmaedd8dd282017-10-30 14:04:45655 // True if this transaction created the network transaction that is now being
656 // used by writers. This is used to check that only this transaction should
657 // account for the network bytes and other statistics of the network
658 // transaction.
659 // TODO(shivanisha) Note that if this transaction dies mid-way and there are
660 // other writer transactions, no transaction then accounts for those
661 // statistics.
662 bool moved_network_transaction_to_writers_;
663
[email protected]f4533ba2013-11-28 09:35:41664 // The helper object to use to create WebSocketHandshakeStreamBase
665 // objects. Only relevant when establishing a WebSocket connection.
666 // This is passed to the underlying network transaction. It is stored here in
667 // case the transaction does not exist yet.
668 WebSocketHandshakeStreamBase::CreateHelper*
669 websocket_handshake_stream_base_create_helper_;
670
[email protected]1826a402014-01-08 15:40:48671 BeforeNetworkStartCallback before_network_start_callback_;
ryansturm49a8cb12016-06-15 16:51:09672 BeforeHeadersSentCallback before_headers_sent_callback_;
Andrey Kosyakov83a6eee2017-08-14 19:20:04673 RequestHeadersCallback request_headers_callback_;
Andrey Kosyakov2e893e62017-08-31 17:00:52674 ResponseHeadersCallback response_headers_callback_;
[email protected]1826a402014-01-08 15:40:48675
jkarlin5de449fd2017-03-22 19:31:43676 // True if the Transaction is currently processing the DoLoop.
677 bool in_do_loop_;
678
Jeremy Romand54000b22019-07-08 18:40:16679 base::WeakPtrFactory<Transaction> weak_factory_{this};
kulkarni.acd7b4462014-08-28 07:41:34680
[email protected]40f169222013-11-22 09:08:54681 DISALLOW_COPY_AND_ASSIGN(Transaction);
[email protected]983a1da2009-11-11 19:59:23682};
683
684} // namespace net
685
686#endif // NET_HTTP_HTTP_CACHE_TRANSACTION_H_