Roll src/third_party/WebKit 90b25ae:5baf25d (svn 200622:200623)
[chromium-blink-merge.git] / net / http / http_transaction.h
blob6b8ea70cb3b679fd1ad1112923819a9869360cc7
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_HTTP_HTTP_TRANSACTION_H_
6 #define NET_HTTP_HTTP_TRANSACTION_H_
8 #include "net/base/completion_callback.h"
9 #include "net/base/load_states.h"
10 #include "net/base/net_export.h"
11 #include "net/base/request_priority.h"
12 #include "net/base/upload_progress.h"
13 #include "net/socket/connection_attempts.h"
14 #include "net/websockets/websocket_handshake_stream_base.h"
16 namespace net {
18 class AuthCredentials;
19 class BoundNetLog;
20 class HttpRequestHeaders;
21 struct HttpRequestInfo;
22 class HttpResponseInfo;
23 class IOBuffer;
24 struct LoadTimingInfo;
25 class ProxyInfo;
26 class QuicServerInfo;
27 class X509Certificate;
29 // Represents a single HTTP transaction (i.e., a single request/response pair).
30 // HTTP redirects are not followed and authentication challenges are not
31 // answered. Cookies are assumed to be managed by the caller.
32 class NET_EXPORT_PRIVATE HttpTransaction {
33 public:
34 // If |*defer| is set to true, the transaction will wait until
35 // ResumeNetworkStart is called before establishing a connection.
36 typedef base::Callback<void(bool* defer)> BeforeNetworkStartCallback;
38 // Provides an opportunity to add proxy-specific request headers. Called after
39 // it is determined that a proxy is being used and before the request headers
40 // are sent. |proxy_info| contains information about the proxy being used,
41 // and additional headers may be added to |request_headers|.
42 typedef base::Callback<void(
43 const ProxyInfo& proxy_info,
44 HttpRequestHeaders* request_headers)> BeforeProxyHeadersSentCallback;
46 // Stops any pending IO and destroys the transaction object.
47 virtual ~HttpTransaction() {}
49 // Starts the HTTP transaction (i.e., sends the HTTP request).
51 // Returns OK if the transaction could be started synchronously, which means
52 // that the request was served from the cache. ERR_IO_PENDING is returned to
53 // indicate that the CompletionCallback will be notified once response info is
54 // available or if an IO error occurs. Any other return value indicates that
55 // the transaction could not be started.
57 // Regardless of the return value, the caller is expected to keep the
58 // request_info object alive until Destroy is called on the transaction.
60 // NOTE: The transaction is not responsible for deleting the callback object.
62 // Profiling information for the request is saved to |net_log| if non-NULL.
63 virtual int Start(const HttpRequestInfo* request_info,
64 const CompletionCallback& callback,
65 const BoundNetLog& net_log) = 0;
67 // Restarts the HTTP transaction, ignoring the last error. This call can
68 // only be made after a call to Start (or RestartIgnoringLastError) failed.
69 // Once Read has been called, this method cannot be called. This method is
70 // used, for example, to continue past various SSL related errors.
72 // Not all errors can be ignored using this method. See error code
73 // descriptions for details about errors that can be ignored.
75 // NOTE: The transaction is not responsible for deleting the callback object.
77 virtual int RestartIgnoringLastError(const CompletionCallback& callback) = 0;
79 // Restarts the HTTP transaction with a client certificate.
80 virtual int RestartWithCertificate(X509Certificate* client_cert,
81 const CompletionCallback& callback) = 0;
83 // Restarts the HTTP transaction with authentication credentials.
84 virtual int RestartWithAuth(const AuthCredentials& credentials,
85 const CompletionCallback& callback) = 0;
87 // Returns true if auth is ready to be continued. Callers should check
88 // this value anytime Start() completes: if it is true, the transaction
89 // can be resumed with RestartWithAuth(L"", L"", callback) to resume
90 // the automatic auth exchange. This notification gives the caller a
91 // chance to process the response headers from all of the intermediate
92 // restarts needed for authentication.
93 virtual bool IsReadyToRestartForAuth() = 0;
95 // Once response info is available for the transaction, response data may be
96 // read by calling this method.
98 // Response data is copied into the given buffer and the number of bytes
99 // copied is returned. ERR_IO_PENDING is returned if response data is not
100 // yet available. The CompletionCallback is notified when the data copy
101 // completes, and it is passed the number of bytes that were successfully
102 // copied. Or, if a read error occurs, the CompletionCallback is notified of
103 // the error. Any other negative return value indicates that the transaction
104 // could not be read.
106 // NOTE: The transaction is not responsible for deleting the callback object.
107 // If the operation is not completed immediately, the transaction must acquire
108 // a reference to the provided buffer.
110 virtual int Read(IOBuffer* buf, int buf_len,
111 const CompletionCallback& callback) = 0;
113 // Stops further caching of this request by the HTTP cache, if there is any.
114 virtual void StopCaching() = 0;
116 // Gets the full request headers sent to the server. This is guaranteed to
117 // work only if Start returns success and the underlying transaction supports
118 // it. (Right now, this is only network transactions, not cache ones.)
120 // Returns true and overwrites headers if it can get the request headers;
121 // otherwise, returns false and does not modify headers.
122 virtual bool GetFullRequestHeaders(HttpRequestHeaders* headers) const = 0;
124 // Get the number of bytes received from network.
125 virtual int64 GetTotalReceivedBytes() const = 0;
127 // Called to tell the transaction that we have successfully reached the end
128 // of the stream. This is equivalent to performing an extra Read() at the end
129 // that should return 0 bytes. This method should not be called if the
130 // transaction is busy processing a previous operation (like a pending Read).
132 // DoneReading may also be called before the first Read() to notify that the
133 // entire response body is to be ignored (e.g., in a redirect).
134 virtual void DoneReading() = 0;
136 // Returns the response info for this transaction. Must not be called until
137 // |Start| completes.
138 virtual const HttpResponseInfo* GetResponseInfo() const = 0;
140 // Returns the load state for this transaction.
141 virtual LoadState GetLoadState() const = 0;
143 // Returns the upload progress in bytes. If there is no upload data,
144 // zero will be returned. This does not include the request headers.
145 virtual UploadProgress GetUploadProgress() const = 0;
147 // SetQuicServerInfo sets a object which reads and writes public information
148 // about a QUIC server.
149 virtual void SetQuicServerInfo(QuicServerInfo* quic_server_info) = 0;
151 // Populates all of load timing, except for request start times and receive
152 // headers time.
153 // |load_timing_info| must have all null times when called. Returns false and
154 // does not modify |load_timing_info| if there's no timing information to
155 // provide.
156 virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const = 0;
158 // Called when the priority of the parent job changes.
159 virtual void SetPriority(RequestPriority priority) = 0;
161 // Set the WebSocketHandshakeStreamBase::CreateHelper to be used for the
162 // request. Only relevant to WebSocket transactions. Must be called before
163 // Start(). Ownership of |create_helper| remains with the caller.
164 virtual void SetWebSocketHandshakeStreamCreateHelper(
165 WebSocketHandshakeStreamBase::CreateHelper* create_helper) = 0;
167 // Set the callback to receive notification just before network use.
168 virtual void SetBeforeNetworkStartCallback(
169 const BeforeNetworkStartCallback& callback) = 0;
171 // Set the callback to receive notification just before a proxy request
172 // is to be sent.
173 virtual void SetBeforeProxyHeadersSentCallback(
174 const BeforeProxyHeadersSentCallback& callback) = 0;
176 // Resumes the transaction after being deferred.
177 virtual int ResumeNetworkStart() = 0;
179 virtual void GetConnectionAttempts(ConnectionAttempts* out) const = 0;
182 } // namespace net
184 #endif // NET_HTTP_HTTP_TRANSACTION_H_