OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cronet/android/org_chromium_net_UrlRequest.h" | 5 #include "components/cronet/android/org_chromium_net_UrlRequest.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "components/cronet/android/url_request_adapter.h" | 10 #include "components/cronet/android/url_request_adapter.h" |
11 #include "components/cronet/android/url_request_context_adapter.h" | 11 #include "components/cronet/android/url_request_context_adapter.h" |
12 #include "jni/UrlRequest_jni.h" | 12 #include "jni/UrlRequest_jni.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "net/base/request_priority.h" | 14 #include "net/base/request_priority.h" |
15 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
16 | 16 |
17 using base::android::ConvertUTF8ToJavaString; | 17 using base::android::ConvertUTF8ToJavaString; |
| 18 using base::android::CheckException; |
18 | 19 |
19 namespace cronet { | 20 namespace cronet { |
20 namespace { | 21 namespace { |
21 | 22 |
22 net::RequestPriority ConvertRequestPriority(jint request_priority) { | 23 net::RequestPriority ConvertRequestPriority(jint request_priority) { |
23 switch (request_priority) { | 24 switch (request_priority) { |
24 case REQUEST_PRIORITY_IDLE: | 25 case REQUEST_PRIORITY_IDLE: |
25 return net::IDLE; | 26 return net::IDLE; |
26 case REQUEST_PRIORITY_LOWEST: | 27 case REQUEST_PRIORITY_LOWEST: |
27 return net::LOWEST; | 28 return net::LOWEST; |
(...skipping 26 matching lines...) Expand all Loading... |
54 } | 55 } |
55 | 56 |
56 // A delegate of URLRequestAdapter that delivers callbacks to the Java layer. | 57 // A delegate of URLRequestAdapter that delivers callbacks to the Java layer. |
57 class JniURLRequestAdapterDelegate | 58 class JniURLRequestAdapterDelegate |
58 : public URLRequestAdapter::URLRequestAdapterDelegate { | 59 : public URLRequestAdapter::URLRequestAdapterDelegate { |
59 public: | 60 public: |
60 JniURLRequestAdapterDelegate(JNIEnv* env, jobject owner) { | 61 JniURLRequestAdapterDelegate(JNIEnv* env, jobject owner) { |
61 owner_ = env->NewGlobalRef(owner); | 62 owner_ = env->NewGlobalRef(owner); |
62 } | 63 } |
63 | 64 |
| 65 virtual void OnAppendChunkCompleted(URLRequestAdapter* request) OVERRIDE { |
| 66 JNIEnv* env = base::android::AttachCurrentThread(); |
| 67 cronet::Java_UrlRequest_onAppendChunkCompleted(env, owner_); |
| 68 } |
| 69 |
64 virtual void OnResponseStarted(URLRequestAdapter* request) OVERRIDE { | 70 virtual void OnResponseStarted(URLRequestAdapter* request) OVERRIDE { |
65 JNIEnv* env = base::android::AttachCurrentThread(); | 71 JNIEnv* env = base::android::AttachCurrentThread(); |
66 cronet::Java_UrlRequest_onResponseStarted(env, owner_); | 72 cronet::Java_UrlRequest_onResponseStarted(env, owner_); |
67 } | 73 } |
68 | 74 |
69 virtual void OnBytesRead(URLRequestAdapter* request) OVERRIDE { | 75 virtual void OnBytesRead(URLRequestAdapter* request) OVERRIDE { |
70 int bytes_read = request->bytes_read(); | 76 int bytes_read = request->bytes_read(); |
71 if (bytes_read != 0) { | 77 if (bytes_read != 0) { |
72 JNIEnv* env = base::android::AttachCurrentThread(); | 78 JNIEnv* env = base::android::AttachCurrentThread(); |
73 base::android::ScopedJavaLocalRef<jobject> java_buffer( | 79 base::android::ScopedJavaLocalRef<jobject> java_buffer( |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 jlong urlRequestAdapter, | 195 jlong urlRequestAdapter, |
190 jstring content_type, | 196 jstring content_type, |
191 jlong content_length) { | 197 jlong content_length) { |
192 URLRequestAdapter* request = | 198 URLRequestAdapter* request = |
193 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 199 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); |
194 SetPostContentType(env, request, content_type); | 200 SetPostContentType(env, request, content_type); |
195 | 201 |
196 request->SetUploadChannel(env, content_length); | 202 request->SetUploadChannel(env, content_length); |
197 } | 203 } |
198 | 204 |
| 205 static void EnableChunkedUpload(JNIEnv* env, |
| 206 jobject object, |
| 207 jlong urlRequestAdapter, |
| 208 jstring content_type) { |
| 209 URLRequestAdapter* request = |
| 210 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); |
| 211 SetPostContentType(env, request, content_type); |
| 212 |
| 213 request->EnableChunkedUpload(); |
| 214 } |
| 215 |
| 216 static void AppendChunk(JNIEnv* env, |
| 217 jobject object, |
| 218 jlong urlRequestAdapter, |
| 219 jobject chunk_byte_buffer, |
| 220 jint chunk_size, |
| 221 jboolean is_last_chunk) { |
| 222 URLRequestAdapter* request = |
| 223 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); |
| 224 CHECK(request != NULL); |
| 225 CHECK(chunk_byte_buffer != NULL); |
| 226 |
| 227 void* chunk = env->GetDirectBufferAddress(chunk_byte_buffer); |
| 228 request->AppendChunk( |
| 229 reinterpret_cast<const char*>(chunk), chunk_size, is_last_chunk); |
| 230 } |
199 | 231 |
200 /* synchronized */ | 232 /* synchronized */ |
201 static void Start(JNIEnv* env, jobject object, jlong urlRequestAdapter) { | 233 static void Start(JNIEnv* env, jobject object, jlong urlRequestAdapter) { |
202 URLRequestAdapter* request = | 234 URLRequestAdapter* request = |
203 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 235 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); |
204 if (request != NULL) { | 236 if (request != NULL) { |
205 request->Start(); | 237 request->Start(); |
206 } | 238 } |
207 } | 239 } |
208 | 240 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 | 391 |
360 // Some implementations (notably HttpURLConnection) include a mapping for the | 392 // Some implementations (notably HttpURLConnection) include a mapping for the |
361 // null key; in HTTP's case, this maps to the HTTP status line. | 393 // null key; in HTTP's case, this maps to the HTTP status line. |
362 ScopedJavaLocalRef<jstring> status_line = | 394 ScopedJavaLocalRef<jstring> status_line = |
363 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); | 395 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); |
364 Java_UrlRequest_onAppendResponseHeader( | 396 Java_UrlRequest_onAppendResponseHeader( |
365 env, object, headersMap, NULL, status_line.Release()); | 397 env, object, headersMap, NULL, status_line.Release()); |
366 } | 398 } |
367 | 399 |
368 } // namespace cronet | 400 } // namespace cronet |
OLD | NEW |