Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: net/url_request/url_request.h

Issue 2262653003: Make URLRequest::Read to return net errors or bytes read instead of a bool (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_H_ 5 #ifndef NET_URL_REQUEST_URL_REQUEST_H_
6 #define NET_URL_REQUEST_URL_REQUEST_H_ 6 #define NET_URL_REQUEST_URL_REQUEST_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 // indicating what's wrong with the certificate. 189 // indicating what's wrong with the certificate.
190 // If |fatal| is true then the host in question demands a higher level 190 // If |fatal| is true then the host in question demands a higher level
191 // of security (due e.g. to HTTP Strict Transport Security, user 191 // of security (due e.g. to HTTP Strict Transport Security, user
192 // preference, or built-in policy). In this case, errors must not be 192 // preference, or built-in policy). In this case, errors must not be
193 // bypassable by the user. 193 // bypassable by the user.
194 virtual void OnSSLCertificateError(URLRequest* request, 194 virtual void OnSSLCertificateError(URLRequest* request,
195 const SSLInfo& ssl_info, 195 const SSLInfo& ssl_info,
196 bool fatal); 196 bool fatal);
197 197
198 // After calling Start(), the delegate will receive an OnResponseStarted 198 // After calling Start(), the delegate will receive an OnResponseStarted
199 // callback when the request has completed. If an error occurred, the 199 // callback when the request has completed. |net_error| will be set to OK
200 // request->status() will be set. On success, all redirects have been 200 // or an actual net error. On success, all redirects have been
201 // followed and the final response is beginning to arrive. At this point, 201 // followed and the final response is beginning to arrive. At this point,
202 // meta data about the response is available, including for example HTTP 202 // meta data about the response is available, including for example HTTP
203 // response headers if this is a request for a HTTP resource. 203 // response headers if this is a request for a HTTP resource.
204 virtual void OnResponseStarted(URLRequest* request) = 0; 204 virtual void OnResponseStarted(URLRequest* request, int net_error);
205 // Deprecated.
206 // TODO(maksims): Remove this;
207 virtual void OnResponseStarted(URLRequest* request);
205 208
206 // Called when the a Read of the response body is completed after an 209 // Called when the a Read of the response body is completed after an
207 // IO_PENDING status from a Read() call. 210 // IO_PENDING status from a Read() call.
208 // The data read is filled into the buffer which the caller passed 211 // The data read is filled into the buffer which the caller passed
209 // to Read() previously. 212 // to Read() previously.
210 // 213 //
211 // If an error occurred, request->status() will contain the error, 214 // If an error occurred, |bytes_read| will be set to the error.
212 // and bytes read will be -1.
213 virtual void OnReadCompleted(URLRequest* request, int bytes_read) = 0; 215 virtual void OnReadCompleted(URLRequest* request, int bytes_read) = 0;
214 216
217 // Used to call OnResponseStarted(). There are two the same methods. The one
218 // that has |net_error| in arguments is a new one. This method is used to
219 // distinguish whether clients have already overridden new OnResponseStarted
220 // and call that one instead of old one without |net_error|.
221 //
222 // The method will be removed as soon as all clients are modified.
223 void NotifyOnResponseStarted(URLRequest* request, int net_error);
mmenke 2016/08/30 22:09:12 This method doesn't exist any more.
maksims (do not use this acc) 2016/08/31 11:16:59 Done.
224
215 protected: 225 protected:
216 virtual ~Delegate() {} 226 virtual ~Delegate() {}
227
228 private:
229 // Used to distinguish whether modified a method is overridden by clients.
230 bool implemented_;
mmenke 2016/08/30 22:09:12 Not used.
maksims (do not use this acc) 2016/08/31 11:16:59 Done.
217 }; 231 };
218 232
219 // If destroyed after Start() has been called but while IO is pending, 233 // If destroyed after Start() has been called but while IO is pending,
220 // then the request will be effectively canceled and the delegate 234 // then the request will be effectively canceled and the delegate
221 // will not have any more of its methods called. 235 // will not have any more of its methods called.
222 ~URLRequest() override; 236 ~URLRequest() override;
223 237
224 // Changes the default cookie policy from allowing all cookies to blocking all 238 // Changes the default cookie policy from allowing all cookies to blocking all
225 // cookies. Embedders that want to implement a more flexible policy should 239 // cookies. Embedders that want to implement a more flexible policy should
226 // change the default to blocking all cookies, and provide a NetworkDelegate 240 // change the default to blocking all cookies, and provide a NetworkDelegate
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
517 void SetLoadFlags(int flags); 531 void SetLoadFlags(int flags);
518 532
519 // Returns true if the request is "pending" (i.e., if Start() has been called, 533 // Returns true if the request is "pending" (i.e., if Start() has been called,
520 // and the response has not yet been called). 534 // and the response has not yet been called).
521 bool is_pending() const { return is_pending_; } 535 bool is_pending() const { return is_pending_; }
522 536
523 // Returns true if the request is in the process of redirecting to a new 537 // Returns true if the request is in the process of redirecting to a new
524 // URL but has not yet initiated the new request. 538 // URL but has not yet initiated the new request.
525 bool is_redirecting() const { return is_redirecting_; } 539 bool is_redirecting() const { return is_redirecting_; }
526 540
527 // Returns the error status of the request.
528 const URLRequestStatus& status() const { return status_; }
529
530 // Returns a globally unique identifier for this request. 541 // Returns a globally unique identifier for this request.
531 uint64_t identifier() const { return identifier_; } 542 uint64_t identifier() const { return identifier_; }
532 543
533 // This method is called to start the request. The delegate will receive 544 // This method is called to start the request. The delegate will receive
534 // a OnResponseStarted callback when the request is started. The request 545 // a OnResponseStarted callback when the request is started. The request
535 // must have a delegate set before this method is called. 546 // must have a delegate set before this method is called.
536 void Start(); 547 void Start();
537 548
538 // This method may be called at any time after Start() has been called to 549 // This method may be called at any time after Start() has been called to
539 // cancel the request. This method may be called many times, and it has 550 // cancel the request. This method may be called many times, and it has
540 // no effect once the response has completed. It is guaranteed that no 551 // no effect once the response has completed. It is guaranteed that no
541 // methods of the delegate will be called after the request has been 552 // methods of the delegate will be called after the request has been
542 // cancelled, except that this may call the delegate's OnReadCompleted() 553 // cancelled, except that this may call the delegate's OnReadCompleted()
543 // during the call to Cancel itself. 554 // during the call to Cancel itself. Returns |ERR_ABORTED| or other net error
544 void Cancel(); 555 // if there was one.
556 int Cancel();
545 557
546 // Cancels the request and sets the error to |error| (see net_error_list.h 558 // Cancels the request and sets the error to |error| (see net_error_list.h
mmenke 2016/08/30 22:09:12 Let's improve this comment. How about "Cancels th
maksims (do not use this acc) 2016/08/31 11:16:59 Sounds reasonable!
547 // for values). 559 // for values). Returns set error.
mmenke 2016/08/30 22:09:12 "Returns set error." -> "Returns final network err
maksims (do not use this acc) 2016/08/31 11:16:59 Done.
548 void CancelWithError(int error); 560 int CancelWithError(int error);
549 561
550 // Cancels the request and sets the error to |error| (see net_error_list.h 562 // Cancels the request and sets the error to |error| (see net_error_list.h
551 // for values) and attaches |ssl_info| as the SSLInfo for that request. This 563 // for values) and attaches |ssl_info| as the SSLInfo for that request. This
552 // is useful to attach a certificate and certificate error to a canceled 564 // is useful to attach a certificate and certificate error to a canceled
553 // request. 565 // request.
554 void CancelWithSSLError(int error, const SSLInfo& ssl_info); 566 void CancelWithSSLError(int error, const SSLInfo& ssl_info);
555 567
556 // Read initiates an asynchronous read from the response, and must only 568 // Read initiates an asynchronous read from the response, and must only
557 // be called after the OnResponseStarted callback is received with a 569 // be called after the OnResponseStarted callback is received with a net::OK.
558 // successful status. 570 // If data is available, length and the data will be returned immediately.
559 // If data is available, Read will return true, and the data and length will 571 // If data is not available, Read returns net::ERR_IO_PENDING, and an
560 // be returned immediately. If data is not available, Read returns false, 572 // asynchronous Read is initiated. The Read is finished when the caller
561 // and an asynchronous Read is initiated. The Read is finished when 573 // receives the OnReadComplete callback. Unless the request was cancelled,
562 // the caller receives the OnReadComplete callback. Unless the request was 574 // OnReadComplete will always be called on async completion with the result of
563 // cancelled, OnReadComplete will always be called, even if the read failed. 575 // the read.
576 //
577 // If an error occurs during synchronous read, net error is
578 // returned immediately, If an error occurs asynchronously, OnReadComplete
579 // callback will be called with the net error code instead.
mmenke 2016/08/30 22:09:12 Let's merge these two paragraphs. Suggested text:
maksims (do not use this acc) 2016/08/31 11:16:59 Done.
564 // 580 //
565 // The buf parameter is a buffer to receive the data. If the operation 581 // The buf parameter is a buffer to receive the data. If the operation
566 // completes asynchronously, the implementation will reference the buffer 582 // completes asynchronously, the implementation will reference the buffer
567 // until OnReadComplete is called. The buffer must be at least max_bytes in 583 // until OnReadComplete is called. The buffer must be at least max_bytes in
568 // length. 584 // length.
569 // 585 //
570 // The max_bytes parameter is the maximum number of bytes to read. 586 // The max_bytes parameter is the maximum number of bytes to read.
571 // 587 int Read(IOBuffer* buf, int max_bytes);
572 // The bytes_read parameter is an output parameter containing the 588 // Deprecated.
573 // the number of bytes read. A value of 0 indicates that there is no 589 // TODO(maksims): Remove this.
574 // more data available to read from the stream.
575 //
576 // If a read error occurs, Read returns false and the request->status
577 // will be set to an error.
578 bool Read(IOBuffer* buf, int max_bytes, int* bytes_read); 590 bool Read(IOBuffer* buf, int max_bytes, int* bytes_read);
579 591
580 // If this request is being cached by the HTTP cache, stop subsequent caching. 592 // If this request is being cached by the HTTP cache, stop subsequent caching.
581 // Note that this method has no effect on other (simultaneous or not) requests 593 // Note that this method has no effect on other (simultaneous or not) requests
582 // for the same resource. The typical example is a request that results in 594 // for the same resource. The typical example is a request that results in
583 // the data being stored to disk (downloaded instead of rendered) so we don't 595 // the data being stored to disk (downloaded instead of rendered) so we don't
584 // want to store it twice. 596 // want to store it twice.
585 void StopCaching(); 597 void StopCaching();
586 598
587 // This method may be called to follow a redirect that was deferred in 599 // This method may be called to follow a redirect that was deferred in
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 // the more general response_info() is available, even though it is a subset. 653 // the more general response_info() is available, even though it is a subset.
642 const HostPortPair& proxy_server() const { 654 const HostPortPair& proxy_server() const {
643 return proxy_server_; 655 return proxy_server_;
644 } 656 }
645 657
646 // Gets the connection attempts made in the process of servicing this 658 // Gets the connection attempts made in the process of servicing this
647 // URLRequest. Only guaranteed to be valid if called after the request fails 659 // URLRequest. Only guaranteed to be valid if called after the request fails
648 // or after the response headers are received. 660 // or after the response headers are received.
649 void GetConnectionAttempts(ConnectionAttempts* out) const; 661 void GetConnectionAttempts(ConnectionAttempts* out) const;
650 662
663 // Returns the error status of the request.
664 // Do not use! Going to be protected!
665 const URLRequestStatus& status() const { return status_; }
666
651 protected: 667 protected:
652 // Allow the URLRequestJob class to control the is_pending() flag. 668 // Allow the URLRequestJob class to control the is_pending() flag.
653 void set_is_pending(bool value) { is_pending_ = value; } 669 void set_is_pending(bool value) { is_pending_ = value; }
654 670
655 // Allow the URLRequestJob class to set our status too. 671 // Allow the URLRequestJob class to set our status too.
656 void set_status(URLRequestStatus status); 672 void set_status(URLRequestStatus status);
657 673
658 // Allow the URLRequestJob to redirect this request. Returns OK if 674 // Allow the URLRequestJob to redirect this request. Returns OK if
659 // successful, otherwise an error code is returned. 675 // successful, otherwise an error code is returned.
660 int Redirect(const RedirectInfo& redirect_info); 676 int Redirect(const RedirectInfo& redirect_info);
661 677
662 // Called by URLRequestJob to allow interception when a redirect occurs. 678 // Called by URLRequestJob to allow interception when a redirect occurs.
663 void NotifyReceivedRedirect(const RedirectInfo& redirect_info, 679 void NotifyReceivedRedirect(const RedirectInfo& redirect_info,
664 bool* defer_redirect); 680 bool* defer_redirect);
665 681
666 // Allow an interceptor's URLRequestJob to restart this request. 682 // Allow an interceptor's URLRequestJob to restart this request.
667 // Should only be called if the original job has not started a response. 683 // Should only be called if the original job has not started a response.
668 void Restart(); 684 void Restart();
669 685
670 private: 686 private:
671 friend class URLRequestJob; 687 friend class URLRequestJob;
672 friend class URLRequestContext; 688 friend class URLRequestContext;
673 689
690 // For testing purposes.
691 // TODO(maksims): Remove this.
692 friend class TestNetworkDelegate;
693
674 // URLRequests are always created by calling URLRequestContext::CreateRequest. 694 // URLRequests are always created by calling URLRequestContext::CreateRequest.
675 // 695 //
676 // If no network delegate is passed in, will use the ones from the 696 // If no network delegate is passed in, will use the ones from the
677 // URLRequestContext. 697 // URLRequestContext.
678 URLRequest(const GURL& url, 698 URLRequest(const GURL& url,
679 RequestPriority priority, 699 RequestPriority priority,
680 Delegate* delegate, 700 Delegate* delegate,
681 const URLRequestContext* context, 701 const URLRequestContext* context,
682 NetworkDelegate* network_delegate); 702 NetworkDelegate* network_delegate);
683 703
(...skipping 11 matching lines...) Expand all
695 // happens when following a HTTP redirect. 715 // happens when following a HTTP redirect.
696 void RestartWithJob(URLRequestJob* job); 716 void RestartWithJob(URLRequestJob* job);
697 void PrepareToRestart(); 717 void PrepareToRestart();
698 718
699 // Detaches the job from this request in preparation for this object going 719 // Detaches the job from this request in preparation for this object going
700 // away or the job being replaced. The job will not call us back when it has 720 // away or the job being replaced. The job will not call us back when it has
701 // been orphaned. 721 // been orphaned.
702 void OrphanJob(); 722 void OrphanJob();
703 723
704 // Cancels the request and set the error and ssl info for this request to the 724 // Cancels the request and set the error and ssl info for this request to the
705 // passed values. 725 // passed values. Returns the error that was set.
706 void DoCancel(int error, const SSLInfo& ssl_info); 726 int DoCancel(int error, const SSLInfo& ssl_info);
707 727
708 // Called by the URLRequestJob when the headers are received, before any other 728 // Called by the URLRequestJob when the headers are received, before any other
709 // method, to allow caching of load timing information. 729 // method, to allow caching of load timing information.
710 void OnHeadersComplete(); 730 void OnHeadersComplete();
711 731
712 // Notifies the network delegate that the request has been completed. 732 // Notifies the network delegate that the request has been completed.
713 // This does not imply a successful completion. Also a canceled request is 733 // This does not imply a successful completion. Also a canceled request is
714 // considered completed. 734 // considered completed.
715 void NotifyRequestCompleted(); 735 void NotifyRequestCompleted();
716 736
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 867
848 // The proxy server used for this request, if any. 868 // The proxy server used for this request, if any.
849 HostPortPair proxy_server_; 869 HostPortPair proxy_server_;
850 870
851 DISALLOW_COPY_AND_ASSIGN(URLRequest); 871 DISALLOW_COPY_AND_ASSIGN(URLRequest);
852 }; 872 };
853 873
854 } // namespace net 874 } // namespace net
855 875
856 #endif // NET_URL_REQUEST_URL_REQUEST_H_ 876 #endif // NET_URL_REQUEST_URL_REQUEST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698