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