OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
| 6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
| 7 |
| 8 #include <jni.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "net/base/request_priority.h" |
| 15 #include "net/http/http_request_headers.h" |
| 16 #include "net/url_request/url_request.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace base { |
| 20 class SingleThreadTaskRunner; |
| 21 } // namespace base |
| 22 |
| 23 namespace net { |
| 24 class GrowableIOBuffer; |
| 25 class HttpResponseHeaders; |
| 26 class UploadDataStream; |
| 27 } // namespace net |
| 28 |
| 29 namespace cronet { |
| 30 |
| 31 class CronetURLRequestContextAdapter; |
| 32 |
| 33 // An adapter from the JNI CronetUrlRequest object to the Chromium URLRequest. |
| 34 class CronetURLRequestAdapter : public net::URLRequest::Delegate { |
| 35 public: |
| 36 // The delegate which is called when the request_adapter finishes. |
| 37 class CronetURLRequestAdapterDelegate { |
| 38 public: |
| 39 virtual void OnRedirect(const GURL& newLocation, int http_status_code) = 0; |
| 40 virtual void OnResponseStarted(int http_status_code) = 0; |
| 41 virtual void OnBytesRead(unsigned char* bytes_buffer, |
| 42 int bytes_read) = 0; |
| 43 virtual void OnRequestFinished() = 0; |
| 44 virtual void OnError(int net_error) = 0; |
| 45 |
| 46 virtual ~CronetURLRequestAdapterDelegate() {} |
| 47 }; |
| 48 |
| 49 CronetURLRequestAdapter( |
| 50 CronetURLRequestContextAdapter* context, |
| 51 scoped_ptr<CronetURLRequestAdapterDelegate> delegate, |
| 52 const GURL& url, |
| 53 net::RequestPriority priority); |
| 54 ~CronetURLRequestAdapter() override; |
| 55 |
| 56 // Sets the request method GET, POST etc. |
| 57 void set_method(const std::string& method) { |
| 58 initial_method_ = method; |
| 59 } |
| 60 |
| 61 // Adds a header to the request before it starts. |
| 62 void AddRequestHeader(const std::string& name, const std::string& value); |
| 63 |
| 64 // Starts the request. |
| 65 void Start(); |
| 66 |
| 67 // Follows redirect. |
| 68 void FollowDeferredRedirect(); |
| 69 |
| 70 // Reads more data. |
| 71 void ReadData(); |
| 72 |
| 73 // Releases all resources for the request and deletes the object |
| 74 // itself. |
| 75 void Destroy(); |
| 76 |
| 77 // Returns the URL of the request. |
| 78 const GURL& url() const { return initial_url_; } |
| 79 |
| 80 // Gets all response headers, as a HttpResponseHeaders object. |
| 81 net::HttpResponseHeaders* GetResponseHeaders() const; |
| 82 |
| 83 // Returns a pointer to the downloaded data. |
| 84 unsigned char* Data() const; |
| 85 |
| 86 // Gets NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. |
| 87 const std::string& GetNegotiatedProtocol() const; |
| 88 |
| 89 // Returns true if response is coming from the cache. |
| 90 bool GetWasCached() const; |
| 91 |
| 92 // Gets the total amount of data received from network after SSL decoding and |
| 93 // proxy handling. |
| 94 int64 GetTotalReceivedBytes() const; |
| 95 |
| 96 // net::URLRequest::Delegate overrides |
| 97 void OnReceivedRedirect(net::URLRequest* request, |
| 98 const net::RedirectInfo& redirect_info, |
| 99 bool* defer_redirect) override; |
| 100 void OnResponseStarted(net::URLRequest* request) override; |
| 101 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
| 102 |
| 103 // Returns true if currently running on network thread. |
| 104 bool IsOnNetworkThread() const; |
| 105 |
| 106 private: |
| 107 void StartOnNetworkThread(); |
| 108 void FollowDeferredRedirectOnNetworkThread(); |
| 109 void ReadDataOnNetworkThread(); |
| 110 void DestroyOnNetworkThread(); |
| 111 |
| 112 // Checks status of the request_adapter, return true if |is_success()| is |
| 113 // true, otherwise report error and cancel request_adapter. |
| 114 bool MaybeReportError(net::URLRequest* request) const; |
| 115 |
| 116 CronetURLRequestContextAdapter* context_; |
| 117 scoped_ptr<CronetURLRequestAdapterDelegate> delegate_; |
| 118 const GURL initial_url_; |
| 119 const net::RequestPriority initial_priority_; |
| 120 std::string initial_method_; |
| 121 net::HttpRequestHeaders initial_request_headers_; |
| 122 scoped_refptr<net::IOBufferWithSize> read_buffer_; |
| 123 scoped_ptr<net::URLRequest> url_request_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); |
| 126 }; |
| 127 |
| 128 } // namespace cronet |
| 129 |
| 130 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
OLD | NEW |