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_URL_REQUEST_ADAPTER_H_ |
6 #define COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ | 6 #define COMPONENTS_CRONET_ANDROID_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 IOBufferWithSize; |
21 class HttpResponseHeaders; | 21 class HttpResponseHeaders; |
22 class UploadDataStream; | 22 class UploadDataStream; |
23 struct RedirectInfo; | 23 struct RedirectInfo; |
24 } // namespace net | 24 } // namespace net |
25 | 25 |
26 namespace cronet { | 26 namespace cronet { |
27 | 27 |
28 class URLRequestContextAdapter; | 28 class URLRequestContextAdapter; |
29 | 29 |
30 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest| | 30 // An adapter from the JNI |UrlRequest| object and the Chromium |URLRequest| |
31 // object. | 31 // object. |
32 class URLRequestAdapter : public net::URLRequest::Delegate { | 32 class URLRequestAdapter : public net::URLRequest::Delegate { |
33 public: | 33 public: |
34 // The delegate which is called when the request finishes. | 34 // The delegate which is called when the request finishes. |
35 class URLRequestAdapterDelegate | 35 class URLRequestAdapterDelegate |
36 : public base::RefCountedThreadSafe<URLRequestAdapterDelegate> { | 36 : public base::RefCountedThreadSafe<URLRequestAdapterDelegate> { |
37 public: | 37 public: |
38 virtual void OnResponseStarted(URLRequestAdapter* request) = 0; | 38 virtual void OnResponseStarted(URLRequestAdapter* request) = 0; |
39 virtual void OnBytesRead(URLRequestAdapter* request) = 0; | 39 virtual void OnBytesRead(URLRequestAdapter* request, int bytes_read) = 0; |
40 virtual void OnRequestFinished(URLRequestAdapter* request) = 0; | 40 virtual void OnRequestFinished(URLRequestAdapter* request) = 0; |
41 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0; | 41 virtual int ReadFromUploadChannel(net::IOBuffer* buf, int buf_length) = 0; |
42 | 42 |
43 protected: | 43 protected: |
44 friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>; | 44 friend class base::RefCountedThreadSafe<URLRequestAdapterDelegate>; |
45 virtual ~URLRequestAdapterDelegate() {} | 45 virtual ~URLRequestAdapterDelegate() {} |
46 }; | 46 }; |
47 | 47 |
48 URLRequestAdapter(URLRequestContextAdapter* context, | 48 URLRequestAdapter(URLRequestContextAdapter* context, |
49 URLRequestAdapterDelegate* delegate, | 49 URLRequestAdapterDelegate* delegate, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 | 105 |
106 // Returns the value of the content-type response header. | 106 // Returns the value of the content-type response header. |
107 std::string content_type() const { return content_type_; } | 107 std::string content_type() const { return content_type_; } |
108 | 108 |
109 // Returns the value of the specified response header. | 109 // Returns the value of the specified response header. |
110 std::string GetHeader(const std::string& name) const; | 110 std::string GetHeader(const std::string& name) const; |
111 | 111 |
112 // Get all response headers, as a HttpResponseHeaders object. | 112 // Get all response headers, as a HttpResponseHeaders object. |
113 net::HttpResponseHeaders* GetResponseHeaders() const; | 113 net::HttpResponseHeaders* GetResponseHeaders() const; |
114 | 114 |
115 // Returns the overall number of bytes read. | |
116 size_t bytes_read() const { return bytes_read_; } | |
117 | |
118 // Returns a pointer to the downloaded data. | 115 // Returns a pointer to the downloaded data. |
119 unsigned char* Data() const; | 116 unsigned char* Data() const; |
120 | 117 |
121 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. | 118 // Get NPN or ALPN Negotiated Protocol (if any) from HttpResponseInfo. |
122 std::string GetNegotiatedProtocol() const; | 119 std::string GetNegotiatedProtocol() const; |
123 | 120 |
124 // net::URLRequest::Delegate implementation: | 121 // net::URLRequest::Delegate implementation: |
125 void OnResponseStarted(net::URLRequest* request) override; | 122 void OnResponseStarted(net::URLRequest* request) override; |
126 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | 123 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
127 void OnReceivedRedirect(net::URLRequest* request, | 124 void OnReceivedRedirect(net::URLRequest* request, |
128 const net::RedirectInfo& redirect_info, | 125 const net::RedirectInfo& redirect_info, |
129 bool* defer_redirect) override; | 126 bool* defer_redirect) override; |
130 | 127 |
131 bool OnNetworkThread() const; | 128 bool OnNetworkThread() const; |
132 | 129 |
133 private: | 130 private: |
134 static void OnDestroyRequest(URLRequestAdapter* self); | 131 static void OnDestroyRequest(URLRequestAdapter* self); |
135 | 132 |
136 void OnInitiateConnection(); | 133 void OnInitiateConnection(); |
137 void OnCancelRequest(); | 134 void OnCancelRequest(); |
138 void OnRequestSucceeded(); | 135 void OnRequestSucceeded(); |
139 void OnRequestFailed(); | 136 void OnRequestFailed(); |
140 void OnRequestCompleted(); | 137 void OnRequestCompleted(); |
141 void OnRequestCanceled(); | |
142 void OnBytesRead(int bytes_read); | |
143 void OnAppendChunk(const scoped_ptr<char[]> bytes, int bytes_len, | 138 void OnAppendChunk(const scoped_ptr<char[]> bytes, int bytes_len, |
144 bool is_last_chunk); | 139 bool is_last_chunk); |
145 | 140 |
146 void Read(); | 141 void Read(); |
142 bool HandleReadResult(net::URLRequest* request, int bytes_read); | |
mmenke
2014/12/10 20:59:14
Document this method.
mmenke
2014/12/10 20:59:14
No need to pass in the request, since it's always
mef
2014/12/10 23:23:36
Done.
| |
147 | 143 |
148 URLRequestContextAdapter* context_; | 144 URLRequestContextAdapter* context_; |
149 scoped_refptr<URLRequestAdapterDelegate> delegate_; | 145 scoped_refptr<URLRequestAdapterDelegate> delegate_; |
150 GURL url_; | 146 GURL url_; |
151 net::RequestPriority priority_; | 147 net::RequestPriority priority_; |
152 std::string method_; | 148 std::string method_; |
153 net::HttpRequestHeaders headers_; | 149 net::HttpRequestHeaders headers_; |
154 scoped_ptr<net::URLRequest> url_request_; | 150 scoped_ptr<net::URLRequest> url_request_; |
155 scoped_ptr<net::UploadDataStream> upload_data_stream_; | 151 scoped_ptr<net::UploadDataStream> upload_data_stream_; |
156 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | 152 scoped_refptr<net::IOBufferWithSize> read_buffer_; |
157 int bytes_read_; | |
158 int total_bytes_read_; | 153 int total_bytes_read_; |
159 int error_code_; | 154 int error_code_; |
160 int http_status_code_; | 155 int http_status_code_; |
161 std::string http_status_text_; | 156 std::string http_status_text_; |
162 std::string content_type_; | 157 std::string content_type_; |
163 bool canceled_; | 158 bool canceled_; |
164 int64 expected_size_; | 159 int64 expected_size_; |
165 bool chunked_upload_; | 160 bool chunked_upload_; |
166 // Indicates whether redirect has been disabled. | 161 // Indicates whether redirect has been disabled. |
167 bool disable_redirect_; | 162 bool disable_redirect_; |
168 | 163 |
169 DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter); | 164 DISALLOW_COPY_AND_ASSIGN(URLRequestAdapter); |
170 }; | 165 }; |
171 | 166 |
172 } // namespace cronet | 167 } // namespace cronet |
173 | 168 |
174 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ | 169 #endif // COMPONENTS_CRONET_ANDROID_URL_REQUEST_ADAPTER_H_ |
OLD | NEW |