Add device_type to SharedPageState as a prelude to removing user_agent_type from...
[chromium-blink-merge.git] / android_webview / native / aw_web_resource_response_impl.cc
blob1fe5b28e18bb1e21193c5469f56616cbfe1b8a3b
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)
23 : java_object_(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())
43 return false;
44 *mime_type = ConvertJavaStringToUTF8(jstring_mime_type);
45 return true;
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())
53 return false;
54 *charset = ConvertJavaStringToUTF8(jstring_charset);
55 return true;
58 bool AwWebResourceResponseImpl::GetStatusInfo(
59 JNIEnv* env,
60 int* status_code,
61 std::string* reason_phrase) const {
62 int status =
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())
67 return false;
68 *status_code = status;
69 *reason_phrase = ConvertJavaStringToUTF8(jstring_reason_phrase);
70 return true;
73 bool AwWebResourceResponseImpl::GetResponseHeaders(
74 JNIEnv* env,
75 net::HttpResponseHeaders* headers) const {
76 ScopedJavaLocalRef<jobjectArray> jstringArray_headerNames =
77 Java_AwWebResourceResponse_getResponseHeaderNames(env,
78 java_object_.obj());
79 ScopedJavaLocalRef<jobjectArray> jstringArray_headerValues =
80 Java_AwWebResourceResponse_getResponseHeaderValues(env,
81 java_object_.obj());
82 if (jstringArray_headerNames.is_null() || jstringArray_headerValues.is_null())
83 return false;
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_EQ(header_values.size(), header_names.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);
97 return true;
100 bool RegisterAwWebResourceResponse(JNIEnv* env) {
101 return RegisterNativesImpl(env);
104 } // namespace android_webview