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. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 JNIEnv* env = AttachCurrentThread(); | 77 JNIEnv* env = AttachCurrentThread(); |
78 if (!buffer_.obj()) { | 78 if (!buffer_.obj()) { |
79 // Allocate transfer buffer. | 79 // Allocate transfer buffer. |
80 base::android::ScopedJavaLocalRef<jbyteArray> temp( | 80 base::android::ScopedJavaLocalRef<jbyteArray> temp( |
81 env, env->NewByteArray(kBufferSize)); | 81 env, env->NewByteArray(kBufferSize)); |
82 buffer_.Reset(temp); | 82 buffer_.Reset(temp); |
83 if (ClearException(env)) | 83 if (ClearException(env)) |
84 return false; | 84 return false; |
85 } | 85 } |
86 | 86 |
87 int remaining_length = length; | |
88 bool eof = false; | |
89 char* dest_write_ptr = dest->data(); | |
87 jbyteArray buffer = buffer_.obj(); | 90 jbyteArray buffer = buffer_.obj(); |
88 *bytes_read = 0; | 91 *bytes_read = 0; |
89 | 92 |
90 const int read_size = std::min(length, kBufferSize); | 93 while (remaining_length > 0 && !eof) { |
91 int32_t byte_count; | 94 const int max_transfer_length = std::min(remaining_length, kBufferSize); |
92 do { | 95 const int transfer_length = Java_InputStream_readI_AB_I_I( |
93 // Unfortunately it is valid for the Java InputStream to read 0 bytes some | 96 env, jobject_.obj(), buffer, 0, max_transfer_length); |
94 // number of times before returning any more data. Because this method | |
95 // signals EOF by setting |bytes_read| to 0 and returning true necessary to | |
96 // call the Java-side read method until it returns something other than 0. | |
97 byte_count = Java_InputStream_readI_AB_I_I( | |
98 env, jobject_.obj(), buffer, 0, read_size); | |
99 if (ClearException(env)) | 97 if (ClearException(env)) |
100 return false; | 98 return false; |
101 } while (byte_count == 0); | 99 eof = (transfer_length < 0); |
102 | 100 // Note: it is possible, yet unlikely, that the Java InputStream returns |
103 // We've reached the end of the stream. | 101 // a transfer_length == 0 from time to time. In such cases we just continue |
104 if (byte_count < 0) | 102 // the read until we get either valid data or reach EOF. |
105 return true; | 103 if (!eof) { |
joth
2013/09/18 20:06:00
I'd find this easier as:
if (transfer_length < 0)
mkosiba (inactive)
2013/09/18 21:14:17
+1
Primiano Tucci (use gerrit)
2013/09/19 01:01:05
Hmm to be honest I did find a bit more readable ha
| |
106 | |
107 #ifndef NDEBUG | 104 #ifndef NDEBUG |
108 int32_t buffer_length = env->GetArrayLength(buffer); | 105 // These DCHECKs are in place to help Chromium developers in case of bugs. |
mkosiba (inactive)
2013/09/18 21:14:17
nit: you can just remove this comment. everyone kn
| |
109 DCHECK_GE(read_size, byte_count); | 106 DCHECK_GE(max_transfer_length, transfer_length); |
110 DCHECK_GE(buffer_length, byte_count); | 107 DCHECK_GE(env->GetArrayLength(buffer), transfer_length); |
111 #endif // NDEBUG | 108 #endif // NDEBUG |
112 | 109 |
113 // The DCHECKs are in place to help Chromium developers in case of bugs, | 110 // This check is to prevent a malicious InputStream implementation from |
114 // this check is to prevent a malicious InputStream implementation from | 111 // overrunning the |dest| buffer. |
115 // overrunning the |dest| buffer. | 112 if (transfer_length > max_transfer_length) |
116 if (byte_count > read_size) | 113 return false; |
117 return false; | |
118 | 114 |
119 // Copy the data over to the provided C++ side buffer. | 115 // Copy the data over to the provided C++ IOBuffer. |
120 DCHECK_GE(length, byte_count); | 116 DCHECK_GE(remaining_length, transfer_length); |
121 env->GetByteArrayRegion(buffer, 0, byte_count, | 117 env->GetByteArrayRegion(buffer, 0, transfer_length, |
122 reinterpret_cast<jbyte*>(dest->data() + *bytes_read)); | 118 reinterpret_cast<jbyte*>(dest_write_ptr)); |
123 if (ClearException(env)) | 119 if (ClearException(env)) |
124 return false; | 120 return false; |
125 | 121 remaining_length -= transfer_length; |
126 *bytes_read = byte_count; | 122 dest_write_ptr += transfer_length; |
123 } | |
124 } | |
125 // bytes_read can be strictly less than the req. length if EOF is encountered. | |
mkosiba (inactive)
2013/09/18 21:14:17
maybe add yet another DCHECK as well?
DCHECK(byte
| |
126 *bytes_read = length - remaining_length; | |
127 return true; | 127 return true; |
128 } | 128 } |
129 | 129 |
130 } // namespace android_webview | 130 } // namespace android_webview |
OLD | NEW |