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 <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.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/source_stream.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; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 | 137 |
| 137 // Gets the remote endpoint that the network stack is currently fetching the | 138 // Gets the remote endpoint that the network stack is currently fetching the |
| 138 // URL from. Returns true and fills in |endpoint| if it is available; returns | 139 // URL from. Returns true and fills in |endpoint| if it is available; returns |
| 139 // false and leaves |endpoint| unchanged if it is unavailable. | 140 // false and leaves |endpoint| unchanged if it is unavailable. |
| 140 virtual bool GetRemoteEndpoint(IPEndPoint* endpoint) const; | 141 virtual bool GetRemoteEndpoint(IPEndPoint* endpoint) const; |
| 141 | 142 |
| 142 // Populates the network error details of the most recent origin that the | 143 // Populates the network error details of the most recent origin that the |
| 143 // network stack makes the request to. | 144 // network stack makes the request to. |
| 144 virtual void PopulateNetErrorDetails(NetErrorDetails* details) const; | 145 virtual void PopulateNetErrorDetails(NetErrorDetails* details) const; |
| 145 | 146 |
| 146 // Called to setup a stream filter for this request. An example of filter is | |
| 147 // content encoding/decoding. | |
| 148 // Subclasses should return the appropriate Filter, or NULL for no Filter. | |
| 149 // This class takes ownership of the returned Filter. | |
| 150 // | |
| 151 // The default implementation returns NULL. | |
| 152 virtual std::unique_ptr<Filter> SetupFilter() const; | |
| 153 | |
| 154 // Called to determine if this response is a redirect. Only makes sense | 147 // Called to determine if this response is a redirect. Only makes sense |
| 155 // for some types of requests. This method returns true if the response | 148 // for some types of requests. This method returns true if the response |
| 156 // is a redirect, and fills in the location param with the URL of the | 149 // is a redirect, and fills in the location param with the URL of the |
| 157 // redirect. The HTTP status code (e.g., 302) is filled into | 150 // redirect. The HTTP status code (e.g., 302) is filled into |
| 158 // |*http_status_code| to signify the type of redirect. | 151 // |*http_status_code| to signify the type of redirect. |
| 159 // | 152 // |
| 160 // The caller is responsible for following the redirect by setting up an | 153 // The caller is responsible for following the redirect by setting up an |
| 161 // appropriate replacement Job. Note that the redirected location may be | 154 // appropriate replacement Job. Note that the redirected location may be |
| 162 // invalid, the caller should be sure it can handle this. | 155 // invalid, the caller should be sure it can handle this. |
| 163 // | 156 // |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 // has failed or the response headers have been received. | 236 // has failed or the response headers have been received. |
| 244 virtual void GetConnectionAttempts(ConnectionAttempts* out) const; | 237 virtual void GetConnectionAttempts(ConnectionAttempts* out) const; |
| 245 | 238 |
| 246 // Given |policy|, |referrer|, and |redirect_destination|, returns the | 239 // Given |policy|, |referrer|, and |redirect_destination|, returns the |
| 247 // referrer URL mandated by |request|'s referrer policy. | 240 // referrer URL mandated by |request|'s referrer policy. |
| 248 static GURL ComputeReferrerForRedirect(URLRequest::ReferrerPolicy policy, | 241 static GURL ComputeReferrerForRedirect(URLRequest::ReferrerPolicy policy, |
| 249 const std::string& referrer, | 242 const std::string& referrer, |
| 250 const GURL& redirect_destination); | 243 const GURL& redirect_destination); |
| 251 | 244 |
| 252 protected: | 245 protected: |
| 246 // Helper method used to perform tasks after reading from |source_stream_| is | |
| 247 // completed. |synchronous| true if the read completed synchronously. | |
| 248 // See the documentation for |Read| above for the contract of this method. | |
| 249 void SourceReadComplete(bool synchronous, int result); | |
|
Randy Smith (Not in Mondays)
2016/08/09 20:28:31
Why is this protected rather than private?
xunjieli
2016/08/15 15:19:08
Done.
| |
| 250 | |
| 253 // Notifies the job that a certificate is requested. | 251 // Notifies the job that a certificate is requested. |
| 254 void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info); | 252 void NotifyCertificateRequested(SSLCertRequestInfo* cert_request_info); |
| 255 | 253 |
| 256 // Notifies the job about an SSL certificate error. | 254 // Notifies the job about an SSL certificate error. |
| 257 void NotifySSLCertificateError(const SSLInfo& ssl_info, bool fatal); | 255 void NotifySSLCertificateError(const SSLInfo& ssl_info, bool fatal); |
| 258 | 256 |
| 259 // Delegates to URLRequest::Delegate. | 257 // Delegates to URLRequest::Delegate. |
| 260 bool CanGetCookies(const CookieList& cookie_list) const; | 258 bool CanGetCookies(const CookieList& cookie_list) const; |
| 261 | 259 |
| 262 // Delegates to URLRequest::Delegate. | 260 // Delegates to URLRequest::Delegate. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 | 298 |
| 301 // Called to tell the job that a filter has successfully reached the end of | 299 // Called to tell the job that a filter has successfully reached the end of |
| 302 // the stream. | 300 // the stream. |
| 303 virtual void DoneReading(); | 301 virtual void DoneReading(); |
| 304 | 302 |
| 305 // Called to tell the job that the body won't be read because it's a redirect. | 303 // Called to tell the job that the body won't be read because it's a redirect. |
| 306 // This is needed so that redirect headers can be cached even though their | 304 // This is needed so that redirect headers can be cached even though their |
| 307 // bodies are never read. | 305 // bodies are never read. |
| 308 virtual void DoneReadingRedirectResponse(); | 306 virtual void DoneReadingRedirectResponse(); |
| 309 | 307 |
| 310 // Reads filtered data from the request. Returns OK if immediately successful, | 308 // Called to set up a SourceStream chain for this request. |
| 311 // ERR_IO_PENDING if the request couldn't complete synchronously, and some | 309 // Subclasses should return the appropriate last SourceStream of the chain, |
| 312 // other error code if the request failed synchronously. Note that this | 310 // or nullptr on error. |
| 313 // function can issue new asynchronous requests if needed, in which case it | 311 virtual std::unique_ptr<SourceStream> SetUpSourceStream(); |
| 314 // returns ERR_IO_PENDING. If this method completes synchronously, | |
| 315 // |*bytes_read| is the number of bytes output by the filter chain if this | |
| 316 // method returns OK, or zero if this method returns an error. | |
| 317 Error ReadFilteredData(int* bytes_read); | |
| 318 | |
| 319 // Whether the response is being filtered in this job. | |
| 320 // Only valid after NotifyHeadersComplete() has been called. | |
| 321 bool HasFilter() { return filter_ != NULL; } | |
| 322 | |
| 323 // At or near destruction time, a derived class may request that the filters | |
| 324 // be destroyed so that statistics can be gathered while the derived class is | |
| 325 // still present to assist in calculations. This is used by URLRequestHttpJob | |
| 326 // to get SDCH to emit stats. | |
| 327 void DestroyFilters(); | |
| 328 | 312 |
| 329 // Provides derived classes with access to the request's network delegate. | 313 // Provides derived classes with access to the request's network delegate. |
| 330 NetworkDelegate* network_delegate() { return network_delegate_; } | 314 NetworkDelegate* network_delegate() { return network_delegate_; } |
| 331 | 315 |
| 332 // The status of the job. | 316 // The status of the job. |
| 333 const URLRequestStatus GetStatus(); | 317 const URLRequestStatus GetStatus(); |
| 334 | 318 |
| 335 // Set the proxy server that was used, if any. | 319 // Set the proxy server that was used, if any. |
| 336 void SetProxyServer(const HostPortPair& proxy_server); | 320 void SetProxyServer(const HostPortPair& proxy_server); |
| 337 | 321 |
| 338 // The number of bytes read after passing through the filter. This value | 322 // The number of bytes read after passing through the filter. This value |
| 339 // reflects bytes read even when there is no filter. | 323 // reflects bytes read even when there is no filter. |
| 340 int64_t postfilter_bytes_read() const { return postfilter_bytes_read_; } | 324 int64_t postfilter_bytes_read() const { return postfilter_bytes_read_; } |
| 341 | 325 |
| 342 // Turns an integer result code into an Error and a count of bytes read. | 326 // Turns an integer result code into an Error and a count of bytes read. |
| 343 // The semantics are: | 327 // The semantics are: |
| 344 // |result| >= 0: |*error| == OK, |*count| == |result| | 328 // |result| >= 0: |*error| == OK, |*count| == |result| |
| 345 // |result| < 0: |*error| = |result|, |*count| == 0 | 329 // |result| < 0: |*error| = |result|, |*count| == 0 |
| 346 static void ConvertResultToError(int result, Error* error, int* count); | 330 static void ConvertResultToError(int result, Error* error, int* count); |
| 347 | 331 |
| 348 // Completion callback for raw reads. See |ReadRawData| for details. | 332 // Completion callback for raw reads. See |ReadRawData| for details. |
| 349 // |bytes_read| is either >= 0 to indicate a successful read and count of | 333 // |bytes_read| is either >= 0 to indicate a successful read and count of |
| 350 // bytes read, or < 0 to indicate an error. | 334 // bytes read, or < 0 to indicate an error. |
| 351 void ReadRawDataComplete(int bytes_read); | 335 void ReadRawDataComplete(int bytes_read); |
| 352 | 336 |
| 353 // The request that initiated this job. This value will never be nullptr. | 337 // The request that initiated this job. This value will never be nullptr. |
| 354 URLRequest* request_; | 338 URLRequest* request_; |
| 355 | 339 |
| 356 private: | 340 private: |
| 341 class URLRequestJobSourceStream; | |
| 342 | |
| 357 // Set the status of the associated URLRequest. | 343 // Set the status of the associated URLRequest. |
| 358 // TODO(mmenke): Make the URLRequest manage its own status. | 344 // TODO(mmenke): Make the URLRequest manage its own status. |
| 359 void SetStatus(const URLRequestStatus& status); | 345 void SetStatus(const URLRequestStatus& status); |
| 360 | 346 |
| 361 // When data filtering is enabled, this function is used to read data | |
| 362 // for the filter. Returns a net error code to indicate if raw data was | |
| 363 // successfully read, an error happened, or the IO is pending. | |
| 364 Error ReadRawDataForFilter(int* bytes_read); | |
| 365 | |
| 366 // Informs the filter chain that data has been read into its buffer. | |
| 367 void PushInputToFilter(int bytes_read); | |
| 368 | |
| 369 // Invokes ReadRawData and records bytes read if the read completes | 347 // Invokes ReadRawData and records bytes read if the read completes |
| 370 // synchronously. | 348 // synchronously. |
| 371 Error ReadRawDataHelper(IOBuffer* buf, int buf_size, int* bytes_read); | 349 int ReadRawDataHelper(IOBuffer* buf, |
| 350 int buf_size, | |
| 351 const CompletionCallback& callback); | |
| 372 | 352 |
| 373 // Called in response to a redirect that was not canceled to follow the | 353 // Called in response to a redirect that was not canceled to follow the |
| 374 // redirect. The current job will be replaced with a new job loading the | 354 // redirect. The current job will be replaced with a new job loading the |
| 375 // given redirect destination. | 355 // given redirect destination. |
| 376 void FollowRedirect(const RedirectInfo& redirect_info); | 356 void FollowRedirect(const RedirectInfo& redirect_info); |
| 377 | 357 |
| 378 // Called after every raw read. If |bytes_read| is > 0, this indicates | 358 // Called after every raw read. If |bytes_read| is > 0, this indicates |
| 379 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read| | 359 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read| |
| 380 // is 0, this indicates that there is no additional data to read. |error| | 360 // is 0, this indicates that there is no additional data to read. |
| 381 // specifies whether an error occurred and no bytes were read. | 361 // If |bytes_read| is negative, no bytes were read. |
| 382 void GatherRawReadStats(Error error, int bytes_read); | 362 void GatherRawReadStats(int bytes_read); |
| 383 | 363 |
| 384 // Updates the profiling info and notifies observers that an additional | 364 // Updates the profiling info and notifies observers that an additional |
| 385 // |bytes_read| unfiltered bytes have been read for this job. | 365 // |bytes_read| unfiltered bytes have been read for this job. |
| 386 void RecordBytesRead(int bytes_read); | 366 void RecordBytesRead(int bytes_read); |
| 387 | 367 |
| 388 // Called to query whether there is data available in the filter to be read | |
| 389 // out. | |
| 390 bool FilterHasData(); | |
| 391 | |
| 392 // NotifyDone marks that request is done. It is really a glorified | 368 // NotifyDone marks that request is done. It is really a glorified |
| 393 // set_status, but also does internal state checking and job tracking. It | 369 // set_status, but also does internal state checking and job tracking. It |
| 394 // should be called once per request, when the job is finished doing all IO. | 370 // should be called once per request, when the job is finished doing all IO. |
| 395 void NotifyDone(const URLRequestStatus& status); | 371 void NotifyDone(const URLRequestStatus& status); |
| 396 | 372 |
| 397 // Some work performed by NotifyDone must be completed asynchronously so | 373 // Some work performed by NotifyDone must be completed asynchronously so |
| 398 // as to avoid re-entering URLRequest::Delegate. This method performs that | 374 // as to avoid re-entering URLRequest::Delegate. This method performs that |
| 399 // work. | 375 // work. |
| 400 void CompleteNotifyDone(); | 376 void CompleteNotifyDone(); |
| 401 | 377 |
| 402 // Subclasses may implement this method to record packet arrival times. | 378 // Subclasses may implement this method to record packet arrival times. |
| 403 // The default implementation does nothing. Only invoked when bytes have been | 379 // The default implementation does nothing. Only invoked when bytes have been |
| 404 // read since the last invocation. | 380 // read since the last invocation. |
| 405 virtual void UpdatePacketReadTimes(); | 381 virtual void UpdatePacketReadTimes(); |
| 406 | 382 |
| 407 // Computes a new RedirectInfo based on receiving a redirect response of | 383 // Computes a new RedirectInfo based on receiving a redirect response of |
| 408 // |location| and |http_status_code|. | 384 // |location| and |http_status_code|. |
| 409 RedirectInfo ComputeRedirectInfo(const GURL& location, int http_status_code); | 385 RedirectInfo ComputeRedirectInfo(const GURL& location, int http_status_code); |
| 410 | 386 |
| 411 // Notify the network delegate that more bytes have been received or sent over | 387 // Notify the network delegate that more bytes have been received or sent over |
| 412 // the network, if bytes have been received or sent since the previous | 388 // the network, if bytes have been received or sent since the previous |
| 413 // notification. | 389 // notification. |
| 414 void MaybeNotifyNetworkBytes(); | 390 void MaybeNotifyNetworkBytes(); |
| 415 | 391 |
| 416 // Indicates that the job is done producing data, either it has completed | 392 // Indicates that the job is done producing data, either it has completed |
| 417 // all the data or an error has been encountered. Set exclusively by | 393 // all the data or an error has been encountered. Set exclusively by |
| 418 // NotifyDone so that it is kept in sync with the request. | 394 // NotifyDone so that it is kept in sync with the request. |
| 419 bool done_; | 395 bool done_; |
| 420 | 396 |
| 397 // Number of raw network bytes read from job subclass. | |
| 421 int64_t prefilter_bytes_read_; | 398 int64_t prefilter_bytes_read_; |
| 399 | |
| 400 // Number of bytes after applying |source_stream_| filters. | |
| 422 int64_t postfilter_bytes_read_; | 401 int64_t postfilter_bytes_read_; |
| 423 | 402 |
| 424 // The data stream filter which is enabled on demand. | 403 // The first SourceStream of the SourceStream chain used. |
| 425 std::unique_ptr<Filter> filter_; | 404 std::unique_ptr<SourceStream> source_stream_; |
| 426 | 405 |
| 427 // If the filter filled its output buffer, then there is a change that it | 406 // Keep a reference to the buffer passed in via URLRequestJob::Read() so it |
| 428 // still has internal data to emit, and this flag is set. | 407 // doesn't get destroyed when the read has not completed. |
| 429 bool filter_needs_more_output_space_; | 408 scoped_refptr<IOBuffer> pending_read_buffer_; |
| 430 | |
| 431 // When we filter data, we receive data into the filter buffers. After | |
| 432 // processing the filtered data, we return the data in the caller's buffer. | |
| 433 // While the async IO is in progress, we save the user buffer here, and | |
| 434 // when the IO completes, we fill this in. | |
| 435 scoped_refptr<IOBuffer> filtered_read_buffer_; | |
| 436 int filtered_read_buffer_len_; | |
| 437 | 409 |
| 438 // We keep a pointer to the read buffer while asynchronous reads are | 410 // We keep a pointer to the read buffer while asynchronous reads are |
| 439 // in progress, so we are able to pass those bytes to job observers. | 411 // in progress, so we are able to pass those bytes to job observers. |
| 440 scoped_refptr<IOBuffer> raw_read_buffer_; | 412 scoped_refptr<IOBuffer> raw_read_buffer_; |
| 441 | 413 |
| 442 // Used by HandleResponseIfNecessary to track whether we've sent the | 414 // Used by HandleResponseIfNecessary to track whether we've sent the |
| 443 // OnResponseStarted callback and potentially redirect callbacks as well. | 415 // OnResponseStarted callback and potentially redirect callbacks as well. |
| 444 bool has_handled_response_; | 416 bool has_handled_response_; |
| 445 | 417 |
| 446 // Expected content size | 418 // Expected content size |
| 447 int64_t expected_content_size_; | 419 int64_t expected_content_size_; |
| 448 | 420 |
| 449 // Set when a redirect is deferred. | 421 // Set when a redirect is deferred. |
| 450 RedirectInfo deferred_redirect_info_; | 422 RedirectInfo deferred_redirect_info_; |
| 451 | 423 |
| 452 // The network delegate to use with this request, if any. | 424 // The network delegate to use with this request, if any. |
| 453 NetworkDelegate* network_delegate_; | 425 NetworkDelegate* network_delegate_; |
| 454 | 426 |
| 455 // The value from GetTotalReceivedBytes() the last time | 427 // The value from GetTotalReceivedBytes() the last time |
| 456 // MaybeNotifyNetworkBytes() was called. Used to calculate how bytes have been | 428 // MaybeNotifyNetworkBytes() was called. Used to calculate how bytes have been |
| 457 // newly received since the last notification. | 429 // newly received since the last notification. |
| 458 int64_t last_notified_total_received_bytes_; | 430 int64_t last_notified_total_received_bytes_; |
| 459 | 431 |
| 460 // The value from GetTotalSentBytes() the last time MaybeNotifyNetworkBytes() | 432 // The value from GetTotalSentBytes() the last time MaybeNotifyNetworkBytes() |
| 461 // was called. Used to calculate how bytes have been newly sent since the last | 433 // was called. Used to calculate how bytes have been newly sent since the last |
| 462 // notification. | 434 // notification. |
| 463 int64_t last_notified_total_sent_bytes_; | 435 int64_t last_notified_total_sent_bytes_; |
| 464 | 436 |
| 437 // Non-null if ReadRawData() returned ERR_IO_PENDING, and the read has not | |
| 438 // completed. | |
| 439 CompletionCallback read_raw_callback_; | |
| 440 | |
| 465 base::WeakPtrFactory<URLRequestJob> weak_factory_; | 441 base::WeakPtrFactory<URLRequestJob> weak_factory_; |
| 466 | 442 |
| 467 DISALLOW_COPY_AND_ASSIGN(URLRequestJob); | 443 DISALLOW_COPY_AND_ASSIGN(URLRequestJob); |
| 468 }; | 444 }; |
| 469 | 445 |
| 470 } // namespace net | 446 } // namespace net |
| 471 | 447 |
| 472 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_ | 448 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_ |
| OLD | NEW |