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/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 using JNI_InputStream::Java_InputStream_available; | |
21 using JNI_InputStream::Java_InputStream_close; | |
22 using JNI_InputStream::Java_InputStream_skip; | |
23 using JNI_InputStream::Java_InputStream_readI_AB_I_I; | |
24 | 20 |
25 namespace android_webview { | 21 namespace android_webview { |
26 | 22 |
| 23 namespace { |
| 24 |
| 25 // This should be the same as InputStramUtil.EXCEPTION_THROWN_STATUS. |
| 26 const int kExceptionThrownStatusCode = -2; |
| 27 |
| 28 } |
| 29 |
27 bool RegisterInputStream(JNIEnv* env) { | 30 bool RegisterInputStream(JNIEnv* env) { |
28 return JNI_InputStream::RegisterNativesImpl(env); | 31 return RegisterNativesImpl(env); |
29 } | 32 } |
30 | 33 |
31 // Maximum number of bytes to be read in a single read. | 34 // Maximum number of bytes to be read in a single read. |
32 const int InputStreamImpl::kBufferSize = 4096; | 35 const int InputStreamImpl::kBufferSize = 4096; |
33 | 36 |
34 //static | 37 //static |
35 const InputStreamImpl* InputStreamImpl::FromInputStream( | 38 const InputStreamImpl* InputStreamImpl::FromInputStream( |
36 const InputStream* input_stream) { | 39 const InputStream* input_stream) { |
37 return static_cast<const InputStreamImpl*>(input_stream); | 40 return static_cast<const InputStreamImpl*>(input_stream); |
38 } | 41 } |
39 | 42 |
40 // TODO: Use unsafe version for all Java_InputStream methods in this file | 43 // TODO: Use unsafe version for all Java_InputStream methods in this file |
41 // once BUG 157880 is fixed and implement graceful exception handling. | 44 // once BUG 157880 is fixed and implement graceful exception handling. |
42 | 45 |
43 InputStreamImpl::InputStreamImpl() { | 46 InputStreamImpl::InputStreamImpl() { |
44 } | 47 } |
45 | 48 |
46 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream) | 49 InputStreamImpl::InputStreamImpl(const JavaRef<jobject>& stream) |
47 : jobject_(stream) { | 50 : jobject_(stream) { |
48 DCHECK(!stream.is_null()); | 51 DCHECK(!stream.is_null()); |
49 } | 52 } |
50 | 53 |
51 InputStreamImpl::~InputStreamImpl() { | 54 InputStreamImpl::~InputStreamImpl() { |
52 JNIEnv* env = AttachCurrentThread(); | 55 JNIEnv* env = AttachCurrentThread(); |
53 Java_InputStream_close(env, jobject_.obj()); | 56 Java_InputStreamUtil_close(env, jobject_.obj()); |
54 } | 57 } |
55 | 58 |
56 bool InputStreamImpl::BytesAvailable(int* bytes_available) const { | 59 bool InputStreamImpl::BytesAvailable(int* bytes_available) const { |
57 JNIEnv* env = AttachCurrentThread(); | 60 JNIEnv* env = AttachCurrentThread(); |
58 int bytes = Java_InputStream_available(env, jobject_.obj()); | 61 int bytes = Java_InputStreamUtil_available(env, jobject_.obj()); |
59 if (ClearException(env)) | 62 if (bytes == kExceptionThrownStatusCode) |
60 return false; | 63 return false; |
61 *bytes_available = bytes; | 64 *bytes_available = bytes; |
62 return true; | 65 return true; |
63 } | 66 } |
64 | 67 |
65 bool InputStreamImpl::Skip(int64_t n, int64_t* bytes_skipped) { | 68 bool InputStreamImpl::Skip(int64_t n, int64_t* bytes_skipped) { |
66 JNIEnv* env = AttachCurrentThread(); | 69 JNIEnv* env = AttachCurrentThread(); |
67 int bytes = Java_InputStream_skip(env, jobject_.obj(), n); | 70 int bytes = Java_InputStreamUtil_skip(env, jobject_.obj(), n); |
68 if (ClearException(env)) | 71 if (bytes < 0) |
69 return false; | 72 return false; |
70 if (bytes > n) | 73 if (bytes > n) |
71 return false; | 74 return false; |
72 *bytes_skipped = bytes; | 75 *bytes_skipped = bytes; |
73 return true; | 76 return true; |
74 } | 77 } |
75 | 78 |
76 bool InputStreamImpl::Read(net::IOBuffer* dest, int length, int* bytes_read) { | 79 bool InputStreamImpl::Read(net::IOBuffer* dest, int length, int* bytes_read) { |
77 JNIEnv* env = AttachCurrentThread(); | 80 JNIEnv* env = AttachCurrentThread(); |
78 if (!buffer_.obj()) { | 81 if (!buffer_.obj()) { |
79 // Allocate transfer buffer. | 82 // Allocate transfer buffer. |
80 base::android::ScopedJavaLocalRef<jbyteArray> temp( | 83 base::android::ScopedJavaLocalRef<jbyteArray> temp( |
81 env, env->NewByteArray(kBufferSize)); | 84 env, env->NewByteArray(kBufferSize)); |
82 buffer_.Reset(temp); | 85 buffer_.Reset(temp); |
83 if (ClearException(env)) | 86 if (ClearException(env)) |
84 return false; | 87 return false; |
85 } | 88 } |
86 | 89 |
87 int remaining_length = length; | 90 int remaining_length = length; |
88 char* dest_write_ptr = dest->data(); | 91 char* dest_write_ptr = dest->data(); |
89 jbyteArray buffer = buffer_.obj(); | 92 jbyteArray buffer = buffer_.obj(); |
90 *bytes_read = 0; | 93 *bytes_read = 0; |
91 | 94 |
92 while (remaining_length > 0) { | 95 while (remaining_length > 0) { |
93 const int max_transfer_length = std::min(remaining_length, kBufferSize); | 96 const int max_transfer_length = std::min(remaining_length, kBufferSize); |
94 const int transfer_length = Java_InputStream_readI_AB_I_I( | 97 const int transfer_length = Java_InputStreamUtil_read( |
95 env, jobject_.obj(), buffer, 0, max_transfer_length); | 98 env, jobject_.obj(), buffer, 0, max_transfer_length); |
96 if (ClearException(env)) | 99 if (transfer_length == kExceptionThrownStatusCode) |
97 return false; | 100 return false; |
98 | 101 |
99 if (transfer_length < 0) // EOF | 102 if (transfer_length < 0) // EOF |
100 break; | 103 break; |
101 | 104 |
102 // Note: it is possible, yet unlikely, that the Java InputStream returns | 105 // Note: it is possible, yet unlikely, that the Java InputStream returns |
103 // a transfer_length == 0 from time to time. In such cases we just continue | 106 // a transfer_length == 0 from time to time. In such cases we just continue |
104 // the read until we get either valid data or reach EOF. | 107 // the read until we get either valid data or reach EOF. |
105 if (transfer_length == 0) | 108 if (transfer_length == 0) |
106 continue; | 109 continue; |
(...skipping 16 matching lines...) Expand all Loading... |
123 remaining_length -= transfer_length; | 126 remaining_length -= transfer_length; |
124 dest_write_ptr += transfer_length; | 127 dest_write_ptr += transfer_length; |
125 } | 128 } |
126 // bytes_read can be strictly less than the req. length if EOF is encountered. | 129 // bytes_read can be strictly less than the req. length if EOF is encountered. |
127 DCHECK(remaining_length >= 0 && remaining_length <= length); | 130 DCHECK(remaining_length >= 0 && remaining_length <= length); |
128 *bytes_read = length - remaining_length; | 131 *bytes_read = length - remaining_length; |
129 return true; | 132 return true; |
130 } | 133 } |
131 | 134 |
132 } // namespace android_webview | 135 } // namespace android_webview |
OLD | NEW |