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 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 return REQUEST_ERROR_CONNECTION_TIMED_OUT; | 277 return REQUEST_ERROR_CONNECTION_TIMED_OUT; |
278 | 278 |
279 case net::ERR_NAME_NOT_RESOLVED: | 279 case net::ERR_NAME_NOT_RESOLVED: |
280 return REQUEST_ERROR_UNKNOWN_HOST; | 280 return REQUEST_ERROR_UNKNOWN_HOST; |
281 case net::ERR_TOO_MANY_REDIRECTS: | 281 case net::ERR_TOO_MANY_REDIRECTS: |
282 return REQUEST_ERROR_TOO_MANY_REDIRECTS; | 282 return REQUEST_ERROR_TOO_MANY_REDIRECTS; |
283 } | 283 } |
284 return REQUEST_ERROR_UNKNOWN; | 284 return REQUEST_ERROR_UNKNOWN; |
285 } | 285 } |
286 | 286 |
287 static ScopedJavaLocalRef<jstring> GetErrorString(JNIEnv* env, | 287 static jstring GetErrorString(JNIEnv* env, |
288 jobject jcaller, | 288 jobject jcaller, |
289 jlong jurl_request_adapter) { | 289 jlong jurl_request_adapter) { |
290 URLRequestAdapter* request_adapter = | 290 URLRequestAdapter* request_adapter = |
291 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 291 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
292 DCHECK(request_adapter); | 292 DCHECK(request_adapter); |
293 int error_code = request_adapter->error_code(); | 293 int error_code = request_adapter->error_code(); |
294 char buffer[200]; | 294 char buffer[200]; |
295 std::string error_string = net::ErrorToString(error_code); | 295 std::string error_string = net::ErrorToString(error_code); |
296 snprintf(buffer, | 296 snprintf(buffer, |
297 sizeof(buffer), | 297 sizeof(buffer), |
298 "System error: %s(%d)", | 298 "System error: %s(%d)", |
299 error_string.c_str(), | 299 error_string.c_str(), |
300 error_code); | 300 error_code); |
301 return ConvertUTF8ToJavaString(env, buffer); | 301 return ConvertUTF8ToJavaString(env, buffer).Release(); |
302 } | 302 } |
303 | 303 |
304 static jint GetHttpStatusCode(JNIEnv* env, | 304 static jint GetHttpStatusCode(JNIEnv* env, |
305 jobject jcaller, | 305 jobject jcaller, |
306 jlong jurl_request_adapter) { | 306 jlong jurl_request_adapter) { |
307 URLRequestAdapter* request_adapter = | 307 URLRequestAdapter* request_adapter = |
308 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 308 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
309 DCHECK(request_adapter); | 309 DCHECK(request_adapter); |
310 return request_adapter->http_status_code(); | 310 return request_adapter->http_status_code(); |
311 } | 311 } |
312 | 312 |
313 static ScopedJavaLocalRef<jstring> | 313 static jstring GetHttpStatusText(JNIEnv* env, |
314 GetHttpStatusText(JNIEnv* env, jobject jcaller, jlong jurl_request_adapter) { | 314 jobject jcaller, |
| 315 jlong jurl_request_adapter) { |
315 URLRequestAdapter* request_adapter = | 316 URLRequestAdapter* request_adapter = |
316 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 317 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
317 DCHECK(request_adapter); | 318 DCHECK(request_adapter); |
318 return ConvertUTF8ToJavaString(env, request_adapter->http_status_text()); | 319 return ConvertUTF8ToJavaString(env, request_adapter->http_status_text()) |
| 320 .Release(); |
319 } | 321 } |
320 | 322 |
321 static ScopedJavaLocalRef<jstring> GetContentType(JNIEnv* env, | 323 static jstring GetContentType(JNIEnv* env, |
322 jobject jcaller, | 324 jobject jcaller, |
323 jlong jurl_request_adapter) { | 325 jlong jurl_request_adapter) { |
324 URLRequestAdapter* request_adapter = | 326 URLRequestAdapter* request_adapter = |
325 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 327 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
326 DCHECK(request_adapter); | 328 DCHECK(request_adapter); |
327 std::string type = request_adapter->content_type(); | 329 std::string type = request_adapter->content_type(); |
328 if (!type.empty()) { | 330 if (!type.empty()) { |
329 return ConvertUTF8ToJavaString(env, type.c_str()); | 331 return ConvertUTF8ToJavaString(env, type.c_str()).Release(); |
330 } else { | 332 } else { |
331 return ScopedJavaLocalRef<jstring>(); | 333 return NULL; |
332 } | 334 } |
333 } | 335 } |
334 | 336 |
335 static jlong GetContentLength(JNIEnv* env, | 337 static jlong GetContentLength(JNIEnv* env, |
336 jobject jcaller, | 338 jobject jcaller, |
337 jlong jurl_request_adapter) { | 339 jlong jurl_request_adapter) { |
338 URLRequestAdapter* request_adapter = | 340 URLRequestAdapter* request_adapter = |
339 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 341 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
340 DCHECK(request_adapter); | 342 DCHECK(request_adapter); |
341 return request_adapter->content_length(); | 343 return request_adapter->content_length(); |
342 } | 344 } |
343 | 345 |
344 static ScopedJavaLocalRef<jstring> GetHeader(JNIEnv* env, | 346 static jstring GetHeader(JNIEnv* env, |
345 jobject jcaller, | 347 jobject jcaller, |
346 jlong jurl_request_adapter, | 348 jlong jurl_request_adapter, |
347 jstring jheader_name) { | 349 jstring jheader_name) { |
348 URLRequestAdapter* request_adapter = | 350 URLRequestAdapter* request_adapter = |
349 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 351 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
350 DCHECK(request_adapter); | 352 DCHECK(request_adapter); |
351 std::string header_name = ConvertJavaStringToUTF8(env, jheader_name); | 353 std::string header_name = ConvertJavaStringToUTF8(env, jheader_name); |
352 std::string header_value = request_adapter->GetHeader(header_name); | 354 std::string header_value = request_adapter->GetHeader(header_name); |
353 if (!header_value.empty()) | 355 if (!header_value.empty()) |
354 return ConvertUTF8ToJavaString(env, header_value.c_str()); | 356 return ConvertUTF8ToJavaString(env, header_value.c_str()).Release(); |
355 return ScopedJavaLocalRef<jstring>(); | 357 return NULL; |
356 } | 358 } |
357 | 359 |
358 static void GetAllHeaders(JNIEnv* env, | 360 static void GetAllHeaders(JNIEnv* env, |
359 jobject jcaller, | 361 jobject jcaller, |
360 jlong jurl_request_adapter, | 362 jlong jurl_request_adapter, |
361 jobject jheaders_map) { | 363 jobject jheaders_map) { |
362 URLRequestAdapter* request_adapter = | 364 URLRequestAdapter* request_adapter = |
363 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 365 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
364 DCHECK(request_adapter); | 366 DCHECK(request_adapter); |
365 | 367 |
(...skipping 14 matching lines...) Expand all Loading... |
380 } | 382 } |
381 | 383 |
382 // Some implementations (notably HttpURLConnection) include a mapping for the | 384 // Some implementations (notably HttpURLConnection) include a mapping for the |
383 // null key; in HTTP's case, this maps to the HTTP status line. | 385 // null key; in HTTP's case, this maps to the HTTP status line. |
384 ScopedJavaLocalRef<jstring> status_line = | 386 ScopedJavaLocalRef<jstring> status_line = |
385 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); | 387 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); |
386 Java_ChromiumUrlRequest_onAppendResponseHeader(env, jcaller, jheaders_map, | 388 Java_ChromiumUrlRequest_onAppendResponseHeader(env, jcaller, jheaders_map, |
387 NULL, status_line.obj()); | 389 NULL, status_line.obj()); |
388 } | 390 } |
389 | 391 |
390 static ScopedJavaLocalRef<jstring> GetNegotiatedProtocol( | 392 static jstring GetNegotiatedProtocol(JNIEnv* env, |
391 JNIEnv* env, | 393 jobject jcaller, |
392 jobject jcaller, | 394 jlong jurl_request_adapter) { |
393 jlong jurl_request_adapter) { | |
394 URLRequestAdapter* request_adapter = | 395 URLRequestAdapter* request_adapter = |
395 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 396 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
396 DCHECK(request_adapter); | 397 DCHECK(request_adapter); |
397 | 398 |
398 std::string negotiated_protocol = request_adapter->GetNegotiatedProtocol(); | 399 std::string negotiated_protocol = request_adapter->GetNegotiatedProtocol(); |
399 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()); | 400 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()).Release(); |
400 } | 401 } |
401 | 402 |
402 static jboolean GetWasCached(JNIEnv* env, | 403 static jboolean GetWasCached(JNIEnv* env, |
403 jobject jcaller, | 404 jobject jcaller, |
404 jlong jurl_request_adapter) { | 405 jlong jurl_request_adapter) { |
405 URLRequestAdapter* request_adapter = | 406 URLRequestAdapter* request_adapter = |
406 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 407 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
407 DCHECK(request_adapter); | 408 DCHECK(request_adapter); |
408 | 409 |
409 bool was_cached = request_adapter->GetWasCached(); | 410 bool was_cached = request_adapter->GetWasCached(); |
410 return was_cached ? JNI_TRUE : JNI_FALSE; | 411 return was_cached ? JNI_TRUE : JNI_FALSE; |
411 } | 412 } |
412 | 413 |
413 static void DisableRedirects(JNIEnv* env, jobject jcaller, | 414 static void DisableRedirects(JNIEnv* env, jobject jcaller, |
414 jlong jrequest_adapter) { | 415 jlong jrequest_adapter) { |
415 URLRequestAdapter* request_adapter = | 416 URLRequestAdapter* request_adapter = |
416 reinterpret_cast<URLRequestAdapter*>(jrequest_adapter); | 417 reinterpret_cast<URLRequestAdapter*>(jrequest_adapter); |
417 DCHECK(request_adapter); | 418 DCHECK(request_adapter); |
418 request_adapter->DisableRedirects(); | 419 request_adapter->DisableRedirects(); |
419 } | 420 } |
420 | 421 |
421 } // namespace cronet | 422 } // namespace cronet |
OLD | NEW |