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..8ad47ad0bc2d23ebba35a2887d3955faae212092 |
--- /dev/null |
+++ b/net/cronet/android/org_chromium_net_UrlRequest.cc |
@@ -0,0 +1,347 @@ |
+// 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" |
szym
2014/02/28 00:50:40
After reading some more on the https://code.google
mef
2014/03/03 19:15:13
Sounds good. Added TODO for now.
|
+ |
+#include <stdio.h> |
+ |
+#include "net/base/net_errors.h" |
+#include "net/base/request_priority.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" |
mmenke
2014/02/27 23:06:02
this isn't used in this file
mef
2014/03/03 19:15:13
Done.
|
+ |
+static jclass g_class; |
+static jmethodID g_method_finish; |
+static jmethodID g_method_onAppendChunkCompleted; |
+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; |
mmenke
2014/02/27 23:06:02
Suggest just putting all these in an anonymous nam
mmenke
2014/02/27 23:06:02
Most of these names violate the Google C++ style g
mef
2014/03/03 19:15:13
Done.
mef
2014/03/03 19:15:13
I'll address this as part of migration to generate
|
+ |
+// Find Java classes and retain them. |
+bool UrlRequestRegisterJni(JNIEnv* env) { |
+ g_class = (jclass) env->NewGlobalRef( |
+ env->FindClass("org/chromium/net/UrlRequest")); |
+ g_method_finish = env->GetMethodID(g_class, "finish", "()V"); |
+ g_method_onAppendChunkCompleted = |
+ env->GetMethodID(g_class, "onAppendChunkCompleted", "()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"); |
+ |
+ if (!g_class |
+ || !g_method_finish |
+ || !g_method_onAppendChunkCompleted |
+ || !g_method_onResponseStarted |
+ || !g_method_onReadBytes |
+ || !g_request_field |
+ || !g_class_OutputStream |
+ || !g_method_write) { |
mmenke
2014/02/27 23:06:02
While this is the right style for Java, and allowe
mef
2014/03/03 19:15:13
Done.
|
+ return false; |
+ } |
+ return true; |
+} |
+ |
+static net::RequestPriority ConvertRequestPriority(jint request_priority) { |
mmenke
2014/02/27 23:06:02
static is not used very often before functions in
mef
2014/03/03 19:15:13
Done.
|
+ 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; |
mmenke
2014/02/27 23:06:02
Put all the returns on the next lines.
mef
2014/03/03 19:15:13
Done.
|
+ } |
+} |
+ |
+/* |
+ * A delegate of URLRequestPeer that delivers callbacks to the Java layer. |
+ */ |
mmenke
2014/02/27 23:06:02
Use C++-style comments. (Goes for entire file)
mef
2014/03/03 19:15:13
Done.
|
+class JniURLRequestPeerDelegate : |
+ public URLRequestPeer::URLRequestPeerDelegate { |
+ jobject owner_; |
+ JavaVM* vm_; |
mmenke
2014/02/27 23:06:02
These should go in a private section at the bottom
mef
2014/03/03 19:15:13
Done.
|
+ |
+ public: |
+ JniURLRequestPeerDelegate(JNIEnv* env, jobject owner) { |
+ owner_ = env->NewGlobalRef(owner); |
+ env->GetJavaVM(&vm_); |
+ } |
+ |
+ virtual void OnAppendChunkCompleted(URLRequestPeer* request) OVERRIDE { |
+ JNIEnv* env = GetEnv(vm_); |
+ env->CallVoidMethod(owner_, g_method_onAppendChunkCompleted); |
+ if (env->ExceptionOccurred()) { |
+ env->ExceptionDescribe(); |
+ env->ExceptionClear(); |
+ } |
+ } |
+ |
+ virtual void OnResponseStarted(URLRequestPeer* request) OVERRIDE { |
+ JNIEnv* env = GetEnv(vm_); |
+ env->CallVoidMethod(owner_, g_method_onResponseStarted); |
+ if (env->ExceptionOccurred()) { |
+ env->ExceptionDescribe(); |
+ env->ExceptionClear(); |
+ } |
+ } |
+ |
+ virtual void OnBytesRead(URLRequestPeer* request) OVERRIDE { |
+ 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) OVERRIDE { |
+ JNIEnv* env = GetEnv(vm_); |
+ env->CallVoidMethod(owner_, g_method_finish); |
+ if (env->ExceptionOccurred()) { |
+ env->ExceptionDescribe(); |
+ env->ExceptionClear(); |
+ } |
+ } |
+ |
+ protected: |
+ virtual ~JniURLRequestPeerDelegate() { |
+ GetEnv(vm_)->DeleteGlobalRef(owner_); |
+ } |
+}; |
+ |
+/* |
+ * Stores a reference to the request in a java field. |
mmenke
2014/02/27 23:06:02
Should Java be capitalized in comments?
mef
2014/03/03 19:15:13
Done.
|
+ */ |
+static void SetNativeObject(JNIEnv *env, jobject object, |
mmenke
2014/02/27 23:06:02
Static methods should be moved to the anonymous na
mef
2014/03/03 19:15:13
Done.
|
+ 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( |
mmenke
2014/02/27 23:06:02
Maybe be able to move all these up a line.
mef
2014/03/03 19:15:13
Done.
|
+ JNIEnv* env, jobject object, jobject request_context, jstring url_string, |
+ jint priority) { |
+ URLRequestContextPeer* context = |
+ GetURLRequestContextPeer(env, request_context); |
+ CHECK(context != NULL); |
mmenke
2014/02/27 23:06:02
DCHECK?
mef
2014/03/03 19:15:13
Done.
|
+ |
+ const char *url_utf8 = env->GetStringUTFChars(url_string, NULL); |
mmenke
2014/02/27 23:06:02
Should be char* url_utf8.
mef
2014/03/03 19:15:13
Done.
|
+ |
+ DVLOG(context->logging_level()) |
+ << "New chromium network request. URL:" << 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); |
mmenke
2014/02/27 23:06:02
DCHECK?
mef
2014/03/03 19:15:13
Done.
|
+ |
+ 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); |
+} |
+ |
+static void SetPostContentType(JNIEnv *env, URLRequestPeer* request, |
+ jstring content_type) { |
mmenke
2014/02/27 23:06:02
Statics up top, -static
mef
2014/03/03 19:15:13
Done.
|
+ CHECK(request != NULL); |
mmenke
2014/02/27 23:06:02
DCHECK?
mef
2014/03/03 19:15:13
Done.
|
+ |
+ 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); |
mmenke
2014/02/27 23:06:02
char* content_...
mef
2014/03/03 19:15:13
Done.
|
+ std::string content_type_string(content_type_utf8); |
+ env->ReleaseStringUTFChars(content_type, content_type_utf8); |
+ |
+ request->AddHeader(content_type_header, content_type_string); |
+} |
+ |
+JNIEXPORT void JNICALL |
+Java_org_chromium_net_UrlRequest_nativeSetPostData( |
+ JNIEnv* env, jobject object, jstring content_type, jbyteArray content) { |
+ URLRequestPeer* request = GetNativeObject(env, object); |
+ SetPostContentType(env, request, content_type); |
+ |
+ 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); |
+ } |
+ } |
+} |
+ |
+JNIEXPORT void JNICALL |
+Java_org_chromium_net_UrlRequest_nativeBeginChunkedUpload( |
+ JNIEnv* env, jobject object, jstring content_type) { |
+ URLRequestPeer* request = GetNativeObject(env, object); |
+ SetPostContentType(env, request, content_type); |
+ |
+ request->EnableStreamingUpload(); |
+} |
+ |
+JNIEXPORT void JNICALL |
+Java_org_chromium_net_UrlRequest_nativeAppendChunk( |
+ JNIEnv* env, jobject object, jobject chunk_byte_buffer, jint chunk_size, |
+ jboolean is_last_chunk) { |
+ URLRequestPeer* request = GetNativeObject(env, object); |
+ CHECK(request != NULL); |
+ |
+ if (chunk_byte_buffer != NULL) { |
+ void* chunk = env->GetDirectBufferAddress(chunk_byte_buffer); |
+ request->AppendChunk(reinterpret_cast<const char*>(chunk), |
+ chunk_size, is_last_chunk); |
+ } |
+} |
+ |
+/* 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: |
mmenke
2014/02/27 23:06:02
case net::OK:
mmenke
2014/02/27 23:06:02
Please add a TODO about investigating returning su
mef
2014/03/03 19:15:13
Done.
mef
2014/03/03 19:15:13
Done.
|
+ 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. |
mmenke
2014/02/27 23:06:02
Add a TODO about investigating this. The fact is
mef
2014/03/03 19:15:13
Done.
|
+ 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(); |
+} |