| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "android_webview/browser/aw_web_resource_response_impl.h" | |
| 6 | |
| 7 #include "android_webview/browser/input_stream_impl.h" | |
| 8 #include "base/android/jni_android.h" | |
| 9 #include "base/android/jni_array.h" | |
| 10 #include "base/android/jni_string.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "jni/AwWebResourceResponse_jni.h" | |
| 13 #include "net/http/http_response_headers.h" | |
| 14 #include "net/url_request/url_request.h" | |
| 15 #include "net/url_request/url_request_job.h" | |
| 16 | |
| 17 using base::android::ScopedJavaLocalRef; | |
| 18 using base::android::AppendJavaStringArrayToStringVector; | |
| 19 | |
| 20 namespace android_webview { | |
| 21 | |
| 22 AwWebResourceResponseImpl::AwWebResourceResponseImpl( | |
| 23 const base::android::JavaRef<jobject>& obj) | |
| 24 : java_object_(obj) {} | |
| 25 | |
| 26 AwWebResourceResponseImpl::~AwWebResourceResponseImpl() {} | |
| 27 | |
| 28 std::unique_ptr<InputStream> AwWebResourceResponseImpl::GetInputStream( | |
| 29 JNIEnv* env) const { | |
| 30 ScopedJavaLocalRef<jobject> jstream = | |
| 31 Java_AwWebResourceResponse_getData(env, java_object_); | |
| 32 if (jstream.is_null()) | |
| 33 return std::unique_ptr<InputStream>(); | |
| 34 return base::MakeUnique<InputStreamImpl>(jstream); | |
| 35 } | |
| 36 | |
| 37 bool AwWebResourceResponseImpl::GetMimeType(JNIEnv* env, | |
| 38 std::string* mime_type) const { | |
| 39 ScopedJavaLocalRef<jstring> jstring_mime_type = | |
| 40 Java_AwWebResourceResponse_getMimeType(env, java_object_); | |
| 41 if (jstring_mime_type.is_null()) | |
| 42 return false; | |
| 43 *mime_type = ConvertJavaStringToUTF8(jstring_mime_type); | |
| 44 return true; | |
| 45 } | |
| 46 | |
| 47 bool AwWebResourceResponseImpl::GetCharset(JNIEnv* env, | |
| 48 std::string* charset) const { | |
| 49 ScopedJavaLocalRef<jstring> jstring_charset = | |
| 50 Java_AwWebResourceResponse_getCharset(env, java_object_); | |
| 51 if (jstring_charset.is_null()) | |
| 52 return false; | |
| 53 *charset = ConvertJavaStringToUTF8(jstring_charset); | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 bool AwWebResourceResponseImpl::GetStatusInfo( | |
| 58 JNIEnv* env, | |
| 59 int* status_code, | |
| 60 std::string* reason_phrase) const { | |
| 61 int status = Java_AwWebResourceResponse_getStatusCode(env, java_object_); | |
| 62 ScopedJavaLocalRef<jstring> jstring_reason_phrase = | |
| 63 Java_AwWebResourceResponse_getReasonPhrase(env, java_object_); | |
| 64 if (status < 100 || status >= 600 || jstring_reason_phrase.is_null()) | |
| 65 return false; | |
| 66 *status_code = status; | |
| 67 *reason_phrase = ConvertJavaStringToUTF8(jstring_reason_phrase); | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 bool AwWebResourceResponseImpl::GetResponseHeaders( | |
| 72 JNIEnv* env, | |
| 73 net::HttpResponseHeaders* headers) const { | |
| 74 ScopedJavaLocalRef<jobjectArray> jstringArray_headerNames = | |
| 75 Java_AwWebResourceResponse_getResponseHeaderNames(env, java_object_); | |
| 76 ScopedJavaLocalRef<jobjectArray> jstringArray_headerValues = | |
| 77 Java_AwWebResourceResponse_getResponseHeaderValues(env, java_object_); | |
| 78 if (jstringArray_headerNames.is_null() || jstringArray_headerValues.is_null()) | |
| 79 return false; | |
| 80 std::vector<std::string> header_names; | |
| 81 std::vector<std::string> header_values; | |
| 82 AppendJavaStringArrayToStringVector(env, jstringArray_headerNames.obj(), | |
| 83 &header_names); | |
| 84 AppendJavaStringArrayToStringVector(env, jstringArray_headerValues.obj(), | |
| 85 &header_values); | |
| 86 DCHECK_EQ(header_values.size(), header_names.size()); | |
| 87 for (size_t i = 0; i < header_names.size(); ++i) { | |
| 88 std::string header_line(header_names[i]); | |
| 89 header_line.append(": "); | |
| 90 header_line.append(header_values[i]); | |
| 91 headers->AddHeader(header_line); | |
| 92 } | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 } // namespace android_webview | |
| OLD | NEW |