OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "android_webview/native/input_stream_impl.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 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 |
| 10 // even if they're unused. |
| 11 #pragma GCC diagnostic push |
| 12 #pragma GCC diagnostic ignored "-Wunused-function" |
| 13 #include "jni/InputStream_jni.h" |
| 14 #pragma GCC diagnostic pop |
| 15 #include "net/base/io_buffer.h" |
| 16 |
| 17 using base::android::AttachCurrentThread; |
| 18 using base::android::ClearException; |
| 19 using base::android::JavaRef; |
| 20 using JNI_InputStream::Java_InputStream_available; |
| 21 using JNI_InputStream::Java_InputStream_skip; |
| 22 using JNI_InputStream::Java_InputStream_readI_AB_I_I; |
| 23 |
| 24 namespace android_webview { |
| 25 |
| 26 bool RegisterInputStream(JNIEnv* env) { |
| 27 return JNI_InputStream::RegisterNativesImpl(env); |
| 28 } |
| 29 |
| 30 // Maximum number of bytes to be read in a single read. |
| 31 const int InputStreamImpl::kBufferSize = 4096; |
| 32 |
| 33 //static |
| 34 const InputStreamImpl* InputStreamImpl::FromInputStream( |
| 35 const InputStream* input_stream) { |
| 36 return static_cast<const InputStreamImpl*>(input_stream); |
| 37 } |
| 38 |
| 39 InputStreamImpl::InputStreamImpl() { |
| 40 } |
| 41 |
| 42 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream) |
| 43 : jobject_(stream) { |
| 44 DCHECK(!stream.is_null()); |
| 45 } |
| 46 |
| 47 InputStreamImpl::~InputStreamImpl() { |
| 48 } |
| 49 |
| 50 bool InputStreamImpl::BytesAvailable(int* bytes_available) const { |
| 51 JNIEnv* env = AttachCurrentThread(); |
| 52 // TODO: Use unsafe version for all Java_InputStream methods in this file |
| 53 // once BUG 157880 is fixed. |
| 54 int bytes = Java_InputStream_available(env, jobject_.obj()); |
| 55 if (ClearException(env)) |
| 56 return false; |
| 57 *bytes_available = bytes; |
| 58 return true; |
| 59 } |
| 60 |
| 61 bool InputStreamImpl::Skip(int64_t n, int64_t* bytes_skipped) { |
| 62 JNIEnv* env = AttachCurrentThread(); |
| 63 int bytes = Java_InputStream_skip(env, jobject_.obj(), n); |
| 64 if (ClearException(env)) |
| 65 return false; |
| 66 if (bytes > n) |
| 67 return false; |
| 68 *bytes_skipped = bytes; |
| 69 return true; |
| 70 } |
| 71 |
| 72 bool InputStreamImpl::Read(net::IOBuffer* dest, int length, int* bytes_read) { |
| 73 JNIEnv* env = AttachCurrentThread(); |
| 74 if (!buffer_.obj()) { |
| 75 // Allocate transfer buffer. |
| 76 buffer_.Reset(env, env->NewByteArray(kBufferSize)); |
| 77 if (ClearException(env)) |
| 78 return false; |
| 79 } |
| 80 |
| 81 jbyteArray buffer = buffer_.obj(); |
| 82 *bytes_read = 0; |
| 83 |
| 84 while (length > 0) { |
| 85 const int read_size = std::min(length, kBufferSize); |
| 86 int32_t byte_count = |
| 87 Java_InputStream_readI_AB_I_I( |
| 88 env, jobject_.obj(), buffer, 0, read_size); |
| 89 |
| 90 if (ClearException(env)) |
| 91 return false; |
| 92 |
| 93 if (byte_count <= 0) |
| 94 break; |
| 95 |
| 96 #ifndef NDEBUG |
| 97 int32_t buffer_length = env->GetArrayLength(buffer); |
| 98 DCHECK_GE(read_size, byte_count); |
| 99 DCHECK_GE(buffer_length, byte_count); |
| 100 #endif // NDEBUG |
| 101 |
| 102 // The DCHECKs are in place to help Chromium developers in case of bugs, |
| 103 // this check is to prevent a malicious InputStream implementation from |
| 104 // overrunning the |dest| buffer. |
| 105 if (byte_count > read_size) |
| 106 return false; |
| 107 |
| 108 // Copy the data over to the provided C++ side buffer. |
| 109 DCHECK_GE(length, byte_count); |
| 110 env->GetByteArrayRegion(buffer, 0, byte_count, |
| 111 reinterpret_cast<jbyte*>(dest->data() + *bytes_read)); |
| 112 if (ClearException(env)) |
| 113 return false; |
| 114 |
| 115 *bytes_read += byte_count; |
| 116 length -= byte_count; |
| 117 } |
| 118 return true; |
| 119 } |
| 120 |
| 121 } // namespace android_webview |
OLD | NEW |