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.
5 #include "android_webview/native/aw_web_resource_response_impl.h"
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 "jni/AwWebResourceResponse_jni.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/url_request/url_request.h"
14 #include "net/url_request/url_request_job.h"
16 using base::android::ScopedJavaLocalRef
;
17 using base::android::AppendJavaStringArrayToStringVector
;
19 namespace android_webview
{
21 AwWebResourceResponseImpl::AwWebResourceResponseImpl(
22 const base::android::JavaRef
<jobject
>& obj
)
26 AwWebResourceResponseImpl::~AwWebResourceResponseImpl() {
29 scoped_ptr
<InputStream
>
30 AwWebResourceResponseImpl::GetInputStream(JNIEnv
* env
) const {
31 ScopedJavaLocalRef
<jobject
> jstream
=
32 Java_AwWebResourceResponse_getData(env
, java_object_
.obj());
33 if (jstream
.is_null())
34 return scoped_ptr
<InputStream
>();
35 return make_scoped_ptr
<InputStream
>(new InputStreamImpl(jstream
));
38 bool AwWebResourceResponseImpl::GetMimeType(JNIEnv
* env
,
39 std::string
* mime_type
) const {
40 ScopedJavaLocalRef
<jstring
> jstring_mime_type
=
41 Java_AwWebResourceResponse_getMimeType(env
, java_object_
.obj());
42 if (jstring_mime_type
.is_null())
44 *mime_type
= ConvertJavaStringToUTF8(jstring_mime_type
);
48 bool AwWebResourceResponseImpl::GetCharset(
49 JNIEnv
* env
, std::string
* charset
) const {
50 ScopedJavaLocalRef
<jstring
> jstring_charset
=
51 Java_AwWebResourceResponse_getCharset(env
, java_object_
.obj());
52 if (jstring_charset
.is_null())
54 *charset
= ConvertJavaStringToUTF8(jstring_charset
);
58 bool AwWebResourceResponseImpl::GetStatusInfo(
61 std::string
* reason_phrase
) const {
63 Java_AwWebResourceResponse_getStatusCode(env
, java_object_
.obj());
64 ScopedJavaLocalRef
<jstring
> jstring_reason_phrase
=
65 Java_AwWebResourceResponse_getReasonPhrase(env
, java_object_
.obj());
66 if (status
< 100 || status
>= 600 || jstring_reason_phrase
.is_null())
68 *status_code
= status
;
69 *reason_phrase
= ConvertJavaStringToUTF8(jstring_reason_phrase
);
73 bool AwWebResourceResponseImpl::GetResponseHeaders(
75 net::HttpResponseHeaders
* headers
) const {
76 ScopedJavaLocalRef
<jobjectArray
> jstringArray_headerNames
=
77 Java_AwWebResourceResponse_getResponseHeaderNames(env
,
79 ScopedJavaLocalRef
<jobjectArray
> jstringArray_headerValues
=
80 Java_AwWebResourceResponse_getResponseHeaderValues(env
,
82 if (jstringArray_headerNames
.is_null() || jstringArray_headerValues
.is_null())
84 std::vector
<std::string
> header_names
;
85 std::vector
<std::string
> header_values
;
86 AppendJavaStringArrayToStringVector(
87 env
, jstringArray_headerNames
.obj(), &header_names
);
88 AppendJavaStringArrayToStringVector(
89 env
, jstringArray_headerValues
.obj(), &header_values
);
90 DCHECK(header_names
.size() == header_values
.size());
91 for(size_t i
= 0; i
< header_names
.size(); ++i
) {
92 std::string
header_line(header_names
[i
]);
93 header_line
.append(": ");
94 header_line
.append(header_values
[i
]);
95 headers
->AddHeader(header_line
);
100 bool RegisterAwWebResourceResponse(JNIEnv
* env
) {
101 return RegisterNativesImpl(env
);
104 } // namespace android_webview