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/chromium_url_request.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/ChromiumUrlRequest_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 | 18 |
19 namespace cronet { | 19 namespace cronet { |
20 namespace { | 20 namespace { |
21 | 21 |
22 net::RequestPriority ConvertRequestPriority(jint request_priority) { | 22 net::RequestPriority ConvertRequestPriority(jint request_priority) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 // A delegate of URLRequestAdapter that delivers callbacks to the Java layer. | 56 // A delegate of URLRequestAdapter that delivers callbacks to the Java layer. |
57 class JniURLRequestAdapterDelegate | 57 class JniURLRequestAdapterDelegate |
58 : public URLRequestAdapter::URLRequestAdapterDelegate { | 58 : public URLRequestAdapter::URLRequestAdapterDelegate { |
59 public: | 59 public: |
60 JniURLRequestAdapterDelegate(JNIEnv* env, jobject owner) { | 60 JniURLRequestAdapterDelegate(JNIEnv* env, jobject owner) { |
61 owner_ = env->NewGlobalRef(owner); | 61 owner_ = env->NewGlobalRef(owner); |
62 } | 62 } |
63 | 63 |
64 virtual void OnResponseStarted(URLRequestAdapter* request) OVERRIDE { | 64 virtual void OnResponseStarted(URLRequestAdapter* request) OVERRIDE { |
65 JNIEnv* env = base::android::AttachCurrentThread(); | 65 JNIEnv* env = base::android::AttachCurrentThread(); |
66 cronet::Java_UrlRequest_onResponseStarted(env, owner_); | 66 cronet::Java_ChromiumUrlRequest_onResponseStarted(env, owner_); |
67 } | 67 } |
68 | 68 |
69 virtual void OnBytesRead(URLRequestAdapter* request) OVERRIDE { | 69 virtual void OnBytesRead(URLRequestAdapter* request) OVERRIDE { |
70 int bytes_read = request->bytes_read(); | 70 int bytes_read = request->bytes_read(); |
71 if (bytes_read != 0) { | 71 if (bytes_read != 0) { |
72 JNIEnv* env = base::android::AttachCurrentThread(); | 72 JNIEnv* env = base::android::AttachCurrentThread(); |
73 base::android::ScopedJavaLocalRef<jobject> java_buffer( | 73 base::android::ScopedJavaLocalRef<jobject> java_buffer( |
74 env, env->NewDirectByteBuffer(request->Data(), bytes_read)); | 74 env, env->NewDirectByteBuffer(request->Data(), bytes_read)); |
75 cronet::Java_UrlRequest_onBytesRead(env, owner_, java_buffer.obj()); | 75 cronet::Java_ChromiumUrlRequest_onBytesRead( |
| 76 env, owner_, java_buffer.obj()); |
76 } | 77 } |
77 } | 78 } |
78 | 79 |
79 virtual void OnRequestFinished(URLRequestAdapter* request) OVERRIDE { | 80 virtual void OnRequestFinished(URLRequestAdapter* request) OVERRIDE { |
80 JNIEnv* env = base::android::AttachCurrentThread(); | 81 JNIEnv* env = base::android::AttachCurrentThread(); |
81 cronet::Java_UrlRequest_finish(env, owner_); | 82 cronet::Java_ChromiumUrlRequest_finish(env, owner_); |
82 } | 83 } |
83 | 84 |
84 virtual int ReadFromUploadChannel(net::IOBuffer* buf, | 85 virtual int ReadFromUploadChannel(net::IOBuffer* buf, |
85 int buf_length) OVERRIDE { | 86 int buf_length) OVERRIDE { |
86 JNIEnv* env = base::android::AttachCurrentThread(); | 87 JNIEnv* env = base::android::AttachCurrentThread(); |
87 base::android::ScopedJavaLocalRef<jobject> java_buffer( | 88 base::android::ScopedJavaLocalRef<jobject> java_buffer( |
88 env, env->NewDirectByteBuffer(buf->data(), buf_length)); | 89 env, env->NewDirectByteBuffer(buf->data(), buf_length)); |
89 jint bytes_read = cronet::Java_UrlRequest_readFromUploadChannel( | 90 jint bytes_read = cronet::Java_ChromiumUrlRequest_readFromUploadChannel( |
90 env, owner_, java_buffer.obj()); | 91 env, owner_, java_buffer.obj()); |
91 return bytes_read; | 92 return bytes_read; |
92 } | 93 } |
93 | 94 |
94 protected: | 95 protected: |
95 virtual ~JniURLRequestAdapterDelegate() { | 96 virtual ~JniURLRequestAdapterDelegate() { |
96 JNIEnv* env = base::android::AttachCurrentThread(); | 97 JNIEnv* env = base::android::AttachCurrentThread(); |
97 env->DeleteGlobalRef(owner_); | 98 env->DeleteGlobalRef(owner_); |
98 } | 99 } |
99 | 100 |
100 private: | 101 private: |
101 jobject owner_; | 102 jobject owner_; |
102 | 103 |
103 DISALLOW_COPY_AND_ASSIGN(JniURLRequestAdapterDelegate); | 104 DISALLOW_COPY_AND_ASSIGN(JniURLRequestAdapterDelegate); |
104 }; | 105 }; |
105 | 106 |
106 } // namespace | 107 } // namespace |
107 | 108 |
108 // Explicitly register static JNI functions. | 109 // Explicitly register static JNI functions. |
109 bool UrlRequestRegisterJni(JNIEnv* env) { return RegisterNativesImpl(env); } | 110 bool ChromiumUrlRequestRegisterJni(JNIEnv* env) { |
| 111 return RegisterNativesImpl(env); |
| 112 } |
110 | 113 |
111 static jlong CreateRequestAdapter(JNIEnv* env, | 114 static jlong CreateRequestAdapter(JNIEnv* env, |
112 jobject object, | 115 jobject object, |
113 jlong urlRequestContextAdapter, | 116 jlong urlRequestContextAdapter, |
114 jstring url_string, | 117 jstring url_string, |
115 jint priority) { | 118 jint priority) { |
116 URLRequestContextAdapter* context = | 119 URLRequestContextAdapter* context = |
117 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); | 120 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); |
118 DCHECK(context != NULL); | 121 DCHECK(context != NULL); |
119 | 122 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 jlong urlRequestAdapter, | 192 jlong urlRequestAdapter, |
190 jstring content_type, | 193 jstring content_type, |
191 jlong content_length) { | 194 jlong content_length) { |
192 URLRequestAdapter* request = | 195 URLRequestAdapter* request = |
193 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 196 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); |
194 SetPostContentType(env, request, content_type); | 197 SetPostContentType(env, request, content_type); |
195 | 198 |
196 request->SetUploadChannel(env, content_length); | 199 request->SetUploadChannel(env, content_length); |
197 } | 200 } |
198 | 201 |
199 | |
200 /* synchronized */ | 202 /* synchronized */ |
201 static void Start(JNIEnv* env, jobject object, jlong urlRequestAdapter) { | 203 static void Start(JNIEnv* env, jobject object, jlong urlRequestAdapter) { |
202 URLRequestAdapter* request = | 204 URLRequestAdapter* request = |
203 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 205 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); |
204 if (request != NULL) { | 206 if (request != NULL) { |
205 request->Start(); | 207 request->Start(); |
206 } | 208 } |
207 } | 209 } |
208 | 210 |
209 /* synchronized */ | 211 /* synchronized */ |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 return; | 348 return; |
347 | 349 |
348 void* iter = NULL; | 350 void* iter = NULL; |
349 std::string header_name; | 351 std::string header_name; |
350 std::string header_value; | 352 std::string header_value; |
351 while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) { | 353 while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) { |
352 ScopedJavaLocalRef<jstring> name = | 354 ScopedJavaLocalRef<jstring> name = |
353 ConvertUTF8ToJavaString(env, header_name); | 355 ConvertUTF8ToJavaString(env, header_name); |
354 ScopedJavaLocalRef<jstring> value = | 356 ScopedJavaLocalRef<jstring> value = |
355 ConvertUTF8ToJavaString(env, header_value); | 357 ConvertUTF8ToJavaString(env, header_value); |
356 Java_UrlRequest_onAppendResponseHeader( | 358 Java_ChromiumUrlRequest_onAppendResponseHeader( |
357 env, object, headersMap, name.Release(), value.Release()); | 359 env, object, headersMap, name.Release(), value.Release()); |
358 } | 360 } |
359 | 361 |
360 // Some implementations (notably HttpURLConnection) include a mapping for the | 362 // Some implementations (notably HttpURLConnection) include a mapping for the |
361 // null key; in HTTP's case, this maps to the HTTP status line. | 363 // null key; in HTTP's case, this maps to the HTTP status line. |
362 ScopedJavaLocalRef<jstring> status_line = | 364 ScopedJavaLocalRef<jstring> status_line = |
363 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); | 365 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); |
364 Java_UrlRequest_onAppendResponseHeader( | 366 Java_ChromiumUrlRequest_onAppendResponseHeader( |
365 env, object, headersMap, NULL, status_line.Release()); | 367 env, object, headersMap, NULL, status_line.Release()); |
366 } | 368 } |
367 | 369 |
368 } // namespace cronet | 370 } // namespace cronet |
OLD | NEW |