| Index: net/cronet/android/org_chromium_net_UrlRequest.cc
|
| diff --git a/net/cronet/android/org_chromium_net_UrlRequest.cc b/net/cronet/android/org_chromium_net_UrlRequest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3be2e7a074c4e7ef74f30646bb5a67d33296d5c4
|
| --- /dev/null
|
| +++ b/net/cronet/android/org_chromium_net_UrlRequest.cc
|
| @@ -0,0 +1,300 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "net/cronet/android/org_chromium_net_UrlRequest.h"
|
| +
|
| +#include <stdio.h>
|
| +
|
| +#include "net/base/net_errors.h"
|
| +#include "net/base/request_priority.h"
|
| +#include "net/cronet/android/android_log.h"
|
| +#include "net/cronet/android/org_chromium_net_UrlRequestContext.h"
|
| +#include "net/cronet/android/url_request_context_peer.h"
|
| +#include "net/cronet/android/url_request_peer.h"
|
| +
|
| +#define LOG_TAG "ChromiumNetwork"
|
| +
|
| +static jclass g_class;
|
| +static jmethodID g_method_finish;
|
| +static jmethodID g_method_onResponseStarted;
|
| +static jmethodID g_method_onReadBytes;
|
| +static jclass g_class_OutputStream;
|
| +static jmethodID g_method_write;
|
| +static jfieldID g_request_field;
|
| +
|
| +static void InitializeJniGlobals(JNIEnv* env) {
|
| + if (g_class != NULL) {
|
| + return;
|
| + }
|
| +
|
| + g_class = (jclass) env->NewGlobalRef(
|
| + env->FindClass("org/chromium/net/UrlRequest"));
|
| + g_method_finish = env->GetMethodID(g_class, "finish", "()V");
|
| + g_method_onResponseStarted =
|
| + env->GetMethodID(g_class, "onResponseStarted", "()V");
|
| + g_method_onReadBytes = env->GetMethodID(g_class, "onBytesRead",
|
| + "(Ljava/nio/ByteBuffer;)V");
|
| + g_request_field = env->GetFieldID(g_class, "mRequest", "J");
|
| +
|
| + g_class_OutputStream = (jclass) env->NewGlobalRef(
|
| + env->FindClass("java/io/OutputStream"));
|
| + g_method_write = env->GetMethodID(g_class_OutputStream, "write", "([BII)V");
|
| +}
|
| +
|
| +static net::RequestPriority ConvertRequestPriority(jint request_priority) {
|
| + switch (request_priority) {
|
| + case REQUEST_PRIORITY_IDLE: return net::IDLE;
|
| + case REQUEST_PRIORITY_LOWEST: return net::LOWEST;
|
| + case REQUEST_PRIORITY_LOW: return net::LOW;
|
| + case REQUEST_PRIORITY_MEDIUM: return net::MEDIUM;
|
| + case REQUEST_PRIORITY_HIGHEST: return net::HIGHEST;
|
| + default: return net::LOWEST;
|
| + }
|
| +}
|
| +
|
| +/*
|
| + * A delegate of URLRequestPeer that delivers callbacks to the Java layer.
|
| + */
|
| +class JniURLRequestPeerDelegate :
|
| + public URLRequestPeer::URLRequestPeerDelegate {
|
| + jobject owner_;
|
| + JavaVM* vm_;
|
| +
|
| + public:
|
| + JniURLRequestPeerDelegate(JNIEnv* env, jobject owner) {
|
| + owner_ = env->NewGlobalRef(owner);
|
| + env->GetJavaVM(&vm_);
|
| + }
|
| +
|
| + ~JniURLRequestPeerDelegate() {
|
| + GetEnv(vm_)->DeleteGlobalRef(owner_);
|
| + }
|
| +
|
| + virtual void OnResponseStarted(URLRequestPeer* request) {
|
| + JNIEnv* env = GetEnv(vm_);
|
| + env->CallVoidMethod(owner_, g_method_onResponseStarted);
|
| + if (env->ExceptionOccurred()) {
|
| + env->ExceptionDescribe();
|
| + env->ExceptionClear();
|
| + }
|
| + }
|
| +
|
| + virtual void OnBytesRead(URLRequestPeer* request) {
|
| + int bytes_read = request->BytesRead();
|
| + if (bytes_read != 0) {
|
| + JNIEnv* env = GetEnv(vm_);
|
| + jobject bytebuf = env->NewDirectByteBuffer(request->Data(), bytes_read);
|
| + env->CallVoidMethod(owner_, g_method_onReadBytes, bytebuf);
|
| + env->DeleteLocalRef(bytebuf);
|
| + if (env->ExceptionOccurred()) {
|
| + env->ExceptionDescribe();
|
| + env->ExceptionClear();
|
| + }
|
| + }
|
| + }
|
| +
|
| + virtual void OnRequestFinished(URLRequestPeer* request) {
|
| + JNIEnv* env = GetEnv(vm_);
|
| + env->CallVoidMethod(owner_, g_method_finish);
|
| + if (env->ExceptionOccurred()) {
|
| + env->ExceptionDescribe();
|
| + env->ExceptionClear();
|
| + }
|
| + }
|
| +};
|
| +
|
| +/*
|
| + * Stores a reference to the request in a java field.
|
| + */
|
| +static void SetNativeObject(JNIEnv *env, jobject object,
|
| + URLRequestPeer* request) {
|
| + env->SetLongField(object, g_request_field,
|
| + reinterpret_cast<jlong>(request));
|
| +}
|
| +
|
| +/*
|
| + * Returns a reference to the request, which is stored in a field of
|
| + * the java object.
|
| + */
|
| +static URLRequestPeer* GetNativeObject(JNIEnv* env, jobject object) {
|
| + return reinterpret_cast<URLRequestPeer *>(
|
| + env->GetLongField(object, g_request_field));
|
| +}
|
| +
|
| +JNIEXPORT void JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeInit(
|
| + JNIEnv* env, jobject object, jobject request_context, jstring url_string,
|
| + jint priority) {
|
| + InitializeJniGlobals(env);
|
| +
|
| + URLRequestContextPeer* context =
|
| + GetURLRequestContextPeer(env, request_context);
|
| + CHECK(context != NULL);
|
| +
|
| + const char *url_utf8 = env->GetStringUTFChars(url_string, NULL);
|
| +
|
| + LOGD(LOG_TAG, context->logging_level(),
|
| + "New chromium network request. URL: %s", url_utf8);
|
| +
|
| + GURL url(url_utf8);
|
| +
|
| + env->ReleaseStringUTFChars(url_string, url_utf8);
|
| +
|
| + URLRequestPeer* request =
|
| + new URLRequestPeer(context, new JniURLRequestPeerDelegate(env, object),
|
| + url, ConvertRequestPriority(priority));
|
| +
|
| + SetNativeObject(env, object, request);
|
| +}
|
| +
|
| +/* synchronized */
|
| +JNIEXPORT void JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeAddHeader(
|
| + JNIEnv* env, jobject object, jstring name, jstring value) {
|
| + URLRequestPeer* request = GetNativeObject(env, object);
|
| + CHECK(request != NULL);
|
| +
|
| + const char *name_utf8 = env->GetStringUTFChars(name, NULL);
|
| + std::string name_string(name_utf8);
|
| + env->ReleaseStringUTFChars(name, name_utf8);
|
| +
|
| + const char *value_utf8 = env->GetStringUTFChars(value, NULL);
|
| + std::string value_string(value_utf8);
|
| + env->ReleaseStringUTFChars(value, value_utf8);
|
| +
|
| + request->AddHeader(name_string, value_string);
|
| +}
|
| +
|
| +JNIEXPORT void JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeSetPostData(
|
| + JNIEnv* env, jobject object, jstring content_type, jbyteArray content) {
|
| + URLRequestPeer* request = GetNativeObject(env, object);
|
| + CHECK(request != NULL);
|
| +
|
| + std::string method_post("POST");
|
| + request->SetMethod(method_post);
|
| +
|
| + std::string content_type_header("Content-Type");
|
| +
|
| + const char *content_type_utf8 = env->GetStringUTFChars(content_type, NULL);
|
| + std::string content_type_string(content_type_utf8);
|
| + env->ReleaseStringUTFChars(content_type, content_type_utf8);
|
| +
|
| + request->AddHeader(content_type_header, content_type_string);
|
| +
|
| + if (content != NULL) {
|
| + jsize size = env->GetArrayLength(content);
|
| + if (size > 0) {
|
| + jbyte* content_bytes = env->GetByteArrayElements(content, NULL);
|
| + request->SetPostContent(reinterpret_cast<const char*>(content_bytes),
|
| + size);
|
| + env->ReleaseByteArrayElements(content, content_bytes, 0);
|
| + }
|
| + }
|
| +}
|
| +
|
| +/* synchronized */
|
| +JNIEXPORT void JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeStart(
|
| + JNIEnv* env, jobject object) {
|
| + URLRequestPeer* request = GetNativeObject(env, object);
|
| + if (request != NULL) {
|
| + request->Start();
|
| + }
|
| +}
|
| +
|
| +/* synchronized */
|
| +JNIEXPORT void JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeRecycle(
|
| + JNIEnv* env, jobject object) {
|
| + URLRequestPeer* request = GetNativeObject(env, object);
|
| + if (request != NULL) {
|
| + request->Destroy();
|
| + }
|
| +
|
| + SetNativeObject(env, object, NULL);
|
| +}
|
| +
|
| +/* synchronized */
|
| +JNIEXPORT void JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeCancel(
|
| + JNIEnv* env, jobject object) {
|
| + URLRequestPeer* request = GetNativeObject(env, object);
|
| + if (request != NULL) {
|
| + request->Cancel();
|
| + }
|
| +}
|
| +
|
| +JNIEXPORT jint JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeGetErrorCode(
|
| + JNIEnv* env, jobject object) {
|
| + URLRequestPeer* request = GetNativeObject(env, object);
|
| + int error_code = request->error_code();
|
| + switch (error_code) {
|
| + case 0:
|
| + return ERROR_SUCCESS;
|
| +
|
| + // Comment from src/content/browser/download/download_resource_handler.cc:
|
| + // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
|
| + // allowed since a number of servers in the wild close the connection too
|
| + // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
|
| + // treat downloads as complete in both cases, so we follow their lead.
|
| + case net::ERR_CONTENT_LENGTH_MISMATCH:
|
| + case net::ERR_INCOMPLETE_CHUNKED_ENCODING:
|
| + return ERROR_SUCCESS;
|
| +
|
| + case net::ERR_INVALID_URL:
|
| + case net::ERR_DISALLOWED_URL_SCHEME:
|
| + case net::ERR_UNKNOWN_URL_SCHEME:
|
| + return ERROR_MALFORMED_URL;
|
| +
|
| + case net::ERR_CONNECTION_TIMED_OUT:
|
| + return ERROR_CONNECTION_TIMED_OUT;
|
| +
|
| + case net::ERR_NAME_NOT_RESOLVED:
|
| + return ERROR_UNKNOWN_HOST;
|
| + }
|
| + return ERROR_UNKNOWN;
|
| +}
|
| +
|
| +JNIEXPORT jstring JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeGetErrorString(
|
| + JNIEnv* env, jobject object) {
|
| + int error_code = GetNativeObject(env, object)->error_code();
|
| + char buffer[200];
|
| + snprintf(buffer, sizeof(buffer), "System error: %s(%d)",
|
| + net::ErrorToString(error_code), error_code);
|
| + return env->NewStringUTF(buffer);
|
| +}
|
| +
|
| +JNIEXPORT jint JNICALL
|
| +Java_org_chromium_net_UrlRequest_getHttpStatusCode(
|
| + JNIEnv* env, jobject object) {
|
| + return GetNativeObject(env, object)->http_status_code();
|
| +}
|
| +
|
| +JNIEXPORT jstring JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeGetContentType(
|
| + JNIEnv* env, jobject object) {
|
| + URLRequestPeer* request = GetNativeObject(env, object);
|
| + if (request == NULL) {
|
| + return NULL;
|
| + }
|
| + std::string type = request->content_type();
|
| + if (!type.empty()) {
|
| + return env->NewStringUTF(type.c_str());
|
| + } else {
|
| + return NULL;
|
| + }
|
| +}
|
| +
|
| +JNIEXPORT jlong JNICALL
|
| +Java_org_chromium_net_UrlRequest_nativeGetContentLength(
|
| + JNIEnv* env, jobject object) {
|
| + URLRequestPeer* request = GetNativeObject(env, object);
|
| + if (request == NULL) {
|
| + return 0;
|
| + }
|
| + return request->content_length();
|
| +}
|
|
|