Chromium Code Reviews| 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/chromium_url_request.h" | 5 #include "components/cronet/android/cronet_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/cronet_url_request_adapter.h" |
| 11 #include "components/cronet/android/url_request_context_adapter.h" | 11 #include "components/cronet/android/cronet_url_request_context_adapter.h" |
| 12 #include "jni/ChromiumUrlRequest_jni.h" | 12 #include "jni/CronetUrlRequest_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) { |
| 23 switch (request_priority) { | 23 switch (request_priority) { |
| 24 case REQUEST_PRIORITY_IDLE: | 24 case CRONET_REQUEST_PRIORITY_IDLE: |
| 25 return net::IDLE; | 25 return net::IDLE; |
| 26 case REQUEST_PRIORITY_LOWEST: | 26 case CRONET_REQUEST_PRIORITY_LOWEST: |
| 27 return net::LOWEST; | 27 return net::LOWEST; |
| 28 case REQUEST_PRIORITY_LOW: | 28 case CRONET_REQUEST_PRIORITY_LOW: |
| 29 return net::LOW; | 29 return net::LOW; |
| 30 case REQUEST_PRIORITY_MEDIUM: | 30 case CRONET_REQUEST_PRIORITY_MEDIUM: |
| 31 return net::MEDIUM; | 31 return net::MEDIUM; |
| 32 case REQUEST_PRIORITY_HIGHEST: | 32 case CRONET_REQUEST_PRIORITY_HIGHEST: |
| 33 return net::HIGHEST; | 33 return net::HIGHEST; |
| 34 default: | 34 default: |
| 35 return net::LOWEST; | 35 return net::LOWEST; |
| 36 } | 36 } |
| 37 } | 37 } |
| 38 | 38 // A delegate of CronetURLRequestAdapter that delivers callbacks to the Java |
| 39 void SetPostContentType(JNIEnv* env, | 39 // layer. |
| 40 URLRequestAdapter* request, | 40 class JniCronetURLRequestAdapterDelegate |
| 41 jstring content_type) { | 41 : public CronetURLRequestAdapter::CronetURLRequestAdapterDelegate { |
| 42 DCHECK(request != NULL); | |
| 43 | |
| 44 std::string method_post("POST"); | |
| 45 request->SetMethod(method_post); | |
| 46 | |
| 47 std::string content_type_header("Content-Type"); | |
| 48 std::string content_type_string( | |
| 49 base::android::ConvertJavaStringToUTF8(env, content_type)); | |
| 50 | |
| 51 request->AddHeader(content_type_header, content_type_string); | |
| 52 } | |
| 53 | |
| 54 // A delegate of URLRequestAdapter that delivers callbacks to the Java layer. | |
| 55 class JniURLRequestAdapterDelegate | |
| 56 : public URLRequestAdapter::URLRequestAdapterDelegate { | |
| 57 public: | 42 public: |
| 58 JniURLRequestAdapterDelegate(JNIEnv* env, jobject owner) { | 43 JniCronetURLRequestAdapterDelegate(JNIEnv* env, jobject owner) { |
| 59 owner_ = env->NewGlobalRef(owner); | 44 owner_ = env->NewGlobalRef(owner); |
| 60 } | 45 } |
| 61 | 46 |
| 62 virtual void OnResponseStarted(URLRequestAdapter* request) OVERRIDE { | 47 virtual void OnRedirect(CronetURLRequestAdapter* request, |
| 48 const GURL& newLocation) OVERRIDE { | |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); | 49 JNIEnv* env = base::android::AttachCurrentThread(); |
| 64 cronet::Java_ChromiumUrlRequest_onResponseStarted(env, owner_); | 50 cronet::Java_CronetUrlRequest_onRedirect( |
| 51 env, | |
| 52 owner_, | |
| 53 ConvertUTF8ToJavaString(env, newLocation.spec()).Release()); | |
| 65 } | 54 } |
| 66 | 55 |
| 67 virtual void OnBytesRead(URLRequestAdapter* request) OVERRIDE { | 56 virtual void OnResponseStarted(CronetURLRequestAdapter* request) OVERRIDE { |
| 68 int bytes_read = request->bytes_read(); | 57 JNIEnv* env = base::android::AttachCurrentThread(); |
| 69 if (bytes_read != 0) { | 58 cronet::Java_CronetUrlRequest_onResponseStarted(env, owner_); |
|
mmenke
2014/10/02 15:24:09
Should check if request->status().is_success() is
mef
2014/10/02 22:07:42
Per discussion moved status checks to cronet_url_r
| |
| 70 JNIEnv* env = base::android::AttachCurrentThread(); | 59 } |
| 60 | |
| 61 virtual void OnBytesRead(CronetURLRequestAdapter* request, | |
| 62 int bytes_read) OVERRIDE { | |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 64 if (bytes_read >= 0) { | |
|
mmenke
2014/10/02 15:24:09
"&& request_->status().is_success()"
mef
2014/10/02 22:07:42
Done.
| |
| 71 base::android::ScopedJavaLocalRef<jobject> java_buffer( | 65 base::android::ScopedJavaLocalRef<jobject> java_buffer( |
| 72 env, env->NewDirectByteBuffer(request->Data(), bytes_read)); | 66 env, env->NewDirectByteBuffer(request->Data(), bytes_read)); |
| 73 cronet::Java_ChromiumUrlRequest_onBytesRead( | 67 cronet::Java_CronetUrlRequest_onDataReceived( |
| 74 env, owner_, java_buffer.obj()); | 68 env, owner_, java_buffer.obj()); |
| 69 } else { | |
| 70 cronet::Java_CronetUrlRequest_onError(env, owner_, request->error_code()); | |
| 75 } | 71 } |
| 76 } | 72 } |
| 77 | 73 |
| 78 virtual void OnRequestFinished(URLRequestAdapter* request) OVERRIDE { | 74 virtual void OnRequestFinished(CronetURLRequestAdapter* request, |
| 75 bool canceled) OVERRIDE { | |
| 79 JNIEnv* env = base::android::AttachCurrentThread(); | 76 JNIEnv* env = base::android::AttachCurrentThread(); |
| 80 cronet::Java_ChromiumUrlRequest_finish(env, owner_); | 77 cronet::Java_CronetUrlRequest_onComplete(env, owner_, |
| 81 } | 78 canceled ? JNI_TRUE : JNI_FALSE ); |
| 82 | |
| 83 virtual int ReadFromUploadChannel(net::IOBuffer* buf, | |
| 84 int buf_length) OVERRIDE { | |
| 85 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 86 base::android::ScopedJavaLocalRef<jobject> java_buffer( | |
| 87 env, env->NewDirectByteBuffer(buf->data(), buf_length)); | |
| 88 jint bytes_read = cronet::Java_ChromiumUrlRequest_readFromUploadChannel( | |
| 89 env, owner_, java_buffer.obj()); | |
| 90 return bytes_read; | |
| 91 } | 79 } |
| 92 | 80 |
| 93 protected: | 81 protected: |
| 94 virtual ~JniURLRequestAdapterDelegate() { | 82 virtual ~JniCronetURLRequestAdapterDelegate() { |
| 95 JNIEnv* env = base::android::AttachCurrentThread(); | 83 JNIEnv* env = base::android::AttachCurrentThread(); |
| 96 env->DeleteGlobalRef(owner_); | 84 env->DeleteGlobalRef(owner_); |
| 97 } | 85 } |
| 98 | 86 |
| 99 private: | 87 private: |
| 100 jobject owner_; | 88 jobject owner_; |
| 101 | 89 |
| 102 DISALLOW_COPY_AND_ASSIGN(JniURLRequestAdapterDelegate); | 90 DISALLOW_COPY_AND_ASSIGN(JniCronetURLRequestAdapterDelegate); |
| 103 }; | 91 }; |
| 104 | 92 |
| 105 } // namespace | 93 } // namespace |
| 106 | 94 |
| 107 // Explicitly register static JNI functions. | 95 // Explicitly register static JNI functions. |
| 108 bool ChromiumUrlRequestRegisterJni(JNIEnv* env) { | 96 bool CronetUrlRequestRegisterJni(JNIEnv* env) { |
| 109 return RegisterNativesImpl(env); | 97 return RegisterNativesImpl(env); |
| 110 } | 98 } |
| 111 | 99 |
| 112 static jlong CreateRequestAdapter(JNIEnv* env, | 100 static jlong CreateRequestAdapter(JNIEnv* env, |
| 113 jobject object, | 101 jobject object, |
| 114 jlong urlRequestContextAdapter, | 102 jlong urlRequestContextAdapter, |
|
mmenke
2014/10/02 15:24:09
We should probably be using C++ style guide style
mef
2014/10/02 22:07:42
Done.
| |
| 115 jstring url_string, | 103 jstring url_string, |
| 116 jint priority) { | 104 jint priority) { |
| 117 URLRequestContextAdapter* context = | 105 CronetURLRequestContextAdapter* context = |
| 118 reinterpret_cast<URLRequestContextAdapter*>(urlRequestContextAdapter); | 106 reinterpret_cast<CronetURLRequestContextAdapter*>( |
| 107 urlRequestContextAdapter); | |
| 119 DCHECK(context != NULL); | 108 DCHECK(context != NULL); |
| 120 | 109 |
| 121 GURL url(base::android::ConvertJavaStringToUTF8(env, url_string)); | 110 GURL url(base::android::ConvertJavaStringToUTF8(env, url_string)); |
| 122 | 111 |
| 123 VLOG(1) << "New chromium network request: " << url.possibly_invalid_spec(); | 112 VLOG(1) << "New chromium network request: " << url.possibly_invalid_spec(); |
| 124 | 113 |
| 125 URLRequestAdapter* adapter = | 114 CronetURLRequestAdapter* adapter = new CronetURLRequestAdapter( |
| 126 new URLRequestAdapter(context, | 115 context, |
| 127 new JniURLRequestAdapterDelegate(env, object), | 116 new JniCronetURLRequestAdapterDelegate(env, object), |
| 128 url, | 117 url, |
| 129 ConvertRequestPriority(priority)); | 118 ConvertRequestPriority(priority)); |
| 130 | 119 |
| 131 return reinterpret_cast<jlong>(adapter); | 120 return reinterpret_cast<jlong>(adapter); |
| 132 } | 121 } |
| 133 | 122 |
| 134 // synchronized | 123 // synchronized |
| 135 static void AddHeader(JNIEnv* env, | 124 static void AddHeader(JNIEnv* env, |
| 136 jobject object, | 125 jobject object, |
| 137 jlong urlRequestAdapter, | 126 jlong urlRequestAdapter, |
| 138 jstring name, | 127 jstring name, |
| 139 jstring value) { | 128 jstring value) { |
| 140 URLRequestAdapter* request = | 129 CronetURLRequestAdapter* request = |
| 141 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 130 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 142 DCHECK(request); | 131 DCHECK(request); |
| 143 | 132 |
| 144 std::string name_string(base::android::ConvertJavaStringToUTF8(env, name)); | 133 std::string name_string(base::android::ConvertJavaStringToUTF8(env, name)); |
| 145 std::string value_string(base::android::ConvertJavaStringToUTF8(env, value)); | 134 std::string value_string(base::android::ConvertJavaStringToUTF8(env, value)); |
| 146 | 135 |
| 147 request->AddHeader(name_string, value_string); | 136 request->AddHeader(name_string, value_string); |
| 148 } | 137 } |
| 149 | 138 |
| 150 static void SetMethod(JNIEnv* env, | 139 static void SetHttpMethod(JNIEnv* env, |
| 151 jobject object, | 140 jobject object, |
| 152 jlong urlRequestAdapter, | 141 jlong urlRequestAdapter, |
| 153 jstring method) { | 142 jstring method) { |
| 154 URLRequestAdapter* request = | 143 CronetURLRequestAdapter* request = |
| 155 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 144 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 156 DCHECK(request); | 145 DCHECK(request); |
| 157 | 146 |
| 158 std::string method_string( | 147 std::string method_string( |
| 159 base::android::ConvertJavaStringToUTF8(env, method)); | 148 base::android::ConvertJavaStringToUTF8(env, method)); |
| 160 | 149 |
| 161 request->SetMethod(method_string); | 150 request->SetMethod(method_string); |
| 162 } | 151 } |
| 163 | 152 |
| 164 static void SetUploadData(JNIEnv* env, | |
| 165 jobject object, | |
| 166 jlong urlRequestAdapter, | |
| 167 jstring content_type, | |
| 168 jbyteArray content) { | |
| 169 URLRequestAdapter* request = | |
| 170 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | |
| 171 SetPostContentType(env, request, content_type); | |
| 172 | |
| 173 if (content != NULL) { | |
| 174 jsize size = env->GetArrayLength(content); | |
| 175 if (size > 0) { | |
| 176 jbyte* content_bytes = env->GetByteArrayElements(content, NULL); | |
| 177 request->SetUploadContent(reinterpret_cast<const char*>(content_bytes), | |
| 178 size); | |
| 179 env->ReleaseByteArrayElements(content, content_bytes, 0); | |
| 180 } | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 static void SetUploadChannel(JNIEnv* env, | |
| 185 jobject object, | |
| 186 jlong urlRequestAdapter, | |
| 187 jstring content_type, | |
| 188 jlong content_length) { | |
| 189 URLRequestAdapter* request = | |
| 190 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | |
| 191 SetPostContentType(env, request, content_type); | |
| 192 | |
| 193 request->SetUploadChannel(env, content_length); | |
| 194 } | |
| 195 | |
| 196 static void EnableChunkedUpload(JNIEnv* env, | |
| 197 jobject object, | |
| 198 jlong urlRequestAdapter, | |
| 199 jstring content_type) { | |
| 200 URLRequestAdapter* request = | |
| 201 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | |
| 202 SetPostContentType(env, request, content_type); | |
| 203 | |
| 204 request->EnableChunkedUpload(); | |
| 205 } | |
| 206 | |
| 207 static void AppendChunk(JNIEnv* env, | |
| 208 jobject object, | |
| 209 jlong urlRequestAdapter, | |
| 210 jobject chunk_byte_buffer, | |
| 211 jint chunk_size, | |
| 212 jboolean is_last_chunk) { | |
| 213 URLRequestAdapter* request = | |
| 214 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | |
| 215 DCHECK(chunk_byte_buffer != NULL); | |
| 216 | |
| 217 void* chunk = env->GetDirectBufferAddress(chunk_byte_buffer); | |
| 218 request->AppendChunk( | |
| 219 reinterpret_cast<const char*>(chunk), chunk_size, is_last_chunk); | |
| 220 } | |
| 221 | |
| 222 /* synchronized */ | 153 /* synchronized */ |
|
mmenke
2014/10/02 15:24:09
None of these are synchronized, are they?
mef
2014/10/02 22:07:42
Done.
| |
| 223 static void Start(JNIEnv* env, jobject object, jlong urlRequestAdapter) { | 154 static void Start(JNIEnv* env, jobject object, jlong urlRequestAdapter) { |
| 224 URLRequestAdapter* request = | 155 CronetURLRequestAdapter* request = |
| 225 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 156 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 226 if (request != NULL) { | 157 if (request != NULL) { |
| 227 request->Start(); | 158 request->Start(); |
| 228 } | 159 } |
| 229 } | 160 } |
| 230 | 161 |
| 231 /* synchronized */ | 162 /* synchronized */ |
| 232 static void DestroyRequestAdapter(JNIEnv* env, | 163 static void DestroyRequestAdapter(JNIEnv* env, |
| 233 jobject object, | 164 jobject object, |
| 234 jlong urlRequestAdapter) { | 165 jlong urlRequestAdapter) { |
| 235 URLRequestAdapter* request = | 166 CronetURLRequestAdapter* request = |
| 236 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 167 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 237 if (request != NULL) { | 168 if (request != NULL) { |
| 238 request->Destroy(); | 169 request->Destroy(); |
| 239 } | 170 } |
| 240 } | 171 } |
| 241 | 172 |
| 242 /* synchronized */ | 173 /* synchronized */ |
| 243 static void Cancel(JNIEnv* env, jobject object, jlong urlRequestAdapter) { | 174 static void Cancel(JNIEnv* env, jobject object, jlong urlRequestAdapter) { |
| 244 URLRequestAdapter* request = | 175 CronetURLRequestAdapter* request = |
| 245 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 176 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 246 if (request != NULL) { | 177 if (request != NULL) { |
| 247 request->Cancel(); | 178 request->Cancel(); |
| 248 } | 179 } |
| 249 } | 180 } |
| 250 | 181 |
| 182 static void ReceiveData(JNIEnv* env, jobject jcaller, jlong urlRequestAdapter) { | |
| 183 CronetURLRequestAdapter* request = | |
| 184 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); | |
| 185 if (request != NULL) { | |
| 186 request->ReadData(); | |
| 187 } | |
|
mmenke
2014/10/02 15:24:08
nit: --braces. (Goes for all the one-line if sta
mef
2014/10/02 22:07:42
Done.
| |
| 188 } | |
| 189 | |
| 190 static void FollowDeferredRedirect(JNIEnv* env, | |
| 191 jobject jcaller, | |
| 192 jlong urlRequestAdapter) { | |
| 193 CronetURLRequestAdapter* request = | |
| 194 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); | |
| 195 if (request != NULL) { | |
| 196 request->FollowDeferredRedirect(); | |
| 197 } | |
| 198 } | |
| 199 | |
| 251 static jint GetErrorCode(JNIEnv* env, jobject object, jlong urlRequestAdapter) { | 200 static jint GetErrorCode(JNIEnv* env, jobject object, jlong urlRequestAdapter) { |
| 252 URLRequestAdapter* request = | 201 CronetURLRequestAdapter* request = |
| 253 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 202 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 254 int error_code = request->error_code(); | 203 int error_code = request->error_code(); |
| 255 switch (error_code) { | 204 switch (error_code) { |
| 256 // TODO(mef): Investigate returning success on positive values, too, as | 205 // TODO(mef): Investigate returning success on positive values, too, as |
| 257 // they technically indicate success. | 206 // they technically indicate success. |
| 258 case net::OK: | 207 case net::OK: |
| 259 return REQUEST_ERROR_SUCCESS; | 208 return CRONET_REQUEST_ERROR_SUCCESS; |
| 260 | 209 |
| 261 // TODO(mef): Investigate this. The fact is that Chrome does not do this, | 210 // TODO(mef): Investigate this. The fact is that Chrome does not do this, |
| 262 // and this library is not just being used for downloads. | 211 // and this library is not just being used for downloads. |
| 263 | 212 |
| 264 // Comment from src/content/browser/download/download_resource_handler.cc: | 213 // Comment from src/content/browser/download/download_resource_handler.cc: |
| 265 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are | 214 // ERR_CONTENT_LENGTH_MISMATCH and ERR_INCOMPLETE_CHUNKED_ENCODING are |
| 266 // allowed since a number of servers in the wild close the connection too | 215 // allowed since a number of servers in the wild close the connection too |
| 267 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 - | 216 // early by mistake. Other browsers - IE9, Firefox 11.0, and Safari 5.1.4 - |
| 268 // treat downloads as complete in both cases, so we follow their lead. | 217 // treat downloads as complete in both cases, so we follow their lead. |
| 269 case net::ERR_CONTENT_LENGTH_MISMATCH: | 218 case net::ERR_CONTENT_LENGTH_MISMATCH: |
| 270 case net::ERR_INCOMPLETE_CHUNKED_ENCODING: | 219 case net::ERR_INCOMPLETE_CHUNKED_ENCODING: |
| 271 return REQUEST_ERROR_SUCCESS; | 220 return CRONET_REQUEST_ERROR_SUCCESS; |
| 272 | 221 |
| 273 case net::ERR_INVALID_URL: | 222 case net::ERR_INVALID_URL: |
| 274 case net::ERR_DISALLOWED_URL_SCHEME: | 223 case net::ERR_DISALLOWED_URL_SCHEME: |
| 275 case net::ERR_UNKNOWN_URL_SCHEME: | 224 case net::ERR_UNKNOWN_URL_SCHEME: |
| 276 return REQUEST_ERROR_MALFORMED_URL; | 225 return CRONET_REQUEST_ERROR_MALFORMED_URL; |
| 277 | 226 |
| 278 case net::ERR_CONNECTION_TIMED_OUT: | 227 case net::ERR_CONNECTION_TIMED_OUT: |
| 279 return REQUEST_ERROR_CONNECTION_TIMED_OUT; | 228 return CRONET_REQUEST_ERROR_CONNECTION_TIMED_OUT; |
| 280 | 229 |
| 281 case net::ERR_NAME_NOT_RESOLVED: | 230 case net::ERR_NAME_NOT_RESOLVED: |
| 282 return REQUEST_ERROR_UNKNOWN_HOST; | 231 return CRONET_REQUEST_ERROR_UNKNOWN_HOST; |
| 283 } | 232 } |
| 284 return REQUEST_ERROR_UNKNOWN; | 233 return CRONET_REQUEST_ERROR_UNKNOWN; |
| 285 } | 234 } |
| 286 | 235 |
| 287 static jstring GetErrorString(JNIEnv* env, | 236 static jstring GetErrorString(JNIEnv* env, |
| 288 jobject object, | 237 jobject object, |
| 289 jlong urlRequestAdapter) { | 238 jlong urlRequestAdapter) { |
| 290 URLRequestAdapter* request = | 239 CronetURLRequestAdapter* request = |
| 291 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 240 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 292 int error_code = request->error_code(); | 241 int error_code = request->error_code(); |
| 293 char buffer[200]; | 242 char buffer[200]; |
| 294 std::string error_string = net::ErrorToString(error_code); | 243 std::string error_string = net::ErrorToString(error_code); |
| 295 snprintf(buffer, | 244 snprintf(buffer, |
| 296 sizeof(buffer), | 245 sizeof(buffer), |
| 297 "System error: %s(%d)", | 246 "System error: %s(%d)", |
| 298 error_string.c_str(), | 247 error_string.c_str(), |
| 299 error_code); | 248 error_code); |
| 300 return ConvertUTF8ToJavaString(env, buffer).Release(); | 249 return ConvertUTF8ToJavaString(env, buffer).Release(); |
| 301 } | 250 } |
| 302 | 251 |
| 303 static jint GetHttpStatusCode(JNIEnv* env, | 252 static jint GetHttpStatusCode(JNIEnv* env, |
| 304 jobject object, | 253 jobject object, |
| 305 jlong urlRequestAdapter) { | 254 jlong urlRequestAdapter) { |
| 306 URLRequestAdapter* request = | 255 CronetURLRequestAdapter* request = |
| 307 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 256 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 308 return request->http_status_code(); | 257 return request->http_status_code(); |
| 309 } | 258 } |
| 310 | 259 |
| 311 static jstring GetContentType(JNIEnv* env, | 260 static jstring GetContentType(JNIEnv* env, |
| 312 jobject object, | 261 jobject object, |
| 313 jlong urlRequestAdapter) { | 262 jlong urlRequestAdapter) { |
| 314 URLRequestAdapter* request = | 263 CronetURLRequestAdapter* request = |
| 315 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 264 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 316 if (request == NULL) { | 265 if (request == NULL) { |
| 317 return NULL; | 266 return NULL; |
| 318 } | 267 } |
| 319 std::string type = request->content_type(); | 268 std::string type = request->content_type(); |
| 320 if (!type.empty()) { | 269 if (!type.empty()) { |
| 321 return ConvertUTF8ToJavaString(env, type.c_str()).Release(); | 270 return ConvertUTF8ToJavaString(env, type.c_str()).Release(); |
| 322 } else { | 271 } else { |
| 323 return NULL; | 272 return NULL; |
| 324 } | 273 } |
| 325 } | 274 } |
| 326 | 275 |
| 327 static jlong GetContentLength(JNIEnv* env, | 276 static jlong GetContentLength(JNIEnv* env, |
| 328 jobject object, | 277 jobject object, |
| 329 jlong urlRequestAdapter) { | 278 jlong urlRequestAdapter) { |
| 330 URLRequestAdapter* request = | 279 CronetURLRequestAdapter* request = |
| 331 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 280 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 332 if (request == NULL) { | 281 if (request == NULL) { |
| 333 return 0; | 282 return 0; |
| 334 } | 283 } |
| 335 return request->content_length(); | 284 return request->content_length(); |
| 336 } | 285 } |
| 337 | 286 |
| 338 static jstring GetHeader(JNIEnv* env, | 287 static jstring GetHeader(JNIEnv* env, |
| 339 jobject object, | 288 jobject object, |
| 340 jlong urlRequestAdapter, | 289 jlong urlRequestAdapter, |
| 341 jstring name) { | 290 jstring name) { |
| 342 URLRequestAdapter* request = | 291 CronetURLRequestAdapter* request = |
| 343 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 292 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 344 if (request == NULL) { | 293 if (request == NULL) { |
| 345 return NULL; | 294 return NULL; |
| 346 } | 295 } |
| 347 | 296 |
| 348 std::string name_string = base::android::ConvertJavaStringToUTF8(env, name); | 297 std::string name_string = base::android::ConvertJavaStringToUTF8(env, name); |
| 349 std::string value = request->GetHeader(name_string); | 298 std::string value = request->GetHeader(name_string); |
| 350 if (!value.empty()) { | 299 if (!value.empty()) { |
| 351 return ConvertUTF8ToJavaString(env, value.c_str()).Release(); | 300 return ConvertUTF8ToJavaString(env, value.c_str()).Release(); |
| 352 } else { | 301 } else { |
| 353 return NULL; | 302 return NULL; |
| 354 } | 303 } |
| 355 } | 304 } |
| 356 | 305 |
| 357 static void GetAllHeaders(JNIEnv* env, | 306 static void GetAllHeaders(JNIEnv* env, |
| 358 jobject object, | 307 jobject object, |
| 359 jlong urlRequestAdapter, | 308 jlong urlRequestAdapter, |
| 360 jobject headersMap) { | 309 jobject headersMap) { |
| 361 URLRequestAdapter* request = | 310 CronetURLRequestAdapter* request = |
| 362 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 311 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 363 if (request == NULL) | 312 if (request == NULL) |
| 364 return; | 313 return; |
| 365 | 314 |
| 366 net::HttpResponseHeaders* headers = request->GetResponseHeaders(); | 315 net::HttpResponseHeaders* headers = request->GetResponseHeaders(); |
| 367 if (headers == NULL) | 316 if (headers == NULL) |
| 368 return; | 317 return; |
| 369 | 318 |
| 370 void* iter = NULL; | 319 void* iter = NULL; |
| 371 std::string header_name; | 320 std::string header_name; |
| 372 std::string header_value; | 321 std::string header_value; |
| 373 while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) { | 322 while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) { |
| 374 ScopedJavaLocalRef<jstring> name = | 323 ScopedJavaLocalRef<jstring> name = |
| 375 ConvertUTF8ToJavaString(env, header_name); | 324 ConvertUTF8ToJavaString(env, header_name); |
| 376 ScopedJavaLocalRef<jstring> value = | 325 ScopedJavaLocalRef<jstring> value = |
| 377 ConvertUTF8ToJavaString(env, header_value); | 326 ConvertUTF8ToJavaString(env, header_value); |
| 378 Java_ChromiumUrlRequest_onAppendResponseHeader( | 327 Java_CronetUrlRequest_onAppendResponseHeader( |
| 379 env, object, headersMap, name.Release(), value.Release()); | 328 env, object, headersMap, name.Release(), value.Release()); |
| 380 } | 329 } |
| 381 | 330 |
| 382 // Some implementations (notably HttpURLConnection) include a mapping for the | 331 // Some implementations (notably HttpURLConnection) include a mapping for the |
| 383 // null key; in HTTP's case, this maps to the HTTP status line. | 332 // null key; in HTTP's case, this maps to the HTTP status line. |
| 384 ScopedJavaLocalRef<jstring> status_line = | 333 ScopedJavaLocalRef<jstring> status_line = |
| 385 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); | 334 ConvertUTF8ToJavaString(env, headers->GetStatusLine()); |
| 386 Java_ChromiumUrlRequest_onAppendResponseHeader( | 335 Java_CronetUrlRequest_onAppendResponseHeader( |
| 387 env, object, headersMap, NULL, status_line.Release()); | 336 env, object, headersMap, NULL, status_line.Release()); |
| 388 } | 337 } |
| 389 | 338 |
| 390 static jstring GetNegotiatedProtocol(JNIEnv* env, | 339 static jstring GetNegotiatedProtocol(JNIEnv* env, |
| 391 jobject object, | 340 jobject object, |
| 392 jlong urlRequestAdapter) { | 341 jlong urlRequestAdapter) { |
| 393 URLRequestAdapter* request = | 342 CronetURLRequestAdapter* request = |
| 394 reinterpret_cast<URLRequestAdapter*>(urlRequestAdapter); | 343 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); |
| 395 if (request == NULL) | 344 if (request == NULL) |
| 396 return ConvertUTF8ToJavaString(env, "").Release(); | 345 return ConvertUTF8ToJavaString(env, "").Release(); |
| 397 | 346 |
| 398 std::string negotiated_protocol = request->GetNegotiatedProtocol(); | 347 std::string negotiated_protocol = request->GetNegotiatedProtocol(); |
| 399 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()).Release(); | 348 return ConvertUTF8ToJavaString(env, negotiated_protocol.c_str()).Release(); |
| 400 } | 349 } |
| 401 | 350 |
| 351 static jboolean GetWasCached(JNIEnv* env, | |
| 352 jobject jcaller, | |
| 353 jlong urlRequestAdapter) { | |
| 354 CronetURLRequestAdapter* request = | |
| 355 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); | |
| 356 if (request == NULL) | |
| 357 return JNI_FALSE; | |
| 358 | |
| 359 return request->GetWasCached() ? JNI_TRUE : JNI_FALSE; | |
| 360 } | |
| 361 | |
| 362 static jlong GetTotalReceivedBytes(JNIEnv* env, | |
| 363 jobject jcaller, | |
| 364 jlong urlRequestAdapter) { | |
| 365 CronetURLRequestAdapter* request = | |
| 366 reinterpret_cast<CronetURLRequestAdapter*>(urlRequestAdapter); | |
| 367 if (request == NULL) { | |
| 368 return 0; | |
| 369 } | |
| 370 return request->GetTotalReceivedBytes(); | |
| 371 } | |
| 372 | |
| 402 } // namespace cronet | 373 } // namespace cronet |
| OLD | NEW |