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 #ifndef NET_URL_REQUEST_URL_REQUEST_JOB_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_JOB_H_ |
| 6 #define NET_URL_REQUEST_URL_REQUEST_JOB_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_JOB_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/power_monitor/power_observer.h" | 17 #include "base/power_monitor/power_observer.h" |
| 18 #include "net/base/host_port_pair.h" | 18 #include "net/base/host_port_pair.h" |
| 19 #include "net/base/load_states.h" | 19 #include "net/base/load_states.h" |
| 20 #include "net/base/net_error_details.h" | 20 #include "net/base/net_error_details.h" |
| 21 #include "net/base/net_export.h" | 21 #include "net/base/net_export.h" |
| 22 #include "net/base/request_priority.h" | 22 #include "net/base/request_priority.h" |
| 23 #include "net/base/upload_progress.h" | 23 #include "net/base/upload_progress.h" |
| 24 #include "net/cookies/canonical_cookie.h" | 24 #include "net/cookies/canonical_cookie.h" |
| 25 #include "net/filter/stream_source.h" | |
| 25 #include "net/socket/connection_attempts.h" | 26 #include "net/socket/connection_attempts.h" |
| 26 #include "net/url_request/redirect_info.h" | 27 #include "net/url_request/redirect_info.h" |
| 27 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
| 28 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 29 | 30 |
| 30 namespace net { | 31 namespace net { |
| 31 | 32 |
| 32 class AuthChallengeInfo; | 33 class AuthChallengeInfo; |
| 33 class AuthCredentials; | 34 class AuthCredentials; |
| 34 class CookieOptions; | 35 class CookieOptions; |
| 35 class Filter; | 36 class Filter; |
| 36 class HttpRequestHeaders; | 37 class HttpRequestHeaders; |
| 37 class HttpResponseInfo; | 38 class HttpResponseInfo; |
| 38 class IOBuffer; | 39 class IOBuffer; |
| 39 struct LoadTimingInfo; | 40 struct LoadTimingInfo; |
| 40 class NetworkDelegate; | 41 class NetworkDelegate; |
| 41 class SSLCertRequestInfo; | 42 class SSLCertRequestInfo; |
| 42 class SSLInfo; | 43 class SSLInfo; |
| 43 class SSLPrivateKey; | 44 class SSLPrivateKey; |
| 44 class UploadDataStream; | 45 class UploadDataStream; |
| 45 class URLRequestStatus; | 46 class URLRequestStatus; |
| 46 class X509Certificate; | 47 class X509Certificate; |
| 47 | 48 |
| 48 class NET_EXPORT URLRequestJob : public base::PowerObserver { | 49 class NET_EXPORT URLRequestJob : public base::PowerObserver { |
| 49 public: | 50 public: |
| 51 typedef base::Callback<void(net::Error, size_t)> ReadRawCompleteCallback; | |
| 50 explicit URLRequestJob(URLRequest* request, | 52 explicit URLRequestJob(URLRequest* request, |
| 51 NetworkDelegate* network_delegate); | 53 NetworkDelegate* network_delegate); |
| 52 ~URLRequestJob() override; | 54 ~URLRequestJob() override; |
| 53 | 55 |
| 54 // Returns the request that owns this job. | 56 // Returns the request that owns this job. |
| 55 URLRequest* request() const { | 57 URLRequest* request() const { |
| 56 return request_; | 58 return request_; |
| 57 } | 59 } |
| 58 | 60 |
| 59 // Sets the upload data, most requests have no upload data, so this is a NOP. | 61 // Sets the upload data, most requests have no upload data, so this is a NOP. |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 // Populates the network error details of the most recent origin that the | 150 // Populates the network error details of the most recent origin that the |
| 149 // network stack makes the request to. | 151 // network stack makes the request to. |
| 150 virtual void PopulateNetErrorDetails(NetErrorDetails* details) const; | 152 virtual void PopulateNetErrorDetails(NetErrorDetails* details) const; |
| 151 | 153 |
| 152 // Called to setup a stream filter for this request. An example of filter is | 154 // Called to setup a stream filter for this request. An example of filter is |
| 153 // content encoding/decoding. | 155 // content encoding/decoding. |
| 154 // Subclasses should return the appropriate Filter, or NULL for no Filter. | 156 // Subclasses should return the appropriate Filter, or NULL for no Filter. |
| 155 // This class takes ownership of the returned Filter. | 157 // This class takes ownership of the returned Filter. |
| 156 // | 158 // |
| 157 // The default implementation returns NULL. | 159 // The default implementation returns NULL. |
| 158 virtual Filter* SetupFilter() const; | 160 // virtual Filter* SetupFilter() const; |
| 161 | |
| 162 virtual scoped_ptr<StreamSource> SetupSource(); | |
| 159 | 163 |
| 160 // Called to determine if this response is a redirect. Only makes sense | 164 // Called to determine if this response is a redirect. Only makes sense |
| 161 // for some types of requests. This method returns true if the response | 165 // for some types of requests. This method returns true if the response |
| 162 // is a redirect, and fills in the location param with the URL of the | 166 // is a redirect, and fills in the location param with the URL of the |
| 163 // redirect. The HTTP status code (e.g., 302) is filled into | 167 // redirect. The HTTP status code (e.g., 302) is filled into |
| 164 // |*http_status_code| to signify the type of redirect. | 168 // |*http_status_code| to signify the type of redirect. |
| 165 // | 169 // |
| 166 // The caller is responsible for following the redirect by setting up an | 170 // The caller is responsible for following the redirect by setting up an |
| 167 // appropriate replacement Job. Note that the redirected location may be | 171 // appropriate replacement Job. Note that the redirected location may be |
| 168 // invalid, the caller should be sure it can handle this. | 172 // invalid, the caller should be sure it can handle this. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 // has failed or the response headers have been received. | 249 // has failed or the response headers have been received. |
| 246 virtual void GetConnectionAttempts(ConnectionAttempts* out) const; | 250 virtual void GetConnectionAttempts(ConnectionAttempts* out) const; |
| 247 | 251 |
| 248 // Given |policy|, |referrer|, and |redirect_destination|, returns the | 252 // Given |policy|, |referrer|, and |redirect_destination|, returns the |
| 249 // referrer URL mandated by |request|'s referrer policy. | 253 // referrer URL mandated by |request|'s referrer policy. |
| 250 static GURL ComputeReferrerForRedirect(URLRequest::ReferrerPolicy policy, | 254 static GURL ComputeReferrerForRedirect(URLRequest::ReferrerPolicy policy, |
| 251 const std::string& referrer, | 255 const std::string& referrer, |
| 252 const GURL& redirect_destination); | 256 const GURL& redirect_destination); |
| 253 | 257 |
| 254 protected: | 258 protected: |
| 259 // Call: req->NotifyReadCompleted | |
| 260 // By: NotifyReadComplete | |
| 261 void SourceReadComplete(Error error, size_t bytes_read); | |
| 262 | |
| 255 // Notifies the job that a certificate is requested. | 263 // Notifies the job that a certificate is requested. |
| 256 void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info); | 264 void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info); |
| 257 | 265 |
| 258 // Notifies the job about an SSL certificate error. | 266 // Notifies the job about an SSL certificate error. |
| 259 void NotifySSLCertificateError(const SSLInfo& ssl_info, bool fatal); | 267 void NotifySSLCertificateError(const SSLInfo& ssl_info, bool fatal); |
| 260 | 268 |
| 261 // Delegates to URLRequest::Delegate. | 269 // Delegates to URLRequest::Delegate. |
| 262 bool CanGetCookies(const CookieList& cookie_list) const; | 270 bool CanGetCookies(const CookieList& cookie_list) const; |
| 263 | 271 |
| 264 // Delegates to URLRequest::Delegate. | 272 // Delegates to URLRequest::Delegate. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 | 313 |
| 306 // Called to tell the job that a filter has successfully reached the end of | 314 // Called to tell the job that a filter has successfully reached the end of |
| 307 // the stream. | 315 // the stream. |
| 308 virtual void DoneReading(); | 316 virtual void DoneReading(); |
| 309 | 317 |
| 310 // Called to tell the job that the body won't be read because it's a redirect. | 318 // Called to tell the job that the body won't be read because it's a redirect. |
| 311 // This is needed so that redirect headers can be cached even though their | 319 // This is needed so that redirect headers can be cached even though their |
| 312 // bodies are never read. | 320 // bodies are never read. |
| 313 virtual void DoneReadingRedirectResponse(); | 321 virtual void DoneReadingRedirectResponse(); |
| 314 | 322 |
| 315 // Reads filtered data from the request. Returns OK if immediately successful, | |
| 316 // ERR_IO_PENDING if the request couldn't complete synchronously, and some | |
| 317 // other error code if the request failed synchronously. Note that this | |
| 318 // function can issue new asynchronous requests if needed, in which case it | |
| 319 // returns ERR_IO_PENDING. If this method completes synchronously, | |
| 320 // |*bytes_read| is the number of bytes output by the filter chain if this | |
| 321 // method returns OK, or zero if this method returns an error. | |
| 322 Error ReadFilteredData(int* bytes_read); | |
| 323 | |
| 324 // Whether the response is being filtered in this job. | 323 // Whether the response is being filtered in this job. |
| 325 // Only valid after NotifyHeadersComplete() has been called. | 324 // Only valid after NotifyHeadersComplete() has been called. |
| 326 bool HasFilter() { return filter_ != NULL; } | 325 bool HasFilter() { return filter_ != NULL; } |
| 327 | 326 |
| 328 // At or near destruction time, a derived class may request that the filters | 327 // At or near destruction time, a derived class may request that the filters |
| 329 // be destroyed so that statistics can be gathered while the derived class is | 328 // be destroyed so that statistics can be gathered while the derived class is |
| 330 // still present to assist in calculations. This is used by URLRequestHttpJob | 329 // still present to assist in calculations. This is used by URLRequestHttpJob |
| 331 // to get SDCH to emit stats. | 330 // to get SDCH to emit stats. |
| 332 void DestroyFilters(); | 331 void DestroyFilters(); |
| 333 | 332 |
| 334 // Provides derived classes with access to the request's network delegate. | 333 // Provides derived classes with access to the request's network delegate. |
| 335 NetworkDelegate* network_delegate() { return network_delegate_; } | 334 NetworkDelegate* network_delegate() { return network_delegate_; } |
| 336 | 335 |
| 337 // The status of the job. | 336 // The status of the job. |
| 338 const URLRequestStatus GetStatus(); | 337 const URLRequestStatus GetStatus(); |
| 339 | 338 |
| 340 // Set the proxy server that was used, if any. | 339 // Set the proxy server that was used, if any. |
| 341 void SetProxyServer(const HostPortPair& proxy_server); | 340 void SetProxyServer(const HostPortPair& proxy_server); |
| 342 | 341 |
| 343 // The number of bytes read before passing to the filter. This value reflects | 342 // The number of bytes read before passing to the filter. This value reflects |
| 344 // bytes read even when there is no filter. | 343 // bytes read even when there is no filter. |
| 345 int64_t prefilter_bytes_read() const { return prefilter_bytes_read_; } | 344 int64_t prefilter_bytes_read() const; |
| 346 | 345 |
| 347 // The number of bytes read after passing through the filter. This value | 346 // The number of bytes read after passing through the filter. This value |
| 348 // reflects bytes read even when there is no filter. | 347 // reflects bytes read even when there is no filter. |
| 349 int64_t postfilter_bytes_read() const { return postfilter_bytes_read_; } | 348 int64_t postfilter_bytes_read() const; |
| 350 | 349 |
| 351 // Turns an integer result code into an Error and a count of bytes read. | 350 // Turns an integer result code into an Error and a count of bytes read. |
| 352 // The semantics are: | 351 // The semantics are: |
| 353 // |result| >= 0: |*error| == OK, |*count| == |result| | 352 // |result| >= 0: |*error| == OK, |*count| == |result| |
| 354 // |result| < 0: |*error| = |result|, |*count| == 0 | 353 // |result| < 0: |*error| = |result|, |*count| == 0 |
| 355 static void ConvertResultToError(int result, Error* error, int* count); | 354 static void ConvertResultToError(int result, Error* error, int* count); |
| 356 | 355 |
| 357 // Completion callback for raw reads. See |ReadRawData| for details. | 356 // Completion callback for raw reads. See |ReadRawData| for details. |
| 358 // |bytes_read| is either >= 0 to indicate a successful read and count of | 357 // |bytes_read| is either >= 0 to indicate a successful read and count of |
| 359 // bytes read, or < 0 to indicate an error. | 358 // bytes read, or < 0 to indicate an error. |
| 360 void ReadRawDataComplete(int bytes_read); | 359 void ReadRawDataComplete(int bytes_read); |
| 361 | 360 |
| 362 // The request that initiated this job. This value will never be nullptr. | 361 // The request that initiated this job. This value will never be nullptr. |
| 363 URLRequest* request_; | 362 URLRequest* request_; |
| 364 | 363 |
| 365 private: | 364 private: |
| 365 friend class URLRequestJobStreamSource; | |
|
mmenke
2016/02/18 22:58:28
Seems like this should just be an inner class inst
xunjieli
2016/03/03 23:00:09
Done.
| |
| 366 // Set the status of the associated URLRequest. | 366 // Set the status of the associated URLRequest. |
| 367 // TODO(mmenke): Make the URLRequest manage its own status. | 367 // TODO(mmenke): Make the URLRequest manage its own status. |
| 368 void SetStatus(const URLRequestStatus& status); | 368 void SetStatus(const URLRequestStatus& status); |
| 369 | 369 |
| 370 // When data filtering is enabled, this function is used to read data | |
| 371 // for the filter. Returns a net error code to indicate if raw data was | |
| 372 // successfully read, an error happened, or the IO is pending. | |
| 373 Error ReadRawDataForFilter(int* bytes_read); | |
| 374 | |
| 375 // Informs the filter chain that data has been read into its buffer. | 370 // Informs the filter chain that data has been read into its buffer. |
| 376 void PushInputToFilter(int bytes_read); | 371 void PushInputToFilter(int bytes_read); |
| 377 | 372 |
| 378 // Invokes ReadRawData and records bytes read if the read completes | 373 // Invokes ReadRawData and records bytes read if the read completes |
| 379 // synchronously. | 374 // synchronously. |
| 380 Error ReadRawDataHelper(IOBuffer* buf, int buf_size, int* bytes_read); | 375 Error ReadRawDataHelper(IOBuffer* buf, |
| 376 int buf_size, | |
| 377 int* bytes_read, | |
| 378 const StreamSource::OnReadCompleteCallback& callback); | |
| 381 | 379 |
| 382 // Called in response to a redirect that was not canceled to follow the | 380 // Called in response to a redirect that was not canceled to follow the |
| 383 // redirect. The current job will be replaced with a new job loading the | 381 // redirect. The current job will be replaced with a new job loading the |
| 384 // given redirect destination. | 382 // given redirect destination. |
| 385 void FollowRedirect(const RedirectInfo& redirect_info); | 383 void FollowRedirect(const RedirectInfo& redirect_info); |
| 386 | 384 |
| 387 // Called after every raw read. If |bytes_read| is > 0, this indicates | 385 // Called after every raw read. If |bytes_read| is > 0, this indicates |
| 388 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read| | 386 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read| |
| 389 // is 0, this indicates that there is no additional data to read. |error| | 387 // is 0, this indicates that there is no additional data to read. |error| |
| 390 // specifies whether an error occurred and no bytes were read. | 388 // specifies whether an error occurred and no bytes were read. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 420 // Notify the network delegate that more bytes have been received or sent over | 418 // Notify the network delegate that more bytes have been received or sent over |
| 421 // the network, if bytes have been received or sent since the previous | 419 // the network, if bytes have been received or sent since the previous |
| 422 // notification. | 420 // notification. |
| 423 void MaybeNotifyNetworkBytes(); | 421 void MaybeNotifyNetworkBytes(); |
| 424 | 422 |
| 425 // Indicates that the job is done producing data, either it has completed | 423 // Indicates that the job is done producing data, either it has completed |
| 426 // all the data or an error has been encountered. Set exclusively by | 424 // all the data or an error has been encountered. Set exclusively by |
| 427 // NotifyDone so that it is kept in sync with the request. | 425 // NotifyDone so that it is kept in sync with the request. |
| 428 bool done_; | 426 bool done_; |
| 429 | 427 |
| 430 int64_t prefilter_bytes_read_; | |
| 431 int64_t postfilter_bytes_read_; | |
| 432 | |
| 433 // The data stream filter which is enabled on demand. | 428 // The data stream filter which is enabled on demand. |
| 434 scoped_ptr<Filter> filter_; | 429 scoped_ptr<Filter> filter_; |
| 430 // TODO doc | |
| 431 scoped_ptr<StreamSource> source_; | |
| 435 | 432 |
| 436 // If the filter filled its output buffer, then there is a change that it | 433 // If the filter filled its output buffer, then there is a change that it |
| 437 // still has internal data to emit, and this flag is set. | 434 // still has internal data to emit, and this flag is set. |
| 438 bool filter_needs_more_output_space_; | 435 bool filter_needs_more_output_space_; |
| 439 | 436 |
| 440 // When we filter data, we receive data into the filter buffers. After | 437 // When we filter data, we receive data into the filter buffers. After |
| 441 // processing the filtered data, we return the data in the caller's buffer. | 438 // processing the filtered data, we return the data in the caller's buffer. |
| 442 // While the async IO is in progress, we save the user buffer here, and | 439 // While the async IO is in progress, we save the user buffer here, and |
| 443 // when the IO completes, we fill this in. | 440 // when the IO completes, we fill this in. |
| 444 scoped_refptr<IOBuffer> filtered_read_buffer_; | 441 scoped_refptr<IOBuffer> filtered_read_buffer_; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 464 // The value from GetTotalReceivedBytes() the last time | 461 // The value from GetTotalReceivedBytes() the last time |
| 465 // MaybeNotifyNetworkBytes() was called. Used to calculate how bytes have been | 462 // MaybeNotifyNetworkBytes() was called. Used to calculate how bytes have been |
| 466 // newly received since the last notification. | 463 // newly received since the last notification. |
| 467 int64_t last_notified_total_received_bytes_; | 464 int64_t last_notified_total_received_bytes_; |
| 468 | 465 |
| 469 // The value from GetTotalSentBytes() the last time MaybeNotifyNetworkBytes() | 466 // The value from GetTotalSentBytes() the last time MaybeNotifyNetworkBytes() |
| 470 // was called. Used to calculate how bytes have been newly sent since the last | 467 // was called. Used to calculate how bytes have been newly sent since the last |
| 471 // notification. | 468 // notification. |
| 472 int64_t last_notified_total_sent_bytes_; | 469 int64_t last_notified_total_sent_bytes_; |
| 473 | 470 |
| 471 ReadRawCompleteCallback read_raw_callback_; | |
| 472 | |
| 473 // Raw network bytes read from job subclass. | |
| 474 size_t raw_bytes_read_; | |
| 475 | |
| 474 base::WeakPtrFactory<URLRequestJob> weak_factory_; | 476 base::WeakPtrFactory<URLRequestJob> weak_factory_; |
| 475 | 477 |
| 476 DISALLOW_COPY_AND_ASSIGN(URLRequestJob); | 478 DISALLOW_COPY_AND_ASSIGN(URLRequestJob); |
| 477 }; | 479 }; |
| 478 | 480 |
| 479 } // namespace net | 481 } // namespace net |
| 480 | 482 |
| 481 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_ | 483 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_ |
| OLD | NEW |