Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: net/cronet/android/org_chromium_net_UrlRequest.cc

Issue 145213003: Initial upload of cronet for Android. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added cronet_package target to copy cronet.jar and stripped libcronet.so Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "net/cronet/android/org_chromium_net_UrlRequest.h"
6
7 #include <stdio.h>
8
9 #include "net/base/net_errors.h"
10 #include "net/base/request_priority.h"
11 #include "net/cronet/android/android_log.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"
15
16 #define LOG_TAG "ChromiumNetwork"
17
18 static jclass g_class;
19 static jmethodID g_method_finish;
20 static jmethodID g_method_onResponseStarted;
21 static jmethodID g_method_onReadBytes;
22 static jclass g_class_OutputStream;
23 static jmethodID g_method_write;
24 static jfieldID g_request_field;
25
26 static void InitializeJniGlobals(JNIEnv* env) {
27 if (g_class != NULL) {
28 return;
29 }
30
31 g_class = (jclass) env->NewGlobalRef(
32 env->FindClass("org/chromium/net/UrlRequest"));
33 g_method_finish = env->GetMethodID(g_class, "finish", "()V");
34 g_method_onResponseStarted =
35 env->GetMethodID(g_class, "onResponseStarted", "()V");
36 g_method_onReadBytes = env->GetMethodID(g_class, "onBytesRead",
37 "(Ljava/nio/ByteBuffer;)V");
38 g_request_field = env->GetFieldID(g_class, "mRequest", "J");
39
40 g_class_OutputStream = (jclass) env->NewGlobalRef(
41 env->FindClass("java/io/OutputStream"));
42 g_method_write = env->GetMethodID(g_class_OutputStream, "write", "([BII)V");
43 }
44
45 static net::RequestPriority ConvertRequestPriority(jint request_priority) {
46 switch (request_priority) {
47 case REQUEST_PRIORITY_IDLE: return net::IDLE;
48 case REQUEST_PRIORITY_LOWEST: return net::LOWEST;
49 case REQUEST_PRIORITY_LOW: return net::LOW;
50 case REQUEST_PRIORITY_MEDIUM: return net::MEDIUM;
51 case REQUEST_PRIORITY_HIGHEST: return net::HIGHEST;
52 default: return net::LOWEST;
53 }
54 }
55
56 /*
57 * A delegate of URLRequestPeer that delivers callbacks to the Java layer.
58 */
59 class JniURLRequestPeerDelegate :
60 public URLRequestPeer::URLRequestPeerDelegate {
61 jobject owner_;
62 JavaVM* vm_;
63
64 public:
65 JniURLRequestPeerDelegate(JNIEnv* env, jobject owner) {
66 owner_ = env->NewGlobalRef(owner);
67 env->GetJavaVM(&vm_);
68 }
69
70 ~JniURLRequestPeerDelegate() {
71 GetEnv(vm_)->DeleteGlobalRef(owner_);
72 }
73
74 virtual void OnResponseStarted(URLRequestPeer* request) {
75 JNIEnv* env = GetEnv(vm_);
76 env->CallVoidMethod(owner_, g_method_onResponseStarted);
77 if (env->ExceptionOccurred()) {
78 env->ExceptionDescribe();
79 env->ExceptionClear();
80 }
81 }
82
83 virtual void OnBytesRead(URLRequestPeer* request) {
84 int bytes_read = request->BytesRead();
85 if (bytes_read != 0) {
86 JNIEnv* env = GetEnv(vm_);
87 jobject bytebuf = env->NewDirectByteBuffer(request->Data(), bytes_read);
88 env->CallVoidMethod(owner_, g_method_onReadBytes, bytebuf);
89 env->DeleteLocalRef(bytebuf);
90 if (env->ExceptionOccurred()) {
91 env->ExceptionDescribe();
92 env->ExceptionClear();
93 }
94 }
95 }
96
97 virtual void OnRequestFinished(URLRequestPeer* request) {
98 JNIEnv* env = GetEnv(vm_);
99 env->CallVoidMethod(owner_, g_method_finish);
100 if (env->ExceptionOccurred()) {
101 env->ExceptionDescribe();
102 env->ExceptionClear();
103 }
104 }
105 };
106
107 /*
108 * Stores a reference to the request in a java field.
109 */
110 static void SetNativeObject(JNIEnv *env, jobject object,
111 URLRequestPeer* request) {
112 env->SetLongField(object, g_request_field,
113 reinterpret_cast<jlong>(request));
114 }
115
116 /*
117 * Returns a reference to the request, which is stored in a field of
118 * the java object.
119 */
120 static URLRequestPeer* GetNativeObject(JNIEnv* env, jobject object) {
121 return reinterpret_cast<URLRequestPeer *>(
122 env->GetLongField(object, g_request_field));
123 }
124
125 JNIEXPORT void JNICALL
126 Java_org_chromium_net_UrlRequest_nativeInit(
127 JNIEnv* env, jobject object, jobject request_context, jstring url_string,
128 jint priority) {
129 InitializeJniGlobals(env);
130
131 URLRequestContextPeer* context =
132 GetURLRequestContextPeer(env, request_context);
133 CHECK(context != NULL);
134
135 const char *url_utf8 = env->GetStringUTFChars(url_string, NULL);
136
137 LOGD(LOG_TAG, context->logging_level(),
138 "New chromium network request. URL: %s", url_utf8);
139
140 GURL url(url_utf8);
141
142 env->ReleaseStringUTFChars(url_string, url_utf8);
143
144 URLRequestPeer* request =
145 new URLRequestPeer(context, new JniURLRequestPeerDelegate(env, object),
146 url, ConvertRequestPriority(priority));
147
148 SetNativeObject(env, object, request);
149 }
150
151 /* synchronized */
152 JNIEXPORT void JNICALL
153 Java_org_chromium_net_UrlRequest_nativeAddHeader(
154 JNIEnv* env, jobject object, jstring name, jstring value) {
155 URLRequestPeer* request = GetNativeObject(env, object);
156 CHECK(request != NULL);
157
158 const char *name_utf8 = env->GetStringUTFChars(name, NULL);
159 std::string name_string(name_utf8);
160 env->ReleaseStringUTFChars(name, name_utf8);
161
162 const char *value_utf8 = env->GetStringUTFChars(value, NULL);
163 std::string value_string(value_utf8);
164 env->ReleaseStringUTFChars(value, value_utf8);
165
166 request->AddHeader(name_string, value_string);
167 }
168
169 JNIEXPORT void JNICALL
170 Java_org_chromium_net_UrlRequest_nativeSetPostData(
171 JNIEnv* env, jobject object, jstring content_type, jbyteArray content) {
172 URLRequestPeer* request = GetNativeObject(env, object);
173 CHECK(request != NULL);
174
175 std::string method_post("POST");
176 request->SetMethod(method_post);
177
178 std::string content_type_header("Content-Type");
179
180 const char *content_type_utf8 = env->GetStringUTFChars(content_type, NULL);
181 std::string content_type_string(content_type_utf8);
182 env->ReleaseStringUTFChars(content_type, content_type_utf8);
183
184 request->AddHeader(content_type_header, content_type_string);
185
186 if (content != NULL) {
187 jsize size = env->GetArrayLength(content);
188 if (size > 0) {
189 jbyte* content_bytes = env->GetByteArrayElements(content, NULL);
190 request->SetPostContent(reinterpret_cast<const char*>(content_bytes),
191 size);
192 env->ReleaseByteArrayElements(content, content_bytes, 0);
193 }
194 }
195 }
196
197 /* synchronized */
198 JNIEXPORT void JNICALL
199 Java_org_chromium_net_UrlRequest_nativeStart(
200 JNIEnv* env, jobject object) {
201 URLRequestPeer* request = GetNativeObject(env, object);
202 if (request != NULL) {
203 request->Start();
204 }
205 }
206
207 /* synchronized */
208 JNIEXPORT void JNICALL
209 Java_org_chromium_net_UrlRequest_nativeRecycle(
210 JNIEnv* env, jobject object) {
211 URLRequestPeer* request = GetNativeObject(env, object);
212 if (request != NULL) {
213 request->Destroy();
214 }
215
216 SetNativeObject(env, object, NULL);
217 }
218
219 /* synchronized */
220 JNIEXPORT void JNICALL
221 Java_org_chromium_net_UrlRequest_nativeCancel(
222 JNIEnv* env, jobject object) {
223 URLRequestPeer* request = GetNativeObject(env, object);
224 if (request != NULL) {
225 request->Cancel();
226 }
227 }
228
229 JNIEXPORT jint JNICALL
230 Java_org_chromium_net_UrlRequest_nativeGetErrorCode(
231 JNIEnv* env, jobject object) {
232 URLRequestPeer* request = GetNativeObject(env, object);
233 int error_code = request->error_code();
234 switch (error_code) {
235 case 0:
236 return ERROR_SUCCESS;
237
238 // Comment from src/content/browser/download/download_resource_handler.cc:
239 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are
240 // allowed since a number of servers in the wild close the connection too
241 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 -
242 // treat downloads as complete in both cases, so we follow their lead.
243 case net::ERR_CONTENT_LENGTH_MISMATCH:
244 case net::ERR_INCOMPLETE_CHUNKED_ENCODING:
245 return ERROR_SUCCESS;
246
247 case net::ERR_INVALID_URL:
248 case net::ERR_DISALLOWED_URL_SCHEME:
249 case net::ERR_UNKNOWN_URL_SCHEME:
250 return ERROR_MALFORMED_URL;
251
252 case net::ERR_CONNECTION_TIMED_OUT:
253 return ERROR_CONNECTION_TIMED_OUT;
254
255 case net::ERR_NAME_NOT_RESOLVED:
256 return ERROR_UNKNOWN_HOST;
257 }
258 return ERROR_UNKNOWN;
259 }
260
261 JNIEXPORT jstring JNICALL
262 Java_org_chromium_net_UrlRequest_nativeGetErrorString(
263 JNIEnv* env, jobject object) {
264 int error_code = GetNativeObject(env, object)->error_code();
265 char buffer[200];
266 snprintf(buffer, sizeof(buffer), "System error: %s(%d)",
267 net::ErrorToString(error_code), error_code);
268 return env->NewStringUTF(buffer);
269 }
270
271 JNIEXPORT jint JNICALL
272 Java_org_chromium_net_UrlRequest_getHttpStatusCode(
273 JNIEnv* env, jobject object) {
274 return GetNativeObject(env, object)->http_status_code();
275 }
276
277 JNIEXPORT jstring JNICALL
278 Java_org_chromium_net_UrlRequest_nativeGetContentType(
279 JNIEnv* env, jobject object) {
280 URLRequestPeer* request = GetNativeObject(env, object);
281 if (request == NULL) {
282 return NULL;
283 }
284 std::string type = request->content_type();
285 if (!type.empty()) {
286 return env->NewStringUTF(type.c_str());
287 } else {
288 return NULL;
289 }
290 }
291
292 JNIEXPORT jlong JNICALL
293 Java_org_chromium_net_UrlRequest_nativeGetContentLength(
294 JNIEnv* env, jobject object) {
295 URLRequestPeer* request = GetNativeObject(env, object);
296 if (request == NULL) {
297 return 0;
298 }
299 return request->content_length();
300 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698