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 "net/cronet/android/org_chromium_net_UrlRequest.h"
9 #include "base/macros.h"
10 #include "net/base/net_errors.h"
11 #include "net/base/request_priority.h"
12 #include "net/cronet/android/org_chromium_net_UrlRequestContext.h"
13 #include "net/cronet/android/url_request_context_peer.h"
14 #include "net/cronet/android/url_request_peer.h"
16 // TODO(mef): Replace following definitions with generated UrlRequest_jni.h
17 //#include "jni/UrlRequest_jni.h"
21 jmethodID g_method_finish;
22 jmethodID g_method_onAppendChunkCompleted;
23 jmethodID g_method_onResponseStarted;
24 jmethodID g_method_onReadBytes;
25 jclass g_class_OutputStream;
26 jmethodID g_method_write;
27 jfieldID g_request_field;
29 net::RequestPriority ConvertRequestPriority(jint request_priority) {
30 switch (request_priority) {
31 case REQUEST_PRIORITY_IDLE:
33 case REQUEST_PRIORITY_LOWEST:
35 case REQUEST_PRIORITY_LOW:
37 case REQUEST_PRIORITY_MEDIUM:
39 case REQUEST_PRIORITY_HIGHEST:
46 // Stores a reference to the request in a java field.
47 void SetNativeObject(JNIEnv* env, jobject object, URLRequestPeer* request) {
48 env->SetLongField(object, g_request_field, reinterpret_cast<jlong>(request));
51 // Returns a reference to the request, which is stored in a field of the Java
53 URLRequestPeer* GetNativeObject(JNIEnv* env, jobject object) {
54 return reinterpret_cast<URLRequestPeer*>(
55 env->GetLongField(object, g_request_field));
58 void SetPostContentType(JNIEnv* env,
59 URLRequestPeer* request,
60 jstring content_type) {
61 DCHECK(request != NULL);
63 std::string method_post("POST");
64 request->SetMethod(method_post);
66 std::string content_type_header("Content-Type");
68 const char* content_type_utf8 = env->GetStringUTFChars(content_type, NULL);
69 std::string content_type_string(content_type_utf8);
70 env->ReleaseStringUTFChars(content_type, content_type_utf8);
72 request->AddHeader(content_type_header, content_type_string);
77 // Find Java classes and retain them.
78 bool UrlRequestRegisterJni(JNIEnv* env) {
79 g_class = reinterpret_cast<jclass>(
80 env->NewGlobalRef(env->FindClass("org/chromium/net/UrlRequest")));
81 g_method_finish = env->GetMethodID(g_class, "finish", "()V");
82 g_method_onAppendChunkCompleted =
83 env->GetMethodID(g_class, "onAppendChunkCompleted", "()V");
84 g_method_onResponseStarted =
85 env->GetMethodID(g_class, "onResponseStarted", "()V");
86 g_method_onReadBytes =
87 env->GetMethodID(g_class, "onBytesRead", "(Ljava/nio/ByteBuffer;)V");
88 g_request_field = env->GetFieldID(g_class, "mRequest", "J");
90 g_class_OutputStream = reinterpret_cast<jclass>(
91 env->NewGlobalRef(env->FindClass("java/io/OutputStream")));
92 g_method_write = env->GetMethodID(g_class_OutputStream, "write", "([BII)V");
94 if (!g_class || !g_method_finish || !g_method_onAppendChunkCompleted ||
95 !g_method_onResponseStarted || !g_method_onReadBytes ||
96 !g_request_field || !g_class_OutputStream || !g_method_write) {
102 // A delegate of URLRequestPeer that delivers callbacks to the Java layer.
103 class JniURLRequestPeerDelegate
104 : public URLRequestPeer::URLRequestPeerDelegate {
106 JniURLRequestPeerDelegate(JNIEnv* env, jobject owner) {
107 owner_ = env->NewGlobalRef(owner);
108 env->GetJavaVM(&vm_);
111 virtual void OnAppendChunkCompleted(URLRequestPeer* request) OVERRIDE {
112 JNIEnv* env = GetEnv(vm_);
113 env->CallVoidMethod(owner_, g_method_onAppendChunkCompleted);
114 if (env->ExceptionOccurred()) {
115 env->ExceptionDescribe();
116 env->ExceptionClear();
120 virtual void OnResponseStarted(URLRequestPeer* request) OVERRIDE {
121 JNIEnv* env = GetEnv(vm_);
122 env->CallVoidMethod(owner_, g_method_onResponseStarted);
123 if (env->ExceptionOccurred()) {
124 env->ExceptionDescribe();
125 env->ExceptionClear();
129 virtual void OnBytesRead(URLRequestPeer* request) OVERRIDE {
130 int bytes_read = request->bytes_read();
131 if (bytes_read != 0) {
132 JNIEnv* env = GetEnv(vm_);
133 jobject bytebuf = env->NewDirectByteBuffer(request->Data(), bytes_read);
134 env->CallVoidMethod(owner_, g_method_onReadBytes, bytebuf);
135 env->DeleteLocalRef(bytebuf);
136 if (env->ExceptionOccurred()) {
137 env->ExceptionDescribe();
138 env->ExceptionClear();
143 virtual void OnRequestFinished(URLRequestPeer* request) OVERRIDE {
144 JNIEnv* env = GetEnv(vm_);
145 env->CallVoidMethod(owner_, g_method_finish);
146 if (env->ExceptionOccurred()) {
147 env->ExceptionDescribe();
148 env->ExceptionClear();
153 virtual ~JniURLRequestPeerDelegate() { GetEnv(vm_)->DeleteGlobalRef(owner_); }
159 DISALLOW_COPY_AND_ASSIGN(JniURLRequestPeerDelegate);
162 JNIEXPORT void JNICALL
163 Java_org_chromium_net_UrlRequest_nativeInit(JNIEnv* env,
165 jobject request_context,
168 URLRequestContextPeer* context =
169 GetURLRequestContextPeer(env, request_context);
170 DCHECK(context != NULL);
172 const char* url_utf8 = env->GetStringUTFChars(url_string, NULL);
174 DVLOG(context->logging_level())
175 << "New chromium network request. URL:" << url_utf8;
179 env->ReleaseStringUTFChars(url_string, url_utf8);
181 URLRequestPeer* request =
182 new URLRequestPeer(context,
183 new JniURLRequestPeerDelegate(env, object),
185 ConvertRequestPriority(priority));
187 SetNativeObject(env, object, request);
191 JNIEXPORT void JNICALL
192 Java_org_chromium_net_UrlRequest_nativeAddHeader(JNIEnv* env,
196 URLRequestPeer* request = GetNativeObject(env, object);
197 DCHECK(request != NULL);
199 const char* name_utf8 = env->GetStringUTFChars(name, NULL);
200 std::string name_string(name_utf8);
201 env->ReleaseStringUTFChars(name, name_utf8);
203 const char* value_utf8 = env->GetStringUTFChars(value, NULL);
204 std::string value_string(value_utf8);
205 env->ReleaseStringUTFChars(value, value_utf8);
207 request->AddHeader(name_string, value_string);
210 JNIEXPORT void JNICALL
211 Java_org_chromium_net_UrlRequest_nativeSetPostData(JNIEnv* env,
213 jstring content_type,
214 jbyteArray content) {
215 URLRequestPeer* request = GetNativeObject(env, object);
216 SetPostContentType(env, request, content_type);
218 if (content != NULL) {
219 jsize size = env->GetArrayLength(content);
221 jbyte* content_bytes = env->GetByteArrayElements(content, NULL);
222 request->SetPostContent(reinterpret_cast<const char*>(content_bytes),
224 env->ReleaseByteArrayElements(content, content_bytes, 0);
229 JNIEXPORT void JNICALL
230 Java_org_chromium_net_UrlRequest_nativeBeginChunkedUpload(
233 jstring content_type) {
234 URLRequestPeer* request = GetNativeObject(env, object);
235 SetPostContentType(env, request, content_type);
237 request->EnableStreamingUpload();
240 JNIEXPORT void JNICALL
241 Java_org_chromium_net_UrlRequest_nativeAppendChunk(JNIEnv* env,
243 jobject chunk_byte_buffer,
245 jboolean is_last_chunk) {
246 URLRequestPeer* request = GetNativeObject(env, object);
247 CHECK(request != NULL);
249 if (chunk_byte_buffer != NULL) {
250 void* chunk = env->GetDirectBufferAddress(chunk_byte_buffer);
251 request->AppendChunk(
252 reinterpret_cast<const char*>(chunk), chunk_size, is_last_chunk);
257 JNIEXPORT void JNICALL
258 Java_org_chromium_net_UrlRequest_nativeStart(JNIEnv* env, jobject object) {
259 URLRequestPeer* request = GetNativeObject(env, object);
260 if (request != NULL) {
266 JNIEXPORT void JNICALL
267 Java_org_chromium_net_UrlRequest_nativeRecycle(JNIEnv* env, jobject object) {
268 URLRequestPeer* request = GetNativeObject(env, object);
269 if (request != NULL) {
273 SetNativeObject(env, object, NULL);
277 JNIEXPORT void JNICALL
278 Java_org_chromium_net_UrlRequest_nativeCancel(JNIEnv* env, jobject object) {
279 URLRequestPeer* request = GetNativeObject(env, object);
280 if (request != NULL) {
285 JNIEXPORT jint JNICALL
286 Java_org_chromium_net_UrlRequest_nativeGetErrorCode(JNIEnv* env,
288 URLRequestPeer* request = GetNativeObject(env, object);
289 int error_code = request->error_code();
290 switch (error_code) {
291 // TODO(mef): Investigate returning success on positive values, too, as
292 // they technically indicate success.
294 return ERROR_SUCCESS;
296 // TODO(mef): Investigate this. The fact is that Chrome does not do this,
297 // and this library is not just being used for downloads.
299 // Comment from src/content/browser/download/download_resource_handler.cc:
300 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
301 // allowed since a number of servers in the wild close the connection too
302 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
303 // treat downloads as complete in both cases, so we follow their lead.
304 case net::ERR_CONTENT_LENGTH_MISMATCH:
305 case net::ERR_INCOMPLETE_CHUNKED_ENCODING:
306 return ERROR_SUCCESS;
308 case net::ERR_INVALID_URL:
309 case net::ERR_DISALLOWED_URL_SCHEME:
310 case net::ERR_UNKNOWN_URL_SCHEME:
311 return ERROR_MALFORMED_URL;
313 case net::ERR_CONNECTION_TIMED_OUT:
314 return ERROR_CONNECTION_TIMED_OUT;
316 case net::ERR_NAME_NOT_RESOLVED:
317 return ERROR_UNKNOWN_HOST;
319 return ERROR_UNKNOWN;
322 JNIEXPORT jstring JNICALL
323 Java_org_chromium_net_UrlRequest_nativeGetErrorString(JNIEnv* env,
325 int error_code = GetNativeObject(env, object)->error_code();
329 "System error: %s(%d)",
330 net::ErrorToString(error_code),
332 return env->NewStringUTF(buffer);
335 JNIEXPORT jint JNICALL
336 Java_org_chromium_net_UrlRequest_getHttpStatusCode(JNIEnv* env,
338 return GetNativeObject(env, object)->http_status_code();
341 JNIEXPORT jstring JNICALL
342 Java_org_chromium_net_UrlRequest_nativeGetContentType(JNIEnv* env,
344 URLRequestPeer* request = GetNativeObject(env, object);
345 if (request == NULL) {
348 std::string type = request->content_type();
350 return env->NewStringUTF(type.c_str());
356 JNIEXPORT jlong JNICALL
357 Java_org_chromium_net_UrlRequest_nativeGetContentLength(JNIEnv* env,
359 URLRequestPeer* request = GetNativeObject(env, object);
360 if (request == NULL) {
363 return request->content_length();