| 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/browser/input_stream_impl.h" | 5 #include "android_webview/browser/input_stream.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/InputStreamUtil_jni.h" | 13 #include "jni/InputStreamUtil_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 | 20 |
| 21 namespace android_webview { | 21 namespace android_webview { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // This should be the same as InputStramUtil.EXCEPTION_THROWN_STATUS. | 25 // This should be the same as InputStramUtil.EXCEPTION_THROWN_STATUS. |
| 26 const int kExceptionThrownStatusCode = -2; | 26 const int kExceptionThrownStatusCode = -2; |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Maximum number of bytes to be read in a single read. | 29 // Maximum number of bytes to be read in a single read. |
| 30 const int InputStreamImpl::kBufferSize = 4096; | 30 const int InputStream::kBufferSize = 4096; |
| 31 | |
| 32 // static | |
| 33 const InputStreamImpl* InputStreamImpl::FromInputStream( | |
| 34 const InputStream* input_stream) { | |
| 35 return static_cast<const InputStreamImpl*>(input_stream); | |
| 36 } | |
| 37 | 31 |
| 38 // TODO: Use unsafe version for all Java_InputStream methods in this file | 32 // TODO: Use unsafe version for all Java_InputStream methods in this file |
| 39 // once BUG 157880 is fixed and implement graceful exception handling. | 33 // once BUG 157880 is fixed and implement graceful exception handling. |
| 40 | 34 |
| 41 InputStreamImpl::InputStreamImpl() {} | 35 InputStream::InputStream() {} |
| 42 | 36 |
| 43 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream) | 37 InputStream::InputStream(const JavaRef<jobject>& stream) : jobject_(stream) { |
| 44 : jobject_(stream) { | |
| 45 DCHECK(!stream.is_null()); | 38 DCHECK(!stream.is_null()); |
| 46 } | 39 } |
| 47 | 40 |
| 48 InputStreamImpl::~InputStreamImpl() { | 41 InputStream::~InputStream() { |
| 49 JNIEnv* env = AttachCurrentThread(); | 42 JNIEnv* env = AttachCurrentThread(); |
| 50 Java_InputStreamUtil_close(env, jobject_); | 43 if (jobject_.obj()) |
| 44 Java_InputStreamUtil_close(env, jobject_); |
| 51 } | 45 } |
| 52 | 46 |
| 53 bool InputStreamImpl::BytesAvailable(int* bytes_available) const { | 47 bool InputStream::BytesAvailable(int* bytes_available) const { |
| 54 JNIEnv* env = AttachCurrentThread(); | 48 JNIEnv* env = AttachCurrentThread(); |
| 55 int bytes = Java_InputStreamUtil_available(env, jobject_); | 49 int bytes = Java_InputStreamUtil_available(env, jobject_); |
| 56 if (bytes == kExceptionThrownStatusCode) | 50 if (bytes == kExceptionThrownStatusCode) |
| 57 return false; | 51 return false; |
| 58 *bytes_available = bytes; | 52 *bytes_available = bytes; |
| 59 return true; | 53 return true; |
| 60 } | 54 } |
| 61 | 55 |
| 62 bool InputStreamImpl::Skip(int64_t n, int64_t* bytes_skipped) { | 56 bool InputStream::Skip(int64_t n, int64_t* bytes_skipped) { |
| 63 JNIEnv* env = AttachCurrentThread(); | 57 JNIEnv* env = AttachCurrentThread(); |
| 64 int bytes = Java_InputStreamUtil_skip(env, jobject_, n); | 58 int bytes = Java_InputStreamUtil_skip(env, jobject_, n); |
| 65 if (bytes < 0) | 59 if (bytes < 0) |
| 66 return false; | 60 return false; |
| 67 if (bytes > n) | 61 if (bytes > n) |
| 68 return false; | 62 return false; |
| 69 *bytes_skipped = bytes; | 63 *bytes_skipped = bytes; |
| 70 return true; | 64 return true; |
| 71 } | 65 } |
| 72 | 66 |
| 73 bool InputStreamImpl::Read(net::IOBuffer* dest, int length, int* bytes_read) { | 67 bool InputStream::Read(net::IOBuffer* dest, int length, int* bytes_read) { |
| 74 JNIEnv* env = AttachCurrentThread(); | 68 JNIEnv* env = AttachCurrentThread(); |
| 75 if (!buffer_.obj()) { | 69 if (!buffer_.obj()) { |
| 76 // Allocate transfer buffer. | 70 // Allocate transfer buffer. |
| 77 base::android::ScopedJavaLocalRef<jbyteArray> temp( | 71 base::android::ScopedJavaLocalRef<jbyteArray> temp( |
| 78 env, env->NewByteArray(kBufferSize)); | 72 env, env->NewByteArray(kBufferSize)); |
| 79 buffer_.Reset(temp); | 73 buffer_.Reset(temp); |
| 80 if (ClearException(env)) | 74 if (ClearException(env)) |
| 81 return false; | 75 return false; |
| 82 } | 76 } |
| 83 | 77 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 dest_write_ptr += transfer_length; | 114 dest_write_ptr += transfer_length; |
| 121 } | 115 } |
| 122 // bytes_read can be strictly less than the req. length if EOF is encountered. | 116 // bytes_read can be strictly less than the req. length if EOF is encountered. |
| 123 DCHECK_GE(remaining_length, 0); | 117 DCHECK_GE(remaining_length, 0); |
| 124 DCHECK_LE(remaining_length, length); | 118 DCHECK_LE(remaining_length, length); |
| 125 *bytes_read = length - remaining_length; | 119 *bytes_read = length - remaining_length; |
| 126 return true; | 120 return true; |
| 127 } | 121 } |
| 128 | 122 |
| 129 } // namespace android_webview | 123 } // namespace android_webview |
| OLD | NEW |