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 jstring GetErrorString(JNIEnv* env, | 287 static ScopedJavaLocalRef<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).Release(); | 301 return ConvertUTF8ToJavaString(env, buffer); |
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 jstring GetHttpStatusText(JNIEnv* env, | 313 static ScopedJavaLocalRef<jstring> |
314 jobject jcaller, | 314 GetHttpStatusText(JNIEnv* env, jobject jcaller, jlong jurl_request_adapter) { |
315 jlong jurl_request_adapter) { | |
316 URLRequestAdapter* request_adapter = | 315 URLRequestAdapter* request_adapter = |
317 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 316 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
318 DCHECK(request_adapter); | 317 DCHECK(request_adapter); |
319 return ConvertUTF8ToJavaString(env, request_adapter->http_status_text()) | 318 return ConvertUTF8ToJavaString(env, request_adapter->http_status_text()); |
320 .Release(); | |
321 } | 319 } |
322 | 320 |
323 static jstring GetContentType(JNIEnv* env, | 321 static ScopedJavaLocalRef<jstring> GetContentType(JNIEnv* env, |
324 jobject jcaller, | 322 jobject jcaller, |
325 jlong jurl_request_adapter) { | 323 jlong jurl_request_adapter) { |
326 URLRequestAdapter* request_adapter = | 324 URLRequestAdapter* request_adapter = |
327 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 325 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
328 DCHECK(request_adapter); | 326 DCHECK(request_adapter); |
329 std::string type = request_adapter->content_type(); | 327 std::string type = request_adapter->content_type(); |
330 if (!type.empty()) { | 328 if (!type.empty()) { |
331 return ConvertUTF8ToJavaString(env, type.c_str()).Release(); | 329 return ConvertUTF8ToJavaString(env, type.c_str()); |
332 } else { | 330 } else { |
333 return NULL; | 331 return ScopedJavaLocalRef<jstring>(); |
334 } | 332 } |
335 } | 333 } |
336 | 334 |
337 static jlong GetContentLength(JNIEnv* env, | 335 static jlong GetContentLength(JNIEnv* env, |
338 jobject jcaller, | 336 jobject jcaller, |
339 jlong jurl_request_adapter) { | 337 jlong jurl_request_adapter) { |
340 URLRequestAdapter* request_adapter = | 338 URLRequestAdapter* request_adapter = |
341 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 339 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
342 DCHECK(request_adapter); | 340 DCHECK(request_adapter); |
343 return request_adapter->content_length(); | 341 return request_adapter->content_length(); |
344 } | 342 } |
345 | 343 |
346 static jstring GetHeader(JNIEnv* env, | 344 static ScopedJavaLocalRef<jstring> GetHeader(JNIEnv* env, |
347 jobject jcaller, | 345 jobject jcaller, |
348 jlong jurl_request_adapter, | 346 jlong jurl_request_adapter, |
349 jstring jheader_name) { | 347 jstring jheader_name) { |
350 URLRequestAdapter* request_adapter = | 348 URLRequestAdapter* request_adapter = |
351 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 349 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
352 DCHECK(request_adapter); | 350 DCHECK(request_adapter); |
353 std::string header_name = ConvertJavaStringToUTF8(env, jheader_name); | 351 std::string header_name = ConvertJavaStringToUTF8(env, jheader_name); |
354 std::string header_value = request_adapter->GetHeader(header_name); | 352 std::string header_value = request_adapter->GetHeader(header_name); |
355 if (!header_value.empty()) | 353 if (!header_value.empty()) |
356 return ConvertUTF8ToJavaString(env, header_value.c_str()).Release(); | 354 return ConvertUTF8ToJavaString(env, header_value.c_str()); |
357 return NULL; | 355 return ScopedJavaLocalRef<jstring>(); |
358 } | 356 } |
359 | 357 |
360 static void GetAllHeaders(JNIEnv* env, | 358 static void GetAllHeaders(JNIEnv* env, |
361 jobject jcaller, | 359 jobject jcaller, |
362 jlong jurl_request_adapter, | 360 jlong jurl_request_adapter, |
363 jobject jheaders_map) { | 361 jobject jheaders_map) { |
364 URLRequestAdapter* request_adapter = | 362 URLRequestAdapter* request_adapter = |
365 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 363 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
366 DCHECK(request_adapter); | 364 DCHECK(request_adapter); |
367 | 365 |
(...skipping 14 matching lines...) Expand all Loading... |
382 } | 380 } |
383 | 381 |
384 // Some implementations (notably HttpURLConnection) include a mapping for the | 382 // Some implementations (notably HttpURLConnection) include a mapping for the |
385 // null key; in HTTP's case, this maps to the HTTP status line. | 383 // null key; in HTTP's case, this maps to the HTTP status line. |
386 ScopedJavaLocalRef<jstring> status_line = | 384 ScopedJavaLocalRef<jstring> status_line = |
387 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); | 385 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); |
388 Java_ChromiumUrlRequest_onAppendResponseHeader(env, jcaller, jheaders_map, | 386 Java_ChromiumUrlRequest_onAppendResponseHeader(env, jcaller, jheaders_map, |
389 NULL, status_line.obj()); | 387 NULL, status_line.obj()); |
390 } | 388 } |
391 | 389 |
392 static jstring GetNegotiatedProtocol(JNIEnv* env, | 390 static ScopedJavaLocalRef<jstring> GetNegotiatedProtocol( |
393 jobject jcaller, | 391 JNIEnv* env, |
394 jlong jurl_request_adapter) { | 392 jobject jcaller, |
| 393 jlong jurl_request_adapter) { |
395 URLRequestAdapter* request_adapter = | 394 URLRequestAdapter* request_adapter = |
396 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 395 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
397 DCHECK(request_adapter); | 396 DCHECK(request_adapter); |
398 | 397 |
399 std::string negotiated_protocol = request_adapter->GetNegotiatedProtocol(); | 398 std::string negotiated_protocol = request_adapter->GetNegotiatedProtocol(); |
400 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()).Release(); | 399 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()); |
401 } | 400 } |
402 | 401 |
403 static jboolean GetWasCached(JNIEnv* env, | 402 static jboolean GetWasCached(JNIEnv* env, |
404 jobject jcaller, | 403 jobject jcaller, |
405 jlong jurl_request_adapter) { | 404 jlong jurl_request_adapter) { |
406 URLRequestAdapter* request_adapter = | 405 URLRequestAdapter* request_adapter = |
407 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); | 406 reinterpret_cast<URLRequestAdapter*>(jurl_request_adapter); |
408 DCHECK(request_adapter); | 407 DCHECK(request_adapter); |
409 | 408 |
410 bool was_cached = request_adapter->GetWasCached(); | 409 bool was_cached = request_adapter->GetWasCached(); |
411 return was_cached ? JNI_TRUE : JNI_FALSE; | 410 return was_cached ? JNI_TRUE : JNI_FALSE; |
412 } | 411 } |
413 | 412 |
414 static void DisableRedirects(JNIEnv* env, jobject jcaller, | 413 static void DisableRedirects(JNIEnv* env, jobject jcaller, |
415 jlong jrequest_adapter) { | 414 jlong jrequest_adapter) { |
416 URLRequestAdapter* request_adapter = | 415 URLRequestAdapter* request_adapter = |
417 reinterpret_cast<URLRequestAdapter*>(jrequest_adapter); | 416 reinterpret_cast<URLRequestAdapter*>(jrequest_adapter); |
418 DCHECK(request_adapter); | 417 DCHECK(request_adapter); |
419 request_adapter->DisableRedirects(); | 418 request_adapter->DisableRedirects(); |
420 } | 419 } |
421 | 420 |
422 } // namespace cronet | 421 } // namespace cronet |
OLD | NEW |