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. |
| (...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 char* dest_write_ptr = dest->data(); | |
| 87 jbyteArray buffer = buffer_.obj(); | 89 jbyteArray buffer = buffer_.obj(); |
| 88 *bytes_read = 0; | 90 *bytes_read = 0; |
| 89 | 91 |
| 90 const int read_size = std::min(length, kBufferSize); | 92 while (remaining_length > 0) { |
| 91 int32_t byte_count; | 93 const int max_transfer_length = std::min(remaining_length, kBufferSize); |
| 92 do { | 94 const int transfer_length = Java_InputStream_readI_AB_I_I( |
| 93 // Unfortunately it is valid for the Java InputStream to read 0 bytes some | 95 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)) | 96 if (ClearException(env)) |
| 100 return false; | 97 return false; |
| 101 } while (byte_count == 0); | 98 if (transfer_length < 0) // EOF |
| 102 | 99 break; |
|
joth
2013/09/19 01:11:58
nit: I'd put \n after each these early-outs
Primiano Tucci (use gerrit)
2013/09/19 01:48:57
Done.
| |
| 103 // We've reached the end of the stream. | 100 // Note: it is possible, yet unlikely, that the Java InputStream returns |
| 104 if (byte_count < 0) | 101 // a transfer_length == 0 from time to time. In such cases we just continue |
| 105 return true; | 102 // the read until we get either valid data or reach EOF. |
| 106 | 103 if (transfer_length > 0) { |
|
joth
2013/09/19 01:11:58
heh, we could go further on this line
if (transfe
Primiano Tucci (use gerrit)
2013/09/19 01:48:57
Done.
| |
| 107 #ifndef NDEBUG | 104 #ifndef NDEBUG |
| 108 int32_t buffer_length = env->GetArrayLength(buffer); | 105 DCHECK_GE(max_transfer_length, transfer_length); |
| 109 DCHECK_GE(read_size, byte_count); | 106 DCHECK_GE(env->GetArrayLength(buffer), transfer_length); |
| 110 DCHECK_GE(buffer_length, byte_count); | |
| 111 #endif // NDEBUG | 107 #endif // NDEBUG |
|
joth
2013/09/19 01:11:58
the "#ifndef NDEBUG" guard not needed here now. (i
Primiano Tucci (use gerrit)
2013/09/19 01:48:57
Done.
| |
| 112 | 108 |
| 113 // The DCHECKs are in place to help Chromium developers in case of bugs, | 109 // This check is to prevent a malicious InputStream implementation from |
| 114 // this check is to prevent a malicious InputStream implementation from | 110 // overrunning the |dest| buffer. |
| 115 // overrunning the |dest| buffer. | 111 if (transfer_length > max_transfer_length) |
| 116 if (byte_count > read_size) | 112 return false; |
| 117 return false; | |
| 118 | 113 |
| 119 // Copy the data over to the provided C++ side buffer. | 114 // Copy the data over to the provided C++ IOBuffer. |
| 120 DCHECK_GE(length, byte_count); | 115 DCHECK_GE(remaining_length, transfer_length); |
| 121 env->GetByteArrayRegion(buffer, 0, byte_count, | 116 env->GetByteArrayRegion(buffer, 0, transfer_length, |
| 122 reinterpret_cast<jbyte*>(dest->data() + *bytes_read)); | 117 reinterpret_cast<jbyte*>(dest_write_ptr)); |
| 123 if (ClearException(env)) | 118 if (ClearException(env)) |
| 124 return false; | 119 return false; |
| 125 | 120 remaining_length -= transfer_length; |
| 126 *bytes_read = byte_count; | 121 dest_write_ptr += transfer_length; |
| 122 } | |
| 123 } | |
| 124 // bytes_read can be strictly less than the req. length if EOF is encountered. | |
| 125 DCHECK(remaining_length >= 0 && remaining_length <= length); | |
| 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 |