| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ | 5 #ifndef COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
| 6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ | 6 #define COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 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 "net/base/request_priority.h" | 15 #include "net/base/request_priority.h" |
| 16 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
| 17 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 class GrowableIOBuffer; | 20 class GrowableIOBuffer; |
| 21 class HttpResponseHeaders; | 21 class HttpResponseHeaders; |
| 22 class UploadDataStream; | 22 class UploadDataStream; |
| 23 } // namespace net | 23 } // namespace net |
| 24 | 24 |
| 25 namespace cronet { | 25 namespace cronet { |
| 26 | 26 |
| 27 class URLRequestContextAdapter; | 27 class CronetURLRequestContextAdapter; |
| 28 | 28 |
| 29 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest| | 29 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest| |
| 30 // object. | 30 // object. |
| 31 class URLRequestAdapter : public net::URLRequest::Delegate { | 31 class CronetURLRequestAdapter : public net::URLRequest::Delegate { |
| 32 public: | 32 public: |
| 33 // The delegate which is called when the request finishes. | 33 // The delegate which is called when the request finishes. |
| 34 class URLRequestAdapterDelegate | 34 class CronetURLRequestAdapterDelegate |
| 35 : public base::RefCountedThreadSafe<URLRequestAdapterDelegate> { | 35 : public base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate> { |
| 36 public: | 36 public: |
| 37 virtual void OnResponseStarted(URLRequestAdapter* request) = 0; | 37 virtual void OnRedirect(CronetURLRequestAdapter* request, |
| 38 virtual void OnBytesRead(URLRequestAdapter* request) = 0; | 38 const GURL& newLocation) = 0; |
| 39 virtual void OnRequestFinished(URLRequestAdapter* request) = 0; | 39 virtual void OnResponseStarted(CronetURLRequestAdapter* request) = 0; |
| 40 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0; | 40 virtual void OnBytesRead(CronetURLRequestAdapter* request, |
| 41 int bytes_read) = 0; |
| 42 virtual void OnRequestFinished(CronetURLRequestAdapter* request) = 0; |
| 41 | 43 |
| 42 protected: | 44 protected: |
| 43 friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>; | 45 friend class base::RefCountedThreadSafe<CronetURLRequestAdapterDelegate>; |
| 44 virtual ~URLRequestAdapterDelegate() {} | 46 virtual ~CronetURLRequestAdapterDelegate() {} |
| 45 }; | 47 }; |
| 46 | 48 |
| 47 URLRequestAdapter(URLRequestContextAdapter* context, | 49 CronetURLRequestAdapter(CronetURLRequestContextAdapter* context, |
| 48 URLRequestAdapterDelegate* delegate, | 50 CronetURLRequestAdapterDelegate* delegate, |
| 49 GURL url, | 51 GURL url, |
| 50 net::RequestPriority priority); | 52 net::RequestPriority priority); |
| 51 virtual ~URLRequestAdapter(); | 53 virtual ~CronetURLRequestAdapter(); |
| 52 | 54 |
| 53 // Sets the request method GET, POST etc | 55 // Sets the request method GET, POST etc |
| 54 void SetMethod(const std::string& method); | 56 void SetMethod(const std::string& method); |
| 55 | 57 |
| 56 // Adds a header to the request | 58 // Adds a header to the request |
| 57 void AddHeader(const std::string& name, const std::string& value); | 59 void AddHeader(const std::string& name, const std::string& value); |
| 58 | 60 |
| 59 // Sets the contents of the POST or PUT request | |
| 60 void SetUploadContent(const char* bytes, int bytes_len); | |
| 61 | |
| 62 // Sets the request to streaming upload. | |
| 63 void SetUploadChannel(JNIEnv* env, int64 content_length); | |
| 64 | |
| 65 // Indicates that the request body will be streamed by calling AppendChunk() | |
| 66 // repeatedly. This must be called before Start(). | |
| 67 void EnableChunkedUpload(); | |
| 68 | |
| 69 // Appends a chunk to the POST body. | |
| 70 // This must be called after EnableChunkedUpload() and Start(). | |
| 71 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | |
| 72 | |
| 73 // Starts the request. | 61 // Starts the request. |
| 74 void Start(); | 62 void Start(); |
| 75 | 63 |
| 64 // Follow redirect. |
| 65 void FollowDeferredRedirect(); |
| 66 |
| 67 // Read more data. |
| 68 void ReadData(); |
| 69 |
| 76 // Cancels the request. | 70 // Cancels the request. |
| 77 void Cancel(); | 71 void Cancel(); |
| 78 | 72 |
| 79 // Releases all resources for the request and deletes the object itself. | 73 // Releases all resources for the request and deletes the object itself. |
| 80 void Destroy(); | 74 void Destroy(); |
| 81 | 75 |
| 82 // Returns the URL of the request. | 76 // Returns the URL of the request. |
| 83 GURL url() const { return url_; } | 77 GURL url() const { return url_; } |
| 84 | 78 |
| 85 // Returns the error code after the request is complete. | 79 // Returns the error code after the request is complete. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 105 | 99 |
| 106 // Returns the overall number of bytes read. | 100 // Returns the overall number of bytes read. |
| 107 size_t bytes_read() const { return bytes_read_; } | 101 size_t bytes_read() const { return bytes_read_; } |
| 108 | 102 |
| 109 // Returns a pointer to the downloaded data. | 103 // Returns a pointer to the downloaded data. |
| 110 unsigned char* Data() const; | 104 unsigned char* Data() const; |
| 111 | 105 |
| 112 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. | 106 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. |
| 113 std::string GetNegotiatedProtocol() const; | 107 std::string GetNegotiatedProtocol() const; |
| 114 | 108 |
| 109 // net::URLRequest::Delegate overrides |
| 110 virtual void OnReceivedRedirect(net::URLRequest* request, |
| 111 const net::RedirectInfo& redirect_info, |
| 112 bool* defer_redirect) OVERRIDE; |
| 113 |
| 115 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | 114 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; |
| 116 | 115 |
| 117 virtual void OnReadCompleted(net::URLRequest* request, | 116 virtual void OnReadCompleted(net::URLRequest* request, |
| 118 int bytes_read) OVERRIDE; | 117 int bytes_read) OVERRIDE; |
| 119 | 118 |
| 120 private: | 119 private: |
| 121 static void OnDestroyRequest(URLRequestAdapter* self); | 120 static void DestroyOnNetworkThread(CronetURLRequestAdapter* self); |
| 121 void StartOnNetworkThread(); |
| 122 void FollowDeferredRedirectOnNetworkThread(); |
| 123 void ReadDataOnNetworkThread(); |
| 124 void CancelOnNetworkThread(); |
| 122 | 125 |
| 123 void OnInitiateConnection(); | 126 CronetURLRequestContextAdapter* context_; |
| 124 void OnCancelRequest(); | 127 scoped_refptr<CronetURLRequestAdapterDelegate> delegate_; |
| 125 void OnRequestSucceeded(); | |
| 126 void OnRequestFailed(); | |
| 127 void OnRequestCompleted(); | |
| 128 void OnRequestCanceled(); | |
| 129 void OnBytesRead(int bytes_read); | |
| 130 void OnAppendChunk(const scoped_ptr<char[]> bytes, int bytes_len, | |
| 131 bool is_last_chunk); | |
| 132 | |
| 133 void Read(); | |
| 134 | |
| 135 URLRequestContextAdapter* context_; | |
| 136 scoped_refptr<URLRequestAdapterDelegate> delegate_; | |
| 137 GURL url_; | 128 GURL url_; |
| 138 net::RequestPriority priority_; | 129 net::RequestPriority priority_; |
| 139 std::string method_; | 130 std::string method_; |
| 140 net::HttpRequestHeaders headers_; | 131 net::HttpRequestHeaders headers_; |
| 141 scoped_ptr<net::URLRequest> url_request_; | 132 scoped_ptr<net::URLRequest> url_request_; |
| 142 scoped_ptr<net::UploadDataStream> upload_data_stream_; | 133 scoped_refptr<net::IOBufferWithSize> read_buffer_; |
| 143 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | |
| 144 int bytes_read_; | 134 int bytes_read_; |
| 145 int total_bytes_read_; | 135 int total_bytes_read_; |
| 146 int error_code_; | 136 int error_code_; |
| 147 int http_status_code_; | 137 int http_status_code_; |
| 148 std::string content_type_; | 138 std::string content_type_; |
| 149 bool canceled_; | 139 bool canceled_; |
| 150 int64 expected_size_; | 140 int64 expected_size_; |
| 151 bool chunked_upload_; | 141 bool chunked_upload_; |
| 152 | 142 |
| 153 DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter); | 143 DISALLOW_COPY_AND_ASSIGN(CronetURLRequestAdapter); |
| 154 }; | 144 }; |
| 155 | 145 |
| 156 } // namespace cronet | 146 } // namespace cronet |
| 157 | 147 |
| 158 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ | 148 #endif // COMPONENTS_CRONET_ANDROID_CRONET_URL_REQUEST_ADAPTER_H_ |
| OLD | NEW |