Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "android_webview/native/input_stream_impl.h" | 5 #include "android_webview/native/input_stream_impl.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 // Disable "Warnings treated as errors" for input_stream_jni as it's a Java | 8 // Disable "Warnings treated as errors" for input_stream_jni as it's a Java |
| 9 // system class and we have to generate C++ hooks for all methods in the class | 9 // system class and we have to generate C++ hooks for all methods in the class |
| 10 // even if they're unused. | 10 // even if they're unused. |
| 11 #pragma GCC diagnostic push | 11 #pragma GCC diagnostic push |
| 12 #pragma GCC diagnostic ignored "-Wunused-function" | 12 #pragma GCC diagnostic ignored "-Wunused-function" |
| 13 #include "jni/InputStream_jni.h" | 13 #include "jni/InputStream_jni.h" |
| 14 #pragma GCC diagnostic pop | 14 #pragma GCC diagnostic pop |
| 15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 16 | 16 |
| 17 using base::android::AttachCurrentThread; | 17 using base::android::AttachCurrentThread; |
| 18 using base::android::ClearException; | 18 using base::android::ClearException; |
| 19 using base::android::JavaRef; | 19 using base::android::JavaRef; |
| 20 using JNI_InputStream::Java_InputStream_available; | 20 using JNI_InputStream::Java_InputStream_available; |
| 21 using JNI_InputStream::Java_InputStream_close; | |
| 21 using JNI_InputStream::Java_InputStream_skip; | 22 using JNI_InputStream::Java_InputStream_skip; |
| 22 using JNI_InputStream::Java_InputStream_readI_AB_I_I; | 23 using JNI_InputStream::Java_InputStream_readI_AB_I_I; |
| 23 | 24 |
| 24 namespace android_webview { | 25 namespace android_webview { |
| 25 | 26 |
| 26 bool RegisterInputStream(JNIEnv* env) { | 27 bool RegisterInputStream(JNIEnv* env) { |
| 27 return JNI_InputStream::RegisterNativesImpl(env); | 28 return JNI_InputStream::RegisterNativesImpl(env); |
| 28 } | 29 } |
| 29 | 30 |
| 30 // Maximum number of bytes to be read in a single read. | 31 // Maximum number of bytes to be read in a single read. |
| 31 const int InputStreamImpl::kBufferSize = 4096; | 32 const int InputStreamImpl::kBufferSize = 4096; |
| 32 | 33 |
| 33 //static | 34 //static |
| 34 const InputStreamImpl* InputStreamImpl::FromInputStream( | 35 const InputStreamImpl* InputStreamImpl::FromInputStream( |
| 35 const InputStream* input_stream) { | 36 const InputStream* input_stream) { |
| 36 return static_cast<const InputStreamImpl*>(input_stream); | 37 return static_cast<const InputStreamImpl*>(input_stream); |
| 37 } | 38 } |
| 38 | 39 |
| 39 InputStreamImpl::InputStreamImpl() { | 40 InputStreamImpl::InputStreamImpl() { |
| 40 } | 41 } |
| 41 | 42 |
| 42 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream) | 43 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream) |
| 43 : jobject_(stream) { | 44 : jobject_(stream) { |
| 44 DCHECK(!stream.is_null()); | 45 DCHECK(!stream.is_null()); |
| 45 } | 46 } |
| 46 | 47 |
| 47 InputStreamImpl::~InputStreamImpl() { | 48 InputStreamImpl::~InputStreamImpl() { |
| 49 JNIEnv* env = AttachCurrentThread(); | |
| 50 Java_InputStream_close(env, jobject_.obj()); | |
| 51 DCHECK(!ClearException(env)); | |
|
joth
2013/01/03 19:18:18
the ClearException will be removed in release buil
mkosiba (inactive)
2013/01/04 12:31:11
uh, actually due to the way the JNI methods are au
benm (inactive)
2013/01/04 12:55:11
I see. So right now in the exception case we'll ne
mkosiba (inactive)
2013/01/04 14:41:01
When the bug is fixed we'll have an unsafe version
benm (inactive)
2013/01/04 16:59:40
Gotcha. Follow up in https://codereview.chromium.o
| |
| 48 } | 52 } |
| 49 | 53 |
| 50 bool InputStreamImpl::BytesAvailable(int* bytes_available) const { | 54 bool InputStreamImpl::BytesAvailable(int* bytes_available) const { |
| 51 JNIEnv* env = AttachCurrentThread(); | 55 JNIEnv* env = AttachCurrentThread(); |
| 52 // TODO: Use unsafe version for all Java_InputStream methods in this file | 56 // TODO: Use unsafe version for all Java_InputStream methods in this file |
| 53 // once BUG 157880 is fixed. | 57 // once BUG 157880 is fixed. |
| 54 int bytes = Java_InputStream_available(env, jobject_.obj()); | 58 int bytes = Java_InputStream_available(env, jobject_.obj()); |
| 55 if (ClearException(env)) | 59 if (ClearException(env)) |
| 56 return false; | 60 return false; |
| 57 *bytes_available = bytes; | 61 *bytes_available = bytes; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 env->GetByteArrayRegion(buffer, 0, byte_count, | 119 env->GetByteArrayRegion(buffer, 0, byte_count, |
| 116 reinterpret_cast<jbyte*>(dest->data() + *bytes_read)); | 120 reinterpret_cast<jbyte*>(dest->data() + *bytes_read)); |
| 117 if (ClearException(env)) | 121 if (ClearException(env)) |
| 118 return false; | 122 return false; |
| 119 | 123 |
| 120 *bytes_read = byte_count; | 124 *bytes_read = byte_count; |
| 121 return true; | 125 return true; |
| 122 } | 126 } |
| 123 | 127 |
| 124 } // namespace android_webview | 128 } // namespace android_webview |
| OLD | NEW |