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/native/aw_web_resource_response_impl.h" | |
6 | |
7 #include "android_webview/native/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 | |
27 AwWebResourceResponseImpl::~AwWebResourceResponseImpl() { | |
28 } | |
29 | |
30 std::unique_ptr<InputStream> AwWebResourceResponseImpl::GetInputStream( | |
31 JNIEnv* env) const { | |
32 ScopedJavaLocalRef<jobject> jstream = | |
33 Java_AwWebResourceResponse_getData(env, java_object_); | |
34 if (jstream.is_null()) | |
35 return std::unique_ptr<InputStream>(); | |
36 return base::MakeUnique<InputStreamImpl>(jstream); | |
37 } | |
38 | |
39 bool AwWebResourceResponseImpl::GetMimeType(JNIEnv* env, | |
40 std::string* mime_type) const { | |
41 ScopedJavaLocalRef<jstring> jstring_mime_type = | |
42 Java_AwWebResourceResponse_getMimeType(env, java_object_); | |
43 if (jstring_mime_type.is_null()) | |
44 return false; | |
45 *mime_type = ConvertJavaStringToUTF8(jstring_mime_type); | |
46 return true; | |
47 } | |
48 | |
49 bool AwWebResourceResponseImpl::GetCharset( | |
50 JNIEnv* env, std::string* charset) const { | |
51 ScopedJavaLocalRef<jstring> jstring_charset = | |
52 Java_AwWebResourceResponse_getCharset(env, java_object_); | |
53 if (jstring_charset.is_null()) | |
54 return false; | |
55 *charset = ConvertJavaStringToUTF8(jstring_charset); | |
56 return true; | |
57 } | |
58 | |
59 bool AwWebResourceResponseImpl::GetStatusInfo( | |
60 JNIEnv* env, | |
61 int* status_code, | |
62 std::string* reason_phrase) const { | |
63 int status = Java_AwWebResourceResponse_getStatusCode(env, java_object_); | |
64 ScopedJavaLocalRef<jstring> jstring_reason_phrase = | |
65 Java_AwWebResourceResponse_getReasonPhrase(env, java_object_); | |
66 if (status < 100 || status >= 600 || jstring_reason_phrase.is_null()) | |
67 return false; | |
68 *status_code = status; | |
69 *reason_phrase = ConvertJavaStringToUTF8(jstring_reason_phrase); | |
70 return true; | |
71 } | |
72 | |
73 bool AwWebResourceResponseImpl::GetResponseHeaders( | |
74 JNIEnv* env, | |
75 net::HttpResponseHeaders* headers) const { | |
76 ScopedJavaLocalRef<jobjectArray> jstringArray_headerNames = | |
77 Java_AwWebResourceResponse_getResponseHeaderNames(env, java_object_); | |
78 ScopedJavaLocalRef<jobjectArray> jstringArray_headerValues = | |
79 Java_AwWebResourceResponse_getResponseHeaderValues(env, java_object_); | |
80 if (jstringArray_headerNames.is_null() || jstringArray_headerValues.is_null()) | |
81 return false; | |
82 std::vector<std::string> header_names; | |
83 std::vector<std::string> header_values; | |
84 AppendJavaStringArrayToStringVector( | |
85 env, jstringArray_headerNames.obj(), &header_names); | |
86 AppendJavaStringArrayToStringVector( | |
87 env, jstringArray_headerValues.obj(), &header_values); | |
88 DCHECK_EQ(header_values.size(), header_names.size()); | |
89 for(size_t i = 0; i < header_names.size(); ++i) { | |
90 std::string header_line(header_names[i]); | |
91 header_line.append(": "); | |
92 header_line.append(header_values[i]); | |
93 headers->AddHeader(header_line); | |
94 } | |
95 return true; | |
96 } | |
97 | |
98 } // namespace android_webview | |
OLD | NEW |