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 #include "components/cronet/android/chromium_url_request.h" | 5 #include "components/cronet/android/chromium_url_request.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "components/cronet/android/url_request_adapter.h" | 10 #include "components/cronet/android/url_request_adapter.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 case REQUEST_PRIORITY_HIGHEST: | 33 case REQUEST_PRIORITY_HIGHEST: |
34 return net::HIGHEST; | 34 return net::HIGHEST; |
35 default: | 35 default: |
36 return net::LOWEST; | 36 return net::LOWEST; |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
40 void SetPostContentType(JNIEnv* env, | 40 void SetPostContentType(JNIEnv* env, |
41 URLRequestAdapter* request_adapter, | 41 URLRequestAdapter* request_adapter, |
42 jstring content_type) { | 42 jstring content_type) { |
43 DCHECK(request_adapter); | |
44 | |
45 std::string method_post("POST"); | 43 std::string method_post("POST"); |
46 request_adapter->SetMethod(method_post); | 44 request_adapter->SetMethod(method_post); |
47 | 45 |
48 std::string content_type_header("Content-Type"); | 46 std::string content_type_header("Content-Type"); |
49 std::string content_type_string(ConvertJavaStringToUTF8(env, content_type)); | 47 std::string content_type_string(ConvertJavaStringToUTF8(env, content_type)); |
50 | 48 |
51 request_adapter->AddHeader(content_type_header, content_type_string); | 49 request_adapter->AddHeader(content_type_header, content_type_string); |
52 } | 50 } |
53 | 51 |
54 // A delegate of URLRequestAdapter that delivers callbacks to the Java layer. | 52 // A delegate of URLRequestAdapter that delivers callbacks to the Java layer. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 request_adapter->SetMethod(method); | 155 request_adapter->SetMethod(method); |
158 } | 156 } |
159 | 157 |
160 static void SetUploadData(JNIEnv* env, | 158 static void SetUploadData(JNIEnv* env, |
161 jobject jcaller, | 159 jobject jcaller, |
162 jlong jurl_request_adapter, | 160 jlong jurl_request_adapter, |
163 jstring jcontent_type, | 161 jstring jcontent_type, |
164 jbyteArray jcontent) { | 162 jbyteArray jcontent) { |
165 URLRequestAdapter* request_adapter = | 163 URLRequestAdapter* request_adapter = |
166 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 164 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 165 DCHECK(request_adapter); |
167 SetPostContentType(env, request_adapter, jcontent_type); | 166 SetPostContentType(env, request_adapter, jcontent_type); |
168 | 167 |
169 if (jcontent != NULL) { | 168 if (jcontent != NULL) { |
170 jsize size = env->GetArrayLength(jcontent); | 169 jsize size = env->GetArrayLength(jcontent); |
171 if (size > 0) { | 170 if (size > 0) { |
172 jbyte* content_bytes = env->GetByteArrayElements(jcontent, NULL); | 171 jbyte* content_bytes = env->GetByteArrayElements(jcontent, NULL); |
173 request_adapter->SetUploadContent( | 172 request_adapter->SetUploadContent( |
174 reinterpret_cast<const char*>(content_bytes), size); | 173 reinterpret_cast<const char*>(content_bytes), size); |
175 env->ReleaseByteArrayElements(jcontent, content_bytes, 0); | 174 env->ReleaseByteArrayElements(jcontent, content_bytes, 0); |
176 } | 175 } |
177 } | 176 } |
178 } | 177 } |
179 | 178 |
180 static void SetUploadChannel(JNIEnv* env, | 179 static void SetUploadChannel(JNIEnv* env, |
181 jobject jcaller, | 180 jobject jcaller, |
182 jlong jurl_request_adapter, | 181 jlong jurl_request_adapter, |
183 jstring jcontent_type, | 182 jstring jcontent_type, |
184 jlong jcontent_length) { | 183 jlong jcontent_length) { |
185 URLRequestAdapter* request_adapter = | 184 URLRequestAdapter* request_adapter = |
186 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 185 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 186 DCHECK(request_adapter); |
187 SetPostContentType(env, request_adapter, jcontent_type); | 187 SetPostContentType(env, request_adapter, jcontent_type); |
188 | 188 |
189 request_adapter->SetUploadChannel(env, jcontent_length); | 189 request_adapter->SetUploadChannel(env, jcontent_length); |
190 } | 190 } |
191 | 191 |
192 static void EnableChunkedUpload(JNIEnv* env, | 192 static void EnableChunkedUpload(JNIEnv* env, |
193 jobject jcaller, | 193 jobject jcaller, |
194 jlong jurl_request_adapter, | 194 jlong jurl_request_adapter, |
195 jstring jcontent_type) { | 195 jstring jcontent_type) { |
196 URLRequestAdapter* request_adapter = | 196 URLRequestAdapter* request_adapter = |
197 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 197 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 198 DCHECK(request_adapter); |
198 SetPostContentType(env, request_adapter, jcontent_type); | 199 SetPostContentType(env, request_adapter, jcontent_type); |
199 | 200 |
200 request_adapter->EnableChunkedUpload(); | 201 request_adapter->EnableChunkedUpload(); |
201 } | 202 } |
202 | 203 |
203 static void AppendChunk(JNIEnv* env, | 204 static void AppendChunk(JNIEnv* env, |
204 jobject jcaller, | 205 jobject jcaller, |
205 jlong jurl_request_adapter, | 206 jlong jurl_request_adapter, |
206 jobject jchunk_byte_buffer, | 207 jobject jchunk_byte_buffer, |
207 jint jchunk_size, | 208 jint jchunk_size, |
208 jboolean jis_last_chunk) { | 209 jboolean jis_last_chunk) { |
209 URLRequestAdapter* request_adapter = | 210 URLRequestAdapter* request_adapter = |
210 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 211 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 212 DCHECK(request_adapter); |
211 DCHECK(jchunk_byte_buffer); | 213 DCHECK(jchunk_byte_buffer); |
212 | 214 |
213 void* chunk = env->GetDirectBufferAddress(jchunk_byte_buffer); | 215 void* chunk = env->GetDirectBufferAddress(jchunk_byte_buffer); |
214 request_adapter->AppendChunk(reinterpret_cast<const char*>(chunk), | 216 request_adapter->AppendChunk(reinterpret_cast<const char*>(chunk), |
215 jchunk_size, jis_last_chunk); | 217 jchunk_size, jis_last_chunk); |
216 } | 218 } |
217 | 219 |
218 /* synchronized */ | 220 /* synchronized */ |
219 static void Start(JNIEnv* env, jobject jcaller, jlong jurl_request_adapter) { | 221 static void Start(JNIEnv* env, jobject jcaller, jlong jurl_request_adapter) { |
220 URLRequestAdapter* request_adapter = | 222 URLRequestAdapter* request_adapter = |
(...skipping 18 matching lines...) Expand all Loading... |
239 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 241 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
240 if (request_adapter != NULL) | 242 if (request_adapter != NULL) |
241 request_adapter->Cancel(); | 243 request_adapter->Cancel(); |
242 } | 244 } |
243 | 245 |
244 static jint GetErrorCode(JNIEnv* env, | 246 static jint GetErrorCode(JNIEnv* env, |
245 jobject jcaller, | 247 jobject jcaller, |
246 jlong jurl_request_adapter) { | 248 jlong jurl_request_adapter) { |
247 URLRequestAdapter* request_adapter = | 249 URLRequestAdapter* request_adapter = |
248 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 250 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 251 DCHECK(request_adapter); |
249 int error_code = request_adapter->error_code(); | 252 int error_code = request_adapter->error_code(); |
250 switch (error_code) { | 253 switch (error_code) { |
251 // TODO(mef): Investigate returning success on positive values, too, as | 254 // TODO(mef): Investigate returning success on positive values, too, as |
252 // they technically indicate success. | 255 // they technically indicate success. |
253 case net::OK: | 256 case net::OK: |
254 return REQUEST_ERROR_SUCCESS; | 257 return REQUEST_ERROR_SUCCESS; |
255 | 258 |
256 // TODO(mef): Investigate this. The fact is that Chrome does not do this, | 259 // TODO(mef): Investigate this. The fact is that Chrome does not do this, |
257 // and this library is not just being used for downloads. | 260 // and this library is not just being used for downloads. |
258 | 261 |
(...skipping 20 matching lines...) Expand all Loading... |
279 return REQUEST_ERROR_TOO_MANY_REDIRECTS; | 282 return REQUEST_ERROR_TOO_MANY_REDIRECTS; |
280 } | 283 } |
281 return REQUEST_ERROR_UNKNOWN; | 284 return REQUEST_ERROR_UNKNOWN; |
282 } | 285 } |
283 | 286 |
284 static jstring GetErrorString(JNIEnv* env, | 287 static jstring GetErrorString(JNIEnv* env, |
285 jobject jcaller, | 288 jobject jcaller, |
286 jlong jurl_request_adapter) { | 289 jlong jurl_request_adapter) { |
287 URLRequestAdapter* request_adapter = | 290 URLRequestAdapter* request_adapter = |
288 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 291 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 292 DCHECK(request_adapter); |
289 int error_code = request_adapter->error_code(); | 293 int error_code = request_adapter->error_code(); |
290 char buffer[200]; | 294 char buffer[200]; |
291 std::string error_string = net::ErrorToString(error_code); | 295 std::string error_string = net::ErrorToString(error_code); |
292 snprintf(buffer, | 296 snprintf(buffer, |
293 sizeof(buffer), | 297 sizeof(buffer), |
294 "System error: %s(%d)", | 298 "System error: %s(%d)", |
295 error_string.c_str(), | 299 error_string.c_str(), |
296 error_code); | 300 error_code); |
297 return ConvertUTF8ToJavaString(env, buffer).Release(); | 301 return ConvertUTF8ToJavaString(env, buffer).Release(); |
298 } | 302 } |
299 | 303 |
300 static jint GetHttpStatusCode(JNIEnv* env, | 304 static jint GetHttpStatusCode(JNIEnv* env, |
301 jobject jcaller, | 305 jobject jcaller, |
302 jlong jurl_request_adapter) { | 306 jlong jurl_request_adapter) { |
303 URLRequestAdapter* request_adapter = | 307 URLRequestAdapter* request_adapter = |
304 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 308 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 309 DCHECK(request_adapter); |
305 return request_adapter->http_status_code(); | 310 return request_adapter->http_status_code(); |
306 } | 311 } |
307 | 312 |
308 static jstring GetHttpStatusText(JNIEnv* env, | 313 static jstring GetHttpStatusText(JNIEnv* env, |
309 jobject jcaller, | 314 jobject jcaller, |
310 jlong jurl_request_adapter) { | 315 jlong jurl_request_adapter) { |
311 URLRequestAdapter* request_adapter = | 316 URLRequestAdapter* request_adapter = |
312 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 317 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
| 318 DCHECK(request_adapter); |
313 return ConvertUTF8ToJavaString(env, request_adapter->http_status_text()) | 319 return ConvertUTF8ToJavaString(env, request_adapter->http_status_text()) |
314 .Release(); | 320 .Release(); |
315 } | 321 } |
316 | 322 |
317 static jstring GetContentType(JNIEnv* env, | 323 static jstring GetContentType(JNIEnv* env, |
318 jobject jcaller, | 324 jobject jcaller, |
319 jlong jurl_request_adapter) { | 325 jlong jurl_request_adapter) { |
320 URLRequestAdapter* request_adapter = | 326 URLRequestAdapter* request_adapter = |
321 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 327 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
322 if (request_adapter == NULL) | 328 DCHECK(request_adapter); |
323 return NULL; | |
324 std::string type = request_adapter->content_type(); | 329 std::string type = request_adapter->content_type(); |
325 if (!type.empty()) { | 330 if (!type.empty()) { |
326 return ConvertUTF8ToJavaString(env, type.c_str()).Release(); | 331 return ConvertUTF8ToJavaString(env, type.c_str()).Release(); |
327 } else { | 332 } else { |
328 return NULL; | 333 return NULL; |
329 } | 334 } |
330 } | 335 } |
331 | 336 |
332 static jlong GetContentLength(JNIEnv* env, | 337 static jlong GetContentLength(JNIEnv* env, |
333 jobject jcaller, | 338 jobject jcaller, |
334 jlong jurl_request_adapter) { | 339 jlong jurl_request_adapter) { |
335 URLRequestAdapter* request_adapter = | 340 URLRequestAdapter* request_adapter = |
336 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 341 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
337 if (request_adapter == NULL) | 342 DCHECK(request_adapter); |
338 return 0; | |
339 return request_adapter->content_length(); | 343 return request_adapter->content_length(); |
340 } | 344 } |
341 | 345 |
342 static jstring GetHeader(JNIEnv* env, | 346 static jstring GetHeader(JNIEnv* env, |
343 jobject jcaller, | 347 jobject jcaller, |
344 jlong jurl_request_adapter, | 348 jlong jurl_request_adapter, |
345 jstring jheader_name) { | 349 jstring jheader_name) { |
346 URLRequestAdapter* request_adapter = | 350 URLRequestAdapter* request_adapter = |
347 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 351 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
348 if (request_adapter == NULL) | 352 DCHECK(request_adapter); |
349 return NULL; | |
350 std::string header_name = ConvertJavaStringToUTF8(env, jheader_name); | 353 std::string header_name = ConvertJavaStringToUTF8(env, jheader_name); |
351 std::string header_value = request_adapter->GetHeader(header_name); | 354 std::string header_value = request_adapter->GetHeader(header_name); |
352 if (!header_value.empty()) { | 355 if (!header_value.empty()) |
353 return ConvertUTF8ToJavaString(env, header_value.c_str()).Release(); | 356 return ConvertUTF8ToJavaString(env, header_value.c_str()).Release(); |
354 } else { | 357 return NULL; |
355 return NULL; | |
356 } | |
357 } | 358 } |
358 | 359 |
359 static void GetAllHeaders(JNIEnv* env, | 360 static void GetAllHeaders(JNIEnv* env, |
360 jobject jcaller, | 361 jobject jcaller, |
361 jlong jurl_request_adapter, | 362 jlong jurl_request_adapter, |
362 jobject jheaders_map) { | 363 jobject jheaders_map) { |
363 URLRequestAdapter* request_adapter = | 364 URLRequestAdapter* request_adapter = |
364 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 365 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
365 if (request_adapter == NULL) | 366 DCHECK(request_adapter); |
366 return; | |
367 | 367 |
368 net::HttpResponseHeaders* headers = request_adapter->GetResponseHeaders(); | 368 net::HttpResponseHeaders* headers = request_adapter->GetResponseHeaders(); |
369 if (headers == NULL) | 369 if (headers == NULL) |
370 return; | 370 return; |
371 | 371 |
372 void* iter = NULL; | 372 void* iter = NULL; |
373 std::string header_name; | 373 std::string header_name; |
374 std::string header_value; | 374 std::string header_value; |
375 while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) { | 375 while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) { |
376 ScopedJavaLocalRef<jstring> name = | 376 ScopedJavaLocalRef<jstring> name = |
(...skipping 10 matching lines...) Expand all Loading... |
387 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); | 387 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); |
388 Java_ChromiumUrlRequest_onAppendResponseHeader(env, jcaller, jheaders_map, | 388 Java_ChromiumUrlRequest_onAppendResponseHeader(env, jcaller, jheaders_map, |
389 NULL, status_line.obj()); | 389 NULL, status_line.obj()); |
390 } | 390 } |
391 | 391 |
392 static jstring GetNegotiatedProtocol(JNIEnv* env, | 392 static jstring GetNegotiatedProtocol(JNIEnv* env, |
393 jobject jcaller, | 393 jobject jcaller, |
394 jlong jurl_request_adapter) { | 394 jlong jurl_request_adapter) { |
395 URLRequestAdapter* request_adapter = | 395 URLRequestAdapter* request_adapter = |
396 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 396 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
397 if (request_adapter == NULL) | 397 DCHECK(request_adapter); |
398 return ConvertUTF8ToJavaString(env, "").Release(); | |
399 | 398 |
400 std::string negotiated_protocol = request_adapter->GetNegotiatedProtocol(); | 399 std::string negotiated_protocol = request_adapter->GetNegotiatedProtocol(); |
401 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()).Release(); | 400 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()).Release(); |
402 } | 401 } |
403 | 402 |
404 static void DisableRedirects(JNIEnv* env, jobject jcaller, | 403 static void DisableRedirects(JNIEnv* env, jobject jcaller, |
405 jlong jrequest_adapter) { | 404 jlong jrequest_adapter) { |
406 URLRequestAdapter* request_adapter = | 405 URLRequestAdapter* request_adapter = |
407 reinterpret_cast<URLRequestAdapter*>(jrequest_adapter); | 406 reinterpret_cast<URLRequestAdapter*>(jrequest_adapter); |
408 if (request_adapter != NULL) | 407 DCHECK(request_adapter); |
409 request_adapter->DisableRedirects(); | 408 request_adapter->DisableRedirects(); |
410 } | 409 } |
411 | 410 |
412 } // namespace cronet | 411 } // namespace cronet |
OLD | NEW |