| 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/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.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_errors.h" |
| 20 #include "net/base/net_export.h" | 21 #include "net/base/net_export.h" |
| 21 #include "net/base/request_priority.h" | 22 #include "net/base/request_priority.h" |
| 22 #include "net/base/upload_progress.h" | 23 #include "net/base/upload_progress.h" |
| 23 #include "net/cookies/canonical_cookie.h" | 24 #include "net/cookies/canonical_cookie.h" |
| 24 #include "net/socket/connection_attempts.h" | 25 #include "net/socket/connection_attempts.h" |
| 25 #include "net/url_request/redirect_info.h" | 26 #include "net/url_request/redirect_info.h" |
| 26 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
| 27 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 28 | 29 |
| 29 namespace net { | 30 namespace net { |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 | 270 |
| 270 // Delegates to URLRequest::Delegate. | 271 // Delegates to URLRequest::Delegate. |
| 271 bool CanEnablePrivacyMode() const; | 272 bool CanEnablePrivacyMode() const; |
| 272 | 273 |
| 273 // Notifies the job that the network is about to be used. | 274 // Notifies the job that the network is about to be used. |
| 274 void NotifyBeforeNetworkStart(bool* defer); | 275 void NotifyBeforeNetworkStart(bool* defer); |
| 275 | 276 |
| 276 // Notifies the job that headers have been received. | 277 // Notifies the job that headers have been received. |
| 277 void NotifyHeadersComplete(); | 278 void NotifyHeadersComplete(); |
| 278 | 279 |
| 279 // Notifies the request that the job has completed a Read operation. | |
| 280 void NotifyReadComplete(int bytes_read); | |
| 281 | |
| 282 // Notifies the request that a start error has occurred. | 280 // Notifies the request that a start error has occurred. |
| 283 void NotifyStartError(const URLRequestStatus& status); | 281 void NotifyStartError(const URLRequestStatus& status); |
| 284 | 282 |
| 285 // NotifyDone marks when we are done with a request. It is really | |
| 286 // a glorified set_status, but also does internal state checking and | |
| 287 // job tracking. It should be called once per request, when the job is | |
| 288 // finished doing all IO. | |
| 289 void NotifyDone(const URLRequestStatus& status); | |
| 290 | |
| 291 // Some work performed by NotifyDone must be completed on a separate task | |
| 292 // so as to avoid re-entering the delegate. This method exists to perform | |
| 293 // that work. | |
| 294 void CompleteNotifyDone(); | |
| 295 | |
| 296 // Used as an asynchronous callback for Kill to notify the URLRequest | 283 // Used as an asynchronous callback for Kill to notify the URLRequest |
| 297 // that we were canceled. | 284 // that we were canceled. |
| 298 void NotifyCanceled(); | 285 void NotifyCanceled(); |
| 299 | 286 |
| 300 // Notifies the job the request should be restarted. | 287 // Notifies the job the request should be restarted. |
| 301 // Should only be called if the job has not started a response. | 288 // Should only be called if the job has not started a response. |
| 302 void NotifyRestartRequired(); | 289 void NotifyRestartRequired(); |
| 303 | 290 |
| 304 // See corresponding functions in url_request.h. | 291 // See corresponding functions in url_request.h. |
| 305 void OnCallToDelegate(); | 292 void OnCallToDelegate(); |
| 306 void OnCallToDelegateComplete(); | 293 void OnCallToDelegateComplete(); |
| 307 | 294 |
| 308 // Called to read raw (pre-filtered) data from this Job. | 295 // Called to read raw (pre-filtered) data from this Job. Reads at most |
| 309 // If returning true, data was read from the job. buf will contain | 296 // |buf_size| bytes into |buf|. |
| 310 // the data, and bytes_read will receive the number of bytes read. | 297 // Possible return values: |
| 311 // If returning true, and bytes_read is returned as 0, there is no | 298 // >= 0: Read completed synchronously. Return value is the number of bytes |
| 312 // additional data to be read. | 299 // read. 0 means eof. |
| 313 // If returning false, an error occurred or an async IO is now pending. | 300 // ERR_IO_PENDING: Read pending asynchronously. |
| 314 // If async IO is pending, the status of the request will be | 301 // When the read completes, |ReadRawDataComplete| should be |
| 315 // URLRequestStatus::IO_PENDING, and buf must remain available until the | 302 // called. |
| 316 // operation is completed. See comments on URLRequest::Read for more | 303 // Any other negative number: Read failed synchronously. Return value is a |
| 317 // info. | 304 // network error code. |
| 318 virtual bool ReadRawData(IOBuffer* buf, int buf_size, int *bytes_read); | 305 // This method might hold onto a reference to |buf| (by incrementing the |
| 306 // refcount) until the method completes or is cancelled. |
| 307 virtual int ReadRawData(IOBuffer* buf, int buf_size); |
| 319 | 308 |
| 320 // Called to tell the job that a filter has successfully reached the end of | 309 // Called to tell the job that a filter has successfully reached the end of |
| 321 // the stream. | 310 // the stream. |
| 322 virtual void DoneReading(); | 311 virtual void DoneReading(); |
| 323 | 312 |
| 324 // Called to tell the job that the body won't be read because it's a redirect. | 313 // Called to tell the job that the body won't be read because it's a redirect. |
| 325 // This is needed so that redirect headers can be cached even though their | 314 // This is needed so that redirect headers can be cached even though their |
| 326 // bodies are never read. | 315 // bodies are never read. |
| 327 virtual void DoneReadingRedirectResponse(); | 316 virtual void DoneReadingRedirectResponse(); |
| 328 | 317 |
| 329 // Informs the filter that data has been read into its buffer | 318 // Reads filtered data from the request. Returns OK if immediately successful, |
| 330 void FilteredDataRead(int bytes_read); | 319 // ERR_IO_PENDING if the request couldn't complete synchronously, and some |
| 331 | 320 // other error code if the request failed synchronously. Note that this |
| 332 // Reads filtered data from the request. Returns true if successful, | 321 // function can issue new asynchronous requests if needed, in which case it |
| 333 // false otherwise. Note, if there is not enough data received to | 322 // returns ERR_IO_PENDING. If this method completes synchronously, |
| 334 // return data, this call can issue a new async IO request under | 323 // |*bytes_read| is the number of bytes output by the filter chain if this |
| 335 // the hood. | 324 // method returns OK, or zero if this method returns an error. |
| 336 bool ReadFilteredData(int *bytes_read); | 325 Error ReadFilteredData(int* bytes_read); |
| 337 | 326 |
| 338 // Whether the response is being filtered in this job. | 327 // Whether the response is being filtered in this job. |
| 339 // Only valid after NotifyHeadersComplete() has been called. | 328 // Only valid after NotifyHeadersComplete() has been called. |
| 340 bool HasFilter() { return filter_ != NULL; } | 329 bool HasFilter() { return filter_ != NULL; } |
| 341 | 330 |
| 342 // At or near destruction time, a derived class may request that the filters | 331 // At or near destruction time, a derived class may request that the filters |
| 343 // be destroyed so that statistics can be gathered while the derived class is | 332 // be destroyed so that statistics can be gathered while the derived class is |
| 344 // still present to assist in calculations. This is used by URLRequestHttpJob | 333 // still present to assist in calculations. This is used by URLRequestHttpJob |
| 345 // to get SDCH to emit stats. | 334 // to get SDCH to emit stats. |
| 346 void DestroyFilters(); | 335 void DestroyFilters(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 358 void SetProxyServer(const HostPortPair& proxy_server); | 347 void SetProxyServer(const HostPortPair& proxy_server); |
| 359 | 348 |
| 360 // The number of bytes read before passing to the filter. This value reflects | 349 // The number of bytes read before passing to the filter. This value reflects |
| 361 // bytes read even when there is no filter. | 350 // bytes read even when there is no filter. |
| 362 int64 prefilter_bytes_read() const { return prefilter_bytes_read_; } | 351 int64 prefilter_bytes_read() const { return prefilter_bytes_read_; } |
| 363 | 352 |
| 364 // The number of bytes read after passing through the filter. This value | 353 // The number of bytes read after passing through the filter. This value |
| 365 // reflects bytes read even when there is no filter. | 354 // reflects bytes read even when there is no filter. |
| 366 int64 postfilter_bytes_read() const { return postfilter_bytes_read_; } | 355 int64 postfilter_bytes_read() const { return postfilter_bytes_read_; } |
| 367 | 356 |
| 357 // Turns an integer result code into an Error and a count of bytes read. |
| 358 // The semantics are: |
| 359 // |result| >= 0: |*error| == OK, |*count| == |result| |
| 360 // |result| < 0: |*error| = |result|, |*count| == 0 |
| 361 static void ConvertResultToError(int result, Error* error, int* count); |
| 362 |
| 363 // Completion callback for raw reads. See |ReadRawData| for details. |
| 364 // |bytes_read| is either >= 0 to indicate a successful read and count of |
| 365 // bytes read, or < 0 to indicate an error. |
| 366 void ReadRawDataComplete(int bytes_read); |
| 367 |
| 368 // The request that initiated this job. This value MAY BE NULL if the | 368 // The request that initiated this job. This value MAY BE NULL if the |
| 369 // request was released by DetachRequest(). | 369 // request was released by DetachRequest(). |
| 370 URLRequest* request_; | 370 URLRequest* request_; |
| 371 | 371 |
| 372 private: | 372 private: |
| 373 // When data filtering is enabled, this function is used to read data | 373 // When data filtering is enabled, this function is used to read data |
| 374 // for the filter. Returns true if raw data was read. Returns false if | 374 // for the filter. Returns a net error code to indicate if raw data was |
| 375 // an error occurred (or we are waiting for IO to complete). | 375 // successfully read, an error happened, or the IO is pending. |
| 376 bool ReadRawDataForFilter(int *bytes_read); | 376 Error ReadRawDataForFilter(int* bytes_read); |
| 377 |
| 378 // Informs the filter chain that data has been read into its buffer. |
| 379 void PushInputToFilter(int bytes_read); |
| 377 | 380 |
| 378 // Invokes ReadRawData and records bytes read if the read completes | 381 // Invokes ReadRawData and records bytes read if the read completes |
| 379 // synchronously. | 382 // synchronously. |
| 380 bool ReadRawDataHelper(IOBuffer* buf, int buf_size, int* bytes_read); | 383 Error ReadRawDataHelper(IOBuffer* buf, int buf_size, int* bytes_read); |
| 381 | 384 |
| 382 // Called in response to a redirect that was not canceled to follow the | 385 // 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 | 386 // redirect. The current job will be replaced with a new job loading the |
| 384 // given redirect destination. | 387 // given redirect destination. |
| 385 void FollowRedirect(const RedirectInfo& redirect_info); | 388 void FollowRedirect(const RedirectInfo& redirect_info); |
| 386 | 389 |
| 387 // Called after every raw read. If |bytes_read| is > 0, this indicates | 390 // Called after every raw read. If |bytes_read| is > 0, this indicates |
| 388 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read| | 391 // a successful read of |bytes_read| unfiltered bytes. If |bytes_read| |
| 389 // is 0, this indicates that there is no additional data to read. If | 392 // is 0, this indicates that there is no additional data to read. |error| |
| 390 // |bytes_read| is < 0, an error occurred and no bytes were read. | 393 // specifies whether an error occurred and no bytes were read. |
| 391 void OnRawReadComplete(int bytes_read); | 394 void GatherRawReadStats(Error error, int bytes_read); |
| 392 | 395 |
| 393 // Updates the profiling info and notifies observers that an additional | 396 // Updates the profiling info and notifies observers that an additional |
| 394 // |bytes_read| unfiltered bytes have been read for this job. | 397 // |bytes_read| unfiltered bytes have been read for this job. |
| 395 void RecordBytesRead(int bytes_read); | 398 void RecordBytesRead(int bytes_read); |
| 396 | 399 |
| 397 // Called to query whether there is data available in the filter to be read | 400 // Called to query whether there is data available in the filter to be read |
| 398 // out. | 401 // out. |
| 399 bool FilterHasData(); | 402 bool FilterHasData(); |
| 400 | 403 |
| 404 // NotifyDone marks that request is done. It is really a glorified |
| 405 // set_status, but also does internal state checking and job tracking. It |
| 406 // should be called once per request, when the job is finished doing all IO. |
| 407 void NotifyDone(const URLRequestStatus& status); |
| 408 |
| 409 // Some work performed by NotifyDone must be completed asynchronously so |
| 410 // as to avoid re-entering URLRequest::Delegate. This method performs that |
| 411 // work. |
| 412 void CompleteNotifyDone(); |
| 413 |
| 401 // Subclasses may implement this method to record packet arrival times. | 414 // Subclasses may implement this method to record packet arrival times. |
| 402 // The default implementation does nothing. Only invoked when bytes have been | 415 // The default implementation does nothing. Only invoked when bytes have been |
| 403 // read since the last invocation. | 416 // read since the last invocation. |
| 404 virtual void UpdatePacketReadTimes(); | 417 virtual void UpdatePacketReadTimes(); |
| 405 | 418 |
| 406 // Computes a new RedirectInfo based on receiving a redirect response of | 419 // Computes a new RedirectInfo based on receiving a redirect response of |
| 407 // |location| and |http_status_code|. | 420 // |location| and |http_status_code|. |
| 408 RedirectInfo ComputeRedirectInfo(const GURL& location, int http_status_code); | 421 RedirectInfo ComputeRedirectInfo(const GURL& location, int http_status_code); |
| 409 | 422 |
| 410 // Notify the network delegate that more bytes have been received or sent over | 423 // Notify the network delegate that more bytes have been received or sent over |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 int64_t last_notified_total_sent_bytes_; | 475 int64_t last_notified_total_sent_bytes_; |
| 463 | 476 |
| 464 base::WeakPtrFactory<URLRequestJob> weak_factory_; | 477 base::WeakPtrFactory<URLRequestJob> weak_factory_; |
| 465 | 478 |
| 466 DISALLOW_COPY_AND_ASSIGN(URLRequestJob); | 479 DISALLOW_COPY_AND_ASSIGN(URLRequestJob); |
| 467 }; | 480 }; |
| 468 | 481 |
| 469 } // namespace net | 482 } // namespace net |
| 470 | 483 |
| 471 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_ | 484 #endif // NET_URL_REQUEST_URL_REQUEST_JOB_H_ |
| OLD | NEW |